Binary Number Representation
Binary Number Representation
(11010)2 = 1 ∗ 24 + 1 ∗ 23 + 0 ∗ 22 + 1 ∗ 21 + 0 ∗ 20 .
I have put little subscripts (10 and 2) to indicate that we are using a particular representation
(decimal or binary). We don’t need to always put this subscript in, but sometimes it helps.
You know how to count in decimal, so let’s consider how to count in binary. You should verify
that the binary representation is a sum of powers of 2 that indeed corresponds to the decimal
representation in the leftmost column. In the left two columns below, I only used as as many digits
or bits as I needed to represent the number. In the right column, I used a fixed number of bits,
namely 8. 8 bits is called a byte.
Then, for any binary number, you write each of its ’1’ bits as a power of 2 using the decimal
representation you are familiar with. Then you add up these decimal numbers, e.g.
110102 = 16 + 8 + 2 = 26.
The other direction is more challenging, however. How do you convert a decimal number to
binary? I will give a simple algorithm for doing so soon which is based on the following idea. I first
explain the idea in base 10 where we have a better intuition. Let m be a positive integer which is
written in decimal. Then,
m = 10 ∗ (m/10) + (m%10).
Note that m/10 chops off the rightmost digit and multiplying by 10 tags on a 0. So dividing and the
multiplying by 10 might not get us back to the original number. What is missing is the remainder
part, which we dropped in the divison.
In binary, the same idea holds. If we represent a number m in binary and we divide by 2,
then we chop off the rightmost bit which becomes the remained and we shift right each bit by one
position. To multiply by 2, we shift the bits to the left by one position and put a 0 in the rightmost
position. So, for example, if
m = (11011)2 = 1 ∗ 24 + 1 ∗ 23 + 1 ∗ 21 + 1 ∗ 20
(11010)2 = 1 ∗ 24 + 1 ∗ 23 + 1 ∗ 21 .
Note this algorithm doesn’t say anything about how m is represented. But in practice, since you
are human, and so m is represented in decimal.
Thus, (241)10 = (11110001)2 . Note that there are an infinite number of 0’s on the left which are
higher powers of 2 which we ignore.
Now let’s apply these ideas to the algorithm for converting to binary. Representing a positive
integer m in binary means that we write it as a sum of powers of 2:
n−1
X
m= bi 2i
i=0
where bi is a bit, which has a value either 0 or 1. So we write m in binary as a bit sequence
(bn−1 bn−2 . . . b2 b1 b0 )2 . In particular,
m % 2 = b0
m / 2 = (bn−1 . . . b2 b1 )2
Thus, we can see that the algorithm for converting to binary, which just repeats the mod and
division operations, essentially just read off the bits of the binary representation of the number!
If you are still not convinced, let’s run another example where we “know” the answer from the
start and we’ll see that the algorithm does the correct thing. Suppose our number is m = 241,
which is (11110001)2 in binary. The algorithm just reads off the rightmost bit of m each time that
we divide it by 2.
i m b[i]
(11110001)2
0 (1111000)2 1
1 (111100)2 0
2 (11110)2 0
3 (1111)2 0
4 (111)2 1
5 (11)2 1
6 (1)2 1
7 0 1
Arithmetic in binary
Let’s add two numbers which are written in binary. I’ve written the binary representation on the
left and the decimal representation on the right.
11010 <-carries
11010 26
+ 1011 +11
------ ----
100101 37
Make sure you see how this is done, namely how the “carries” work. For example, in column 0, we
have 0 + 1 and get 1 and there is no carry. In column 1, we have 1 + 1 (in fact, 1 ∗ 21 + 1 ∗ 21 ) and
we get 2 ∗ 21 = 22 and so we carry a 1 over column 2 which represents the 22 terms. Make sure you
understand how the rest of the carries work.
m = bN −1 2N −1 + bN −2 2N −2 + . . . b1 2 + b0
where bN −1 = 1, that is, we only use as many bits as we need. This is similar to the idea that
in decimal we don’t usually write, for example, 0000364 but rather we write 364. We don’t write
0000364 because the 0’s on the left don’t contribute anything.
Let’s derive an expression for how many bits N we need to represent n. I will do so by deriving
a lower bound and an upper bound for N . First, the lower bound: Since bN −1 = 1 and since each
of the other bi ’s is either 0 or 1 for 0 ≤ i < N − 1, we have
m ≤ 2N −1 + 2N −2 + · · · + 2 + 1. (∗)
To go further, I will next use of the following claim which many of you have seen from Calculus:
for any real number number x,
N −1
X xN − 1
xi = . (∗∗)
i=0
x−1
The proof of this fact goes as follows. Take the sum on the left and multiply by x − 1 and expand:
N
X −1 N
X N
X −1
i i
x (x − 1) = x − xi
i=0 i=1 i=0
Note the indices of the first sum on the right go from 1 to N and the second go from 0 to N − 1.
Because we are taking a difference on the right, all terms cancel except for two, namely
N
X N
X −1
i
x − xi = xN − 1.
i=1 i=0
Thus,
N
X −1
xi (x − 1) == xN − 1.
i=0
which is what I claimed above.
If we consider the case x = 2 we get
N
X −1
2i = 2N − 1.
i=0