Unit 2 - Binary Numbers and Arithmetic
Unit 2 - Binary Numbers and Arithmetic
102
..
101
100
10-1
10-2
..
10-n
Decimal point
For example:
3410 = 30 + 4
3
4
4 * 100 = 4
3 * 101= 30
Sum=3410
As can be seen, the number 3 has a weighting of 10 (101) due to its position and the number 4
has a weighting of 1 (100) due to its position
For example:
123.4510 = 100 + 20 + 3 + (4/10) + (5/100)
= (1 * 102) + (2 * 101) + (3 * 100) + (4 * 10-1) + (5 * 10-2)
Note, a number with a subscript of 10 (i.e. Base 10) indicates that this is a decimal number.
However, decimal is the default number system, so usually the subscript is not included for
decimal numbers.
DECIMAL
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
BINARY
02
12
102
112
1002
1012
1102
1112
10002
10012
10102
10112
11002
11012
11102
11112
As can be seen from the table above, it takes four bits to count sixteen decimal numbers. A
simple formula is used to calculate how far it is possible to count with n bits, i.e. 2n-1, where
n is the number of bits available.
For Example:
n =4
24-1 = 16 - 1
= 15
Therefore, it is possible to count to the decimal number fifteen with four binary digits. To
count to sixteen a fifth binary digit (bit) is needed.
Computers use binary numbers to select memory locations. Each location is assigned a
unique number (called an address). The Pentium Microprocessor has 32 address lines, which
means it has
232 = 4,294,967,296
unique locations.
2
[910]
[5910]
..
22
21
20
2-1
2-2
..
2-n
Binary point
All bits to the left of the binary point have weights that are positive powers of 2 and all to the
right have weights that are negative powers of two, i.e. fractional weights:
2-1
2-2
2-3
2-4
= 1/21
= 1/2
2
= 1/2
= 1/4
= 1/23
= 1/8
= 1/24
= 1/16
:
The same process is used when converting decimal fractions.
Ex 3: Convert the binary number 0.11012 to a decimal number.
[0.812510]
[25.37510]
Dividing 1310 by the total number of combinations, we obtain 13/16=0.8125 which is the
same as in the last calculation.
0.11012 = 0.812510
[11110112]
[10011112]
[1011102]
The procedure is a little more complicated when we have a decimal number that has digits
attached to the right of the decimal point. The successive division method is used for those
digits to the left of the decimal point, as before. However the digits to the right of the
decimal point undergo a different transformation. Instead of being divided continuously by
two, they are multiplied continuously by two. This happens until there are no numbers to the
right of the decimal point. The binary number is read from the first carry to the last carry.
Ex 8: Convert the decimal number 0.75 to binary.
[0.1102]
If in doubt, you can verify your answer easily by doing a binary to decimal conversion.
ForEx 8,
0.112 1 1 1 1
2
4
0.7510
Ex 9: Convert the decimal number 0.875 to binary.
Ex 10: Convert the decimal number 27.125 to binary.
Ex 11: Convert the decimal number 32.48 to binary (to 5 places).
[0.11102]
[11011.0012]
[100000.011112]
Binary Arithmetic
Binary Addition
There are four basic rules for adding binary digits
02 + 02 = 02
(Sum of 0 with a carry of 0)
12 + 02 = 12
(Sum of 1 with a carry of 0)
02 + 12 = 12
(Sum of 1 with a carry of 0)
12 + 12 = 102
(Sum of 0 with a carry of 1)
Some examples:
12 + 02 + 02 = 012
12 + 12 + 02 = 102
12 + 02 + 12 = 102
12 + 12 + 12 = 112
[10002 (8)]
[11012 (13)]
[101012 (21)]
[1011112 (47)]
[1.001012 (1.15625)]
Negative numbers
Consideration is now given to how negative numbers are represented. A signed binary
number consists of both sign and magnitude information. The sign indicates whether the
number is positive or negative and the magnitude is the value of the number. There are three
ways in which signed whole numbers can be expressed in binary form:
Sign-Magnitude
The left-most bit is the sign bit, and the remaining bits are magnitude bits. In the sign
magnitude system, the negative number has the same magnitude as the corresponding
positive number but the sign bit is a 1 rather than a 0.
Example:
Sign Bit
Magnitude Bits
00011001 +25
10011001
Sign Bit
-25
Magnitude Bits
In this case, there are 7 magnitude bits, so the largest magnitude is 2 7-1=127. Therefore, this
number system ranges from 127 to +127.
1s Complement System
Positive numbers here are the same as in the sign-magnitude system. However, a negative
number is the 1s complement of the corresponding positive number. The 1s complement of
binary numbers is found by changing all 1s to 0s and all 0s to 1s. Again the most significant
bit (MSB) indicates whether the number is positive or negative.
Example:
00011001
11100110
+25
-25
2s Complement System
Positive numbers here are again the same as for the 1s Complement and Sign-Magnitude
systems. However, a negative number is the 2s complement of the corresponding positive
number. The 2s complement of a binary number is found by adding 1 to the least significant
bit (LSB) of the 1s complement. Again the most significant bit (MSB) indicates whether the
number is positive or negative.
Example:
00011001
11100111
+25
-25
For 2s complement signed numbers, the range of values for n-bit numbers is
-(2n-1) to (2n-1-1)
where in each case there is one sign bit and (n-1) magnitude bits.
So for this example, n=8, and numbers can be represented in 2s Complement from 128 to
+127.
In summary:
1011 0110 10002
0100 1001 01112
+12
0100 1001 10002
Binary Number
1s Complement
Add 1
2s Complement
An alternative method useful for finding the 2s Complement of long binary numbers is to
start at the right, with the LSB, and write the bits as they are up to and including the first 1.
Then take the 1s complement of the remaining bits.
101101101000
These bits remain as
they are
010010011000
1s complement of the
original bits
Ex 17 : For each binary number in a 4 bit system, (n=4) find the decimal representation in
sign magnitude, ones complement and twos complement.
4 BIT
BINARY
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
SIGNMAGNITUDE
1S
COMPLEMENT
2S
COMPLEMENT
When dealing with negative binary numbers, it is important to know where the sign bit is.
Describing the number system used as an n-bit binary number system specifies this. This
indicates that the nth bit is the sign bit.
When two numbers in twos complement format are added, the result will be in twos
complement format. Hence, computers use the 2s complement to represent negative integer
numbers in all arithmetic operations.
7
Ex 18: Add the 8-bit 2s comp. binary numbers 00000111 and 00000100. Verify your answer
by converting the numbers being added and the result to decimal. [000010112 (11)]
Ex 19: Add the 8-bit 2s comp. binary numbers 00001111 and 11111010. Verify your answer
by converting the numbers being added and the result to decimal. [000010012 (9)]
Ex 20: Add the 8-bit 2s comp. binary numbers 00010000 and 11101000. Verify your answer
by converting the numbers being added and the result to decimal. [111110002 (-8)]
Ex 21: Add the 8-bit 2s comp. binary numbers 11111011 and 11110111. Verify your answer
by converting the numbers being added and the result to decimal. [111100102 (-14)]
Binary Subtraction
Subtraction is a special case of addition. For example, subtracting +6 from +9 is the same as
adding 6 to +9. Subtraction of two numbers is the same as taking the 2s complement of the
number you are subtracting and adding the two numbers, while discarding any final carry
bit.
Ex 22: Evaluate 000011102-000010012. Verify your answer by converting the numbers being
subtracted and the result to decimal.
[000001012 (5)]
Ex 23: Evaluate 000010012-000011102. Verify your answer by converting the numbers being
subtracted and the result to decimal.
[-000001012 (-5)]
When two numbers are added and the number of bits required to represent the sum exceeds
the number of bits in the two numbers, an overflow condition results and is indicated by an
incorrect sign bit.
Ex 24: Add the 8-bit binary numbers 01111101 and 00111010. Determine whether or not
your answer is correct by converting the numbers being added and the result to decimal.
What is the answer if a 12-bit binary system is used?
[101101112 (183)]
Binary Multiplication
Binary Multiplication is analogous to decimal multiplication. Negative binary numbers must
be changed to uncomplemented form before binary multiplication.
Ex 25: Multiply 101012 by 1102. Verify your answer by converting the numbers being
multiplied and the result to decimal.
[11111102 (126)]
Binary Division
Binary Division is analogous to decimal division. Negative binary numbers must be changed
to uncomplemented form before binary division.
Ex 26: Divide 110112 by 1102. Verify your answer by converting the numbers being divided
and the result to decimal.
[100.102 (4.5)]