Home Contact Shop Notes Questions Programming Links Dictionary Coursework FORUM Tutors
 Recommended Amazon Searches: Computer Science | Computing | Computer Systems | Database | Computing Revision

## Notes > Foundations of Computing > Binary Numbers

### Representing Decimal Numbers in Binary

To convert from decimal to binary, the original number is divided by 2 to get the quotient and remainder. This process is repeated with the quotient being divided by 2 until it is zero. The pattern of zeros and ones in the remainder column is taken to be the binary representation of the number.

For example:

Convert the decimal number 19 to binary

Quotient (div 2)   Remainder (mod 2)
19   9                  1
9    4                  1
4    2                  0
2    1                  0
1    0                  1

This process gives the binary number 10011 (written with the most significant bit to the far left)

### Representing Fractions or Real Numbers in Binary

The decimal number 1.625 can be represented in binary as 1.101. The principal is similar to that of whole decimal numbers being represented in binary. The binary number that is stored consists of a decimal point which remains fixed. The bits to the right of it represent fractions halving in magnitude from left to right (1/2, 1/4, 1/8 etc...). The bits to the left of the decimal point represent whole numbers as normal.

To convert a decimal fraction to binary, the following steps can be carried out:

• The decimal number is multiplied by 2 to give the result X.
• If X is larger or equal to 1, a 1 will be written down.
• If X is smaller than 1, a 0 will be written down.
• If X is larger than 1, the value of 1 is subtracted from X then this is used in the next step of the process.
• Repeat until X is exactly 1.
For example:

Convert 0.6875 to binary

0.6875 x 2 = 1.375     1
0.3750 x 2 = 0.750     0
0.7500 x 2 = 1.500     1
0.5000 x 2 = 1.000     1
This process gives the binary number 0.1011. The zero before the decimal point is assumed because the number is obviously less than 1.

The digits of the two numbers being added are added together in pairs starting from the right hand side with the least significant bit. The following rules apply when adding two binary digits together:
• 0 + 0 = 0
• 0 + 1 = 1
• 1 + 0 = 1
• 1 + 1 = 0 (and carry over 1 to the next column to the left)

For example:

Add the binary numbers 1010 (ten) and 0110 (six) together

1010
0110 +
-----
10000 (sixteen)
An overflow may occur if the sum of the two original numbers exceeds the size available to store the number. The above 4-bit numbers produce a sum which has 5 bits. If the numbers were being stored in 4-bit bytes, an overflow error would occur.

### Subtracting Binary Numbers

To subtract two binary numbers, the following method can be used. Note that the complement of a binary number is the value gained after changing all the 1s to 0s and vice versa.

In the case of A-B:

• Check that A is larger than B
• Take the complement of B
• Add A and the complement of B
• Remove the leading digit of the result
• Add 1 to the result
For example:

Take away the binary number 0110 (six) from 1100 (twelve)

1100 larger then 0110? YES
Complement of 0110: 1001
1100 + 1001 = 10101