Number Bases For Integers

Base Ten (Decimal)

Humans are most familiar with decimal numbers.

The ten refers to two things:
There are ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
And as we go right to left in a decimal number, each place value is ten times the place value to its right.

_ _ _ _ _ _
          ^ ones place
        ^ tens place
      ^ hundreds place
    ^ thousands place

Base Two (Binary)

Integers are stored on the computer in Binary.

The two refers to two things:
There are two digits: 0, 1.
And as we go from right to left in a binary number, each place value is two times the place value to its right.

_ _ _ _ _ _
          ^ ones place
        ^ twos place
      ^ fours place
    ^ eights place
  ^ sixteens place
^ thirty-twos place
Notice that binary digits are usually called bits. The "b" from binary and the "its" from the last letters of digits.

Addition of binary numbers

Two binary numbers are added like two decimal numbers: a column at a time starting on the right. There are only 8 possible columns. Look below. The bit on top is the carry from the previous column. If there is no previous column, then the carry is 0. The second bit in each column is the bit from the top number. The third bit in each column is the bit from the bottom number. The result of each addition appears as a two bit number under the --. If there are no 1's being added, then the result is 00. If there is a single one being added, then the result is 01. If two ones are being added the result is two, written in binary as 10. That is, 1 two and 0 ones Finally if three ones are being added the result is three, written in binary as 11. That is, 1 two and 1 one.
 0    1    0    0    0    1    1    1
 0    0    1    0    1    0    1    1
+0   +0   +0   +1   +1   +1   +0   +1
--   --   --   --   --   --   --   --
00   01   01   01   10   10   10   11
When we add two binary numbers we work a column at a time from right to left. We write the right bit of each result under its column and use the left bit of each result as the carry into the next column. Example:
 11110
+ 1101
------
As a first step we fill out the bottom number with a leading 0 an put a carry of zero in the right hand column
     0
 11110
+01101
------
The result from the first column is 01. So we write down the 1 and carry the zero:
    00
 11110
+01101
------
     1
The result from the second column is also 01, so we write down the 1 and carry the zero:
   000
 11110
+01101
------
    11
The result from the third column is also 10, so we write down the 0 and carry the 1:
  1000
 11110
+01101
------
   011
The result from the fourth column is also 11, so we write down the 1 and carry the 1:
 11000
 11110
+01101
------
  1011
The result from the fifth column is also 10, so we write down the 0 and carry the 1. But there are no more bits (zeros?) so write the 1 in the sixth column
 11000
 11110
+01101
------
101011

Conversions Between Decimal And Binary

Conversion from Binary to Decimal.

Example: Convert 01101011 Binary to Decimal.

Work from left to right. Start with ans=0. For each bit, double the ans and add the digit into the ans.

Working left to right.
pattern: ans *= 2;     ans += d;  where d is the digit

ans *= 2;  ans += 0;   so ans=0
ans *= 2;  ans += 1;   so ans=1
ans *= 2;  ans += 1;   so ans=3
ans *= 2;  ans += 0;   so ans=6
ans *= 2;  ans += 1;   so ans=13
ans *= 2;  ans += 0;   so ans=26
ans *= 2;  ans += 1;   so ans=53
ans *= 2;  ans += 1;   so ans=107
107 Decimal is the equivalent value.

Alternate approach: The left-most 1 is in the sixty-fours place, so we have: 64+32+8+2+1=107

Convert from Decimal to Binary

Example: Convert 297 Decimal to Binary.

There are two steps (both are C operations):

Remainder: num%2 is the remainder when dividing num by 2. It is 0 if num is even and 1 if num is odd.
Quotient: num /= 2 puts the quotient of the division into num. Suppose num is 9. Then num /= 2 puts 4 into num.

Start with num=297.
The bits will be found from right to left.
Step 1: Compute num%2. That's a bit in the answer! Write it down. Move to the left to be ready to write the next bit.
Step 2: Get the next num: num /=2
Repeat these two steps. Stop when num==0.

num=297                               Binary = ---------
r = num%2 = 1   q = (num /= 2) = 148  Binary = --------1
r = num%2 = 0   q = (num /= 2) =  74  Binary = -------01
r = num%2 = 0   q = (num /= 2) =  37  Binary = ------001
r = num%2 = 1   q = (num /= 2) =  18  Binary = -----1001
r = num%2 = 0   q = (num /= 2) =   9  Binary = ----01001
r = num%2 = 1   q = (num /= 2) =   4  Binary = ---101001
r = num%2 = 0   q = (num /= 2) =   2  Binary = --0101001
r = num%2 = 0   q = (num /= 2) =   1  Binary = -00101001
r = num%2 = 1   q = (num /= 2) =   0  Binary = 100101001

next: Integer Storage on Computers