0 Notes
0 Notes
7, 2016
Welcome to COMP 273. Let’s get started! You all know that computers represent numbers using
0’s and 1’s. But how exactly this works is probably a mystery for most of you. We start out
with a basic and fundamental example, namely how to represent integers. We’ll start with positive
integers, then negative integers, and then we’ll turn to non-integers i.e. real numbers.
When m is already represented as binary number (i.e. “base 2”), the quotient and remainder
are trivial to obtain. The remainder is the rightmost bit – called the least significant bit (LSB). The
quotient is the number with the LSB chopped off. To convince yourself of this, note that writing a
positive integer as an n bit binary number means that you write it as a sum of powers of 2,
n−1
X n−1
X n−2
X
i i
(bn−1 bn−2 . . . b2 b1 b0 )two ≡ bi 2 = bi 2 + b0 = 2 × bi+1 2i + b0
i=0 i=1 i=0
For example,
Thus,
241ten = 11110001two .
You need to be able to do this for yourself. So practice it!
If you are not fully clear why the algorithm is correct, consider what happens when you run
the algorithm where you already have the number in binary representation. (The quotient and
remainder of a division by 2 does not depend on how you have represented the number i.e. whether
it is represented in decimal or binary.)
(quotient) (remainder)
11110001
1111000 1
111100 0
11110 0
1111 0
111 1
11 1
1 1
0 1
0 0
0 0
: :
The remainders are simply the bits used in the binary representation of the number!
Final note: The representation has an infinite number of 0’s on the left which can be truncated.
I mention this because in the heat of an exam you might get confused about which way to order
the 0’s and 1’s. Remembering that you have infinitely many 0’s when you continue to apply the
trick. This tells you that the remainder bits go from right to left.
Its the same algorithm that you use with decimal, except that you are only allowed 0’s and 1’s.
Whenever the sum in a column is 2 or more, you carry to the next column:
2i + 2i = 2i+1
We would like to apply the grade school algorithm to do subtraction, multiplication and long division
in binary. However, there are some subtleties in doing so. For example, when you subtract a big
positive number from a small positive number, you end up with a negative number, and I have not
yet told you how to represent negative numbers. That’s what I’ll start with next lecture.