Introduction to Binary Numbers


Binary numerals

Binary numerals are a way of expressing any whole numbers (1,2,3,...) using only the two binary digits, also called bits: 0 and 1. For those of you who have studied arithemtic in different bases, binary numerals are base 2 numbers. Binary numerals are a positional number system, where the powers of 10: 1's 10's 100's 1000's etc for decimal numerals are replaced by the powers of 2: 1's 2's 4's 8's 16's 32's 64's for binary numbers.

Thus, the binary numeral 1011 represents 11 because it has a 1 in the 8's, 2's, and 1's places and so represents the number with one 8, one 2, and one 1, that is, 8+2+1 = 11.

place 128 64 32 16 8 4 2 1 1 0 1 1 ================ 8 + 2+ 1 = 11 Exercise 1: You should try to convert the following binary numerals to decimal: 111, 10001, 101010, 1111111. The answers are here

For example, the binary numerals for 0 and the first several integers are as follows:

0 0 1 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 10 1010 11 1011 12 1100 13 1101 14 1110 15 1111 16 10000 17 10001 18 10010 19 10011 20 10100 21 10101 22 10110 23 10111 24 11000 25 11001 26 11010 27 11011 28 11100 29 11101 30 11110 31 11111 32 100000
Observe that with five bits we can represent every number between 0 and 31, inclusive. This gives us 32 different numbers. In general with k bits you can represent 2^k different numbers, where 2^k is 2 to the power k, which is the product of 2 by itself k times. Thus, with 10 bits we can represent all numbers from 0 to 2^10-1 = 1024-1 = 1023.

This fact can be used to count to 1000 on your fingers by resting your palm on a table (or your knee) and then representing a zero by lifting a finger off the table and a one by placing the finger on the table. Letting your little finger represent the 1's, your ring finger represent 2's, your middle finger 4's, pointer 8's and thumb 16's, you can count to 31 with one hand. It turns out that the counting motions are fairly easy to learn and if you then add your other hand in, you can count to (2^10)-1 = 1,023 with two hands. See if you can find the pattern that allows you to count to 1023 with your ten fingers.


Powers of two

The first 20 powers of two are listed in the following table:

1 2^0 2 2^1 4 2^2 8 2^3 16 2^4 32 2^5 64 2^6 128 2^7 256 2^8 512 2^9 1024 2^10 2048 2^11 4096 2^12 8192 2^13 16384 2^14 32768 2^15 65536 2^16 131072 2^17 262144 2^18 524288 2^19 1048576 2^20 Observe in particular that 2^10 is about 1000 and 2^ 20 is about one million. One convenient fact about binary numbers is that the largest binary number you can represent with k bits is 2^k-1, as shown in the following table: k largest k bit its value binary number in decimal 1 1 1= 1 2 11 3= 2+1 3 111 7= 4+2+1 4 1111 15= 8+4+2+1 5 11111 31= 16+8+4+2+1 6 111111 63= 32+16+8+4+2+1 7 1111111 127= 64+32+16+8+4+2+1 8 11111111 255= 128+64+32+16+8+4+2+1 9 111111111 511= 256+128+64+32+16+8+4+2+1 10 1111111111 1023= 512+256+128+64+32+16+8+4+2+1 Thus, using 10 bits we can express about 1000 numbers. Similarly, 20 bits can represent about 1 million numbers, 30 bits represent about 1 billion, etc.


IP addresses and sizes of subnets

Recall that an IP address is a number that is 4 bytes long. Thus, there are exactly 256*256*256*256 = 4,294,967,296 IP addresses. This is about 4 billion. The number of IP addresses in subnets of levels 1,2,3 can be calculated in the same way: level IP address number of addresses 0 xxx.yyy.zzz.www 256*256*256*256 = 4,294,967,296 1 AAA.xxx.yyy.zzz 256*256*256 = 16,777,216 2 AAA.BBB.xxx.yyy 256*256 = 65,536 3 AAA.BBB.CCC.xxx 256 = 256


Converting Decimal to Binary

You can convert from decimal to binary by expressing your decimal number as a sum of powers of two (with no repeats). The easiest way to do this for small numbers is by repeatedly subtracting the biggest possible powers of two from your number. Thus, to convert 43 to binary, we would
  1. first find the highest power of two that isn't bigger than 43 and subtract it from 43. This would be 32 and 43 - 32 = 11. In other words, 43 = 32 + 11.
  2. find the highest power of two that isn't bigger than 11 and subtract from 11. This would be 8, and 11-8 = 3, so 43 = 32 + 8 + 3.
  3. Finally, 3 = 2 + 1, so 43 = 32 + 8 + 2 + 1. If we then put a 1 in the 32,8,2, and 1 places and 0 in the other places we get the binary representation for 43: 101011. place 128 64 32 16 8 4 2 1 1 0 1 0 1 1 ========================================== 32 + 8+ 2+ 1 = 43 Binary numbers that consist of 8 bits (i.e. 8 place binary numbers) are called bytes and they play a prominent role in today's computer technology. Observe that one byte can represent any number from 0 to 128+64+32+16+8+4+2+1 = 255. place 128 64 32 16 8 4 2 1 1 1 1 1 1 1 1 1 ========================================== 128+ 64+ 32+ 16+ 8+ 4+ 2+ 1 You should try converting the following decimals to binary: Exercise 2: You should try to convert the following decimals to binary: 10, 99, 100, 111. here



    Answers to exercises