Programming Fundamentals: The Binary Number System

Computers store and operate on data represented in the binary number system. A great many aspects of computer science require some understanding of the binary number system. In order to understand the binary number system, it is often helpful to examine a number system we are all familiar with.
At some point in grammar school, you learned the “decimal” number system. Such a number system is based upon units of “10″. In mathematics and computer science we call the decimal number system the “Base 10″ number system. We use the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 to represent numbers in the following way:
DigitsRepresentation
0Value of 0
1Value of 1
2Value of 2
3Value of 3
4Value of 4
5Value of 5
6Value of 6
7Value of 7
8Value of 8
9Value of 9
10One “group” of 10 followed by the value of 0
11One “group” of 10 followed by the value of 1
20Two “groups” of 10 followed by the value of 0
100One “group” of 100 followed by zero groups of 10 followed by the value of 0
125One “group” of 100 followed by 2 groups of 10 followed by the value of 5
In other words, when we work with the decimal (Base 10) number system, we tend to think in groups of 10, 100, 1000 and so on. We count up using the symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Each successive “place” in a number is a multiple of 10. That is, we have the “ones”. “tens”, “hundreds”, “thousands” and so on.
  Number:  125

  Hundreds       Tens       Ones
    1              2          5

  Number:  3461

  Thousands    Hundreds       Tens       Ones
      3            4           6           1
In the binary (Base 2) number system, a digit can only take on one of two values: Either 0 or 1. We call such a digit a “bit” (short for “Binary Digit”). Each successive “bit” in a binary number is a multiple of 2. That is we have the “ones”, “twos”, “fours”, “eights”, “sixteens”, “thirtytwos” and so on. Below are some examples of binary numbers:
Decimal DigitsBinary DigitsRepresentation
00Value of 0
11Value of 1
210One “group” of 2 followed by the value 0
311One “group” of 2 followed by the value 1
4100One “group” of 4 followed by 0 groups of 2 followed by the value 0
5101One “group” of 4 followed by 0 groups of 2 followed by the value 1
6110One “group” of 4 followed by 1 group of 2 followed by the value 0
7111One “group” of 4 followed by 1 group of 2 followed by the value 1
81000One “group” of 8, 0 groups of 4, 0 groups of 2 and the value 0
2010100One “group” of 16, 0 groups of 8, 1 group of 4, 0 groups of 2 and the value 0
10011001001 group of 64, 1 group of 32, 0 groups of 16, 0 groups of 8, 1 group of 4, 0 groups of 2 and 0
12511111011 group of 64, 1 group of 32, 1 group of 16, 1 group of 8, 1 group of 4, 0 groups of 2 and 1
Computer scientists routinely memorize powers of 2. The process is simply to start with 0, 1 and then to double the number:
0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 
4098, 8192, 16384, 32768, 65536 and so on...
You may have notices that the RAM memory in your PC is also quite likely a power of 2:
8   Megabytes of RAM
16  Megabytes of RAM
32  Megabytes of RAM
64  Megabytes of RAM
128 Megabytes of RAM
256 Megabytes of RAM
and so on
Some questions we commonly ask are:
  • If I need to store the number 1 through 100, how many bits will I need?
    We see that with 7 bits, we can represent numbers from 0 to 127. So 7 bits is all we would need to store numbers from 1 to 100.
  • What is the largest number I can represent using 16 bits?
    The fastest way to calculate this is to take 2 to the 16th power (216) and then subtract 1.
    2 16 = 65,536
    So the largest number we can represent with 16 bits is: 65,536 – 1 = 65,535
  • In my database, I can store 28 records in one disk block (a fixed sized storage area on the disk). How many bits would be required to index all of the records in one block?
    One way to answer this is to just run up the powers of 2:
    0, 1, 2, 4, 8, 16, 32, 64…
    Since with 5 bits we can represent numbers from 0 to 31, (recall that 2 to the 5th power is 32), all we should need are 5 bits to index the 28 record entries in the block.
  • In networking, version 4 of the TCP/IP protocol designates IP addresses that are 32 bits long. How many hosts can be defined using 32 bits?
  • Also in networking, the ethernet LAN protocol specifies that every ethernet device have a 48-bit Media Access Control (MAC) address.