Binary Numbers and Positive Integers

For a definition of binary numbers visit this Wikipedia page

Computers store data using the binary system. This is based on the number 2 and numbers in the system use the digits 0 and 1. That is 2 digits in all.  Like a decimal, each place (or column) in a number can be represented by a power. In this case it is a power of 2 as the system is 2 based. An example of a 2 based number is 101. This can be thought of as: 1 four, plus 0 twos, plus 1 one.The table below shows how this can be represented.

 22 = 4
 21 = 2
 20 = 1
 1 0 1

Its decimal equivalent is: 5 = 1x4 + 0x2 + 1x1
or:1x22 + 0x21 + 1x20

Each column or place in the number is represented by what is known as a bit. This is short for binary digit.

Here is another example, this time using 4 bits.

 23 = 8
 22 = 4
 21 = 2
 20 = 1
 1  1 1 1

Its decimal equivalent is: 15 = 1x8 + 1x4 +1x2 + 1x1
or: 24 - 1   (16 - 1 = 15)

Using 4 bits there are 24 (16) different combinations of zeros and ones, 0000 being the smallest and 1111 the largest. There is a simple formula to work out the largest decimal number that can be represented by a certain number of bits all set to 1. It is 2 to the power of the number of bits minus 1. One is taken off because the system starts at 0. So, using four bits the following decimal numbers can be represented: 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15. This is sixteen numbers in all.

Particular bit patterns have names. Four bits is called a nibble, eight bits is called a byte, four bytes is called a word. Using the formula above a byte can represent a maximum decimal value of 28 - 1, or 255.

Different systems within a computer can use different numbers of bits. For example, most modern processors can read/write 64 bits of data at a time from/to RAM, whereas the Windows 7 operating system comes in both 32 and 64 bit versions. For various reasons the 32 bit version limits the amount of RAM it can address to around 3 Gigabytes, so anything above this figure is useless. Visit this page to learn more. Another example is the number of bits used to represent colours on a monitor. Not too many years ago 8 bit systems were the norm and this limited the number of colours to around 28 or 256. These colours were commonly referred to as "the web safe colours". Today, most computers use a 24 or even 32 bit colour system. This allows for many millions of colours - more than the human eye can distinguish.

In the examples above binary numbers are converted to their decimal equivalents. For example, using 4 bits, 7 can be converted to 0111. The conversion algorithm or method goes like this:
  1. find the highest power of 2 that is less than the target number an put a 1 in that place.
  2. take the power of 2 away from the target number and make this value the new target number.
  3. repeat step 1 until 1 or 0 is left as a remainder.
  4. fill any places that do not contain a 1 with zeros.
For example, using 8 bits 127 is:

127 - 64 = 63
 27 = 128
 26 = 64
 25 = 32
 24 = 16
 23 = 8
 22 = 4
 21 = 2
 20 = 1
   1            

63 - 32 = 31
 27 = 128
 26 = 64
 25 = 32
 24 = 16
 23 = 8
 22 = 4
 21 = 2
 20 = 1
   1  1          

31 - 16 = 15
 27 = 128
 26 = 64
 25 = 32
 24 = 16
 23 = 8
 22 = 4
 21 = 2
 20 = 1
   1  1  1        


15 - 8 = 7
 27 = 128
 26 = 64
 25 = 32
 24 = 16
 23 = 8
 22 = 4
 21 = 2
 20 = 1
   1  1  1  1      


7 - 4 = 3
 27 = 128
 26 = 64
 25 = 32
 24 = 16
 23 = 8
 22 = 4
 21 = 2
 20 = 1
   1  1  1  1  1    


3 - 2 = 1
 27 = 128
 26 = 64
 25 = 32
 24 = 16
 23 = 8
 22 = 4
 21 = 2
 20 = 1
   1  1  1  1  1  1  

1
 27 = 128
 26 = 64
 25 = 32
 24 = 16
 23 = 8
 22 = 4
 21 = 2
 20 = 1
   1  1  1  1  1  1  1


Fill in empty places with zeros
 27 = 128
 26 = 64
 25 = 32
 24 = 16
 23 = 8
 22 = 4
 21 = 2
 20 = 1
 0  1  1  1  1  1  1  1


Adding binary numbers

Binary numbers can be added just like decimal numbers. The rule is that  0 added to 1 gives 1; 1 added to 0 gives 1; 1 added to 1 gives 0 with 1 carried to the next column to be included in the next addition. If there are two ones in a column and there is a 1 carry then a 1 is put down in the result and 1 is carried to the next column. The example below adds 0101 (5) to 0011 (3) using a 4 bit system.

decimal value
  8  4  2  1  
carry  0 1  1  1    decimal
    0  1  0  1  5
    0  0  1  1  3
result   1  0  0  0  8

This example adds 5 and 7

decimal value
  8  4  2  1  
carry  0 1  1  1    decimal
    0  1  0  1  5
    0  1  1  1  7
result   1  1  0  0  12

However, what happens when 5 is added to 11?

decimal value
 16  8  4  2  1  
carry  1  1  1  1    decimal
     0  1  0  1  5
     1  0  1  1  11
result  1  0  0  0  0 16? (not if only 4 bits are used)

This is a case of overflow because we would need an extra bit for the 1 that would enable 16 to be represented. Using just 4 bits 16 would be represented by 0000. This is zero, which is plainly not the right answer. So, any system has its limits. If an addition of decimal numbers results in a number that is 2n or higher, where n is the number of bits used, an overflow will occur. For example, using 32 bits the limit is 232 or 4294967296. This is a large number, but it is easy to exceed it in computer programs. When this happens the result may 'wrap around'. This is where a negative number becomes positive and vice-versa.

A recent example of decimal overflow was the Year 2K problem where it was thought that the year dates in legacy computer programs (that were still in use) would change to 1900 when the year 2000 was reached. This was because in these old programs years were represented using two digits to conserve space on punch cards. This used to be the way data was fed into computers. So, the year 1958 would be represented by the number 58, and the years 1900 and 2000 both by 00. In the event, the Y2K problem did not cause a world wide meltdown in computer systems, but a lot of programmers made very good money fixing suspect old programs. Click on this link to read more.


Multiplying or dividing by 2

It is easy to multiply a binary number by 2, just add a zero to its right-hand end (and drop the extra digit from the right-hand end). The following examples use 4 bits.

Double 4: 0100 => 1000 (8)
Double 1: 0001 => 0010 (2)

Division is the reverse - take a digit from the right-hand end (and add a zero to the right-hand end). Note: this may result in rounding down.

Half 4: 0100 => 0010 (2)
Half 5: 0101 => 0010 (2)

Additional resources

The following links points to excellent tutorials on binary numbers.





Comments