0% found this document useful (0 votes)
59 views3 pages

Binary Addition and Subtraction

Example 1.1 demonstrates how to add two positive binary numbers (37 and 58) by performing binary addition. This gives the correct sum of 95. Example 1.2 shows an issue that can occur when adding two positive numbers in binary - if the sum is outside the range that can be represented, it will produce an incorrect negative result due to overflow. Example 1.3 explains how to perform binary subtraction by converting the number being subtracted to its two's complement and then adding. This allows 95 - 68 to be correctly calculated as 27.

Uploaded by

Riaz Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
59 views3 pages

Binary Addition and Subtraction

Example 1.1 demonstrates how to add two positive binary numbers (37 and 58) by performing binary addition. This gives the correct sum of 95. Example 1.2 shows an issue that can occur when adding two positive numbers in binary - if the sum is outside the range that can be represented, it will produce an incorrect negative result due to overflow. Example 1.3 explains how to perform binary subtraction by converting the number being subtracted to its two's complement and then adding. This allows 95 - 68 to be correctly calculated as 27.

Uploaded by

Riaz Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Binary addition

Consider Examples 1.1 and 1.2.

Example 1.1 Add 0 0 1 0 0 1 0 1 (37 in denary) and 0 0 1 1 1 0 1 0 (58 in denary).

Solution
−128 64 32 16 8 4 2 1
0 0 1 0 0 1 0 1
+
0 0 1 1 1 0 1 0
=
0 1 0 1 1 1 1 1

This gives us 0 1 0 1 1 1 1 1, which is 95 in denary; the correct answer.

Example 1.2 Add 0 1 0 1 0 0 1 0 (82 in denary) and 0 1 0 0 0 1 0 1 (69 in denary).

Solution
−128 64 32 16 8 4 2 1
0 1 0 1 0 0 1 0
+
0 1 0 0 0 1 0 1
=
1 0 0 1 0 1 1 1

This gives us 1 0 0 1 0 1 1 1, which is –105 in denary (which is clearly nonsense).


When adding two positive numbers, the result should always be positive
(likewise, when adding two negative numbers, the result should always be
negative). Here, the addition of two positive numbers has resulted in a negative
answer. This is due to the result of the addition producing a number which is
outside the range of values which can be represented by the 8 bits being used (in
this case +127 is the largest value which can be represented, and the calculation
produces the value 151, which is larger than 127 and, therefore, out of range).
This causes overf low; it is considered in more detail in Chapter 13.

457591_01_CI_AS & A_Level_CS_001-026.indd 4 25/04/19 9:11 AM


Binary subtraction
To carry out subtraction in binary, we convert the number being subtracted into
its negative equivalent using two’s complement, and then add the two numbers.

Example 1.3 Carry out the subtraction 95 – 68 in binary.

Solution
1 Convert the two numbers into binary:
95 = 0 1 0 1 1 1 1 1
68 = 0 1 0 0 0 1 0 0
2 Find the two’s complement of 68:

invert the digits: 1 0 1 1 1 0 1 1


add 1: 1
which gives: 1 0 1 1 1 1 0 0 = −68
3 Add 95 and −68:

−128 64 32 16 8 4 2 1
0 1 0 1 1 1 1 1
+
1 0 1 1 1 1 0 0
=
1 0 0 0 1 1 0 1 1
The additional ninth bit is simply ignored leaving the binary number
0 0 0 1 1 0 1 1 (denary equivalent of 27, which is the correct result of the
subtraction).

Example 1.4 Carry out the subtraction 49 – 80 in binary.

Solution
1 Convert the two numbers into binary:
49 = 0 0 1 1 0 0 0 1
80 = 0 1 0 1 0 0 0 0
2 Find the two’s complement of 80:

invert the digits: 1 0 1 0 1 1 1 1


add 1: 1
which gives: 1 0 1 1 0 0 0 0 = −80
3 Add 49 and −80:

−128 64 32 16 8 4 2 1
0 0 1 1 0 0 0 1
+
1 0 1 1 0 0 0 0
=
1 1 1 0 0 0 0 1
5
This gives us 1 1 1 0 0 0 0 1, which is −31 in denary; the correct answer.

457591_01_CI_AS & A_Level_CS_001-026.indd 5 25/04/19 9:11 AM


457591_01_CI_AS & A_Level_CS_001-026.indd 6 25/04/19 9:11 AM

You might also like