Addition of Binary Numbers

Download Report

Transcript Addition of Binary Numbers

NUMBER
SYSTEMS
The BASE of a number system

Determines the number of digits
available

In our number system we use 10 digits:
0-9

The base in our system is 10

It is called decimal or denary
The BASE of a number system
Computers are bi-stable devices
 A bi-stable device has only two possible
states: ON or OFF
 Hence a base 2 number system is
enough for such devices
 A base 2 number system is called
binary
 The 2 possible digits are 0 and 1

The BASE of a number system
 An
other number system used
with computers is Hexadecimal
 The
base for this system is 16
 The
16 possible digits are 0 – 9
and A - F
The BASE of a number system
System
Base
Digits used
Decimal
base 10
e.g. 510
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Binary
base 2
e.g. 102
0, 1
Hexadecimal
base 16 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
e.g. 3C16 A, B, C, D, E, F
Place values
 The
position of each number within
a series of numbers is very
important
 The
position of the number
determines how large it is
 All
number bases have their place
values
Place values
In base 10, the place values are as follows:
106
105
104
103
102
101
100
1 000 000
100 000
10 000
1000
100
10
1
Values go up in powers of 10 as you move
from left to right
Place values
In base 2, the place values are as follows:
29
28
27 26 25 24 23 22 21 20
512 256 128 64 32 16 8 4
2
Values go up in powers of 10 as you move
from left to right.
1
Binary to Decimal Conversion
To convert binary numbers to decimal we
need to follow these four steps:
1.Write
down the place values, starting from the
right hand side
2.Write
each binary digit under its place value
3.Multiply
4.Add
each binary digit by its place value
up the total number
Binary to Decimal Conversion
Example
Convert the binary number 10101 to decimal.
24
23
22
21
20
Step 1 Place
values
16
8
4
2
1
Step 2 Binary
digits
1
0
1
0
1
1 × 16
= 16
0×8
=0
1×4
=4
0×2
=0
1×1
=1
16 +
0+
4+
0+
1=
Step 3
Step 4 Decimal
Answer: 101012 = 2110
21
Decimal to Binary Conversion
To convert a decimal number to
binary
1.Successively
divide the decimal number
by 2 and record the remainder
2.Use
the numerator for the next division
until the result of the division is 0
3.The
remainder (1 or 0) of each division
makes up the binary number
Decimal to Binary Conversion
To convert a decimal number to binary
1.
The remainder of the first division gives the
LEAST significant bit
2.
The remainder of the next division gives the
NEXT bit etc.
3.
This method produces the bits in reverse
order - reading down the way gives the bits in
order of right to left
Decimal to Binary Conversion
Example
Convert 3710 in binary
2 37
2 18 with a remainder of 1
2 9 with a remainder of 0
2 4 with a remainder of 1
2 2 with a remainder of 0
2 1 with a remainder of 0
0 with a remainder of 1
Answer : 3710 = 1001012
Decimal to Binary Conversion
An alternative method is to use
the place values. To convert a
decimal number to binary follow
these steps:
1. Write down the place values up to the
one which is just greater than the
decimal number we need to convert
Decimal to Binary Conversion
Let us consider the decimal number 37,
we would need to write down the place
values up to 64
Place values
26 25 24 23 22 21 20
64 32 16 8 4 2 1
Decimal to Binary Conversion
2. Work through the place values
deciding on whether to place a
1 or a 0 under the place value
Decimal to Binary Conversion
Since 37 is less than 64 we write a 0
under the 64. We would need a 32 so
we write a 1 under the 32
Place values
Binary
number
26 25 24 23 22 21 20
64 32 16 8 4 2 1
0 1
Decimal to Binary Conversion
This would leave us with 37 – 32 = 5. Now we are left with
5 to distribute under the other place values. Since 5 is less
than 16 and 8 we write a 0 under both. We can now place a
1 under 4. This leaves us with 5 – 4 = 1. Hence we put a 0
under 2 and a 1 under 1.
Place values
Binary
number
So 3710 = 1001012
26 25 24 23 22 21 20
64 32 16 8 4 2 1
0 1 0 0 1 0 1
The Hexadecimal (Hex) number
system
 Base
16
 16 symbols: 0 – 9 & A – F
 Place values increase in powers of
16
The advantage of the hexadecimal
system is its usefulness in converting
directly from a 4-bit binary number
Equivalent numbers in different bases
Decimal Binary Hex
0
0000
0
1
0001
1
2
0010
2
3
0011
3
4
0100
4
5
0101
5
6
0110
6
7
0111
7
8
1000
8
9
1001
9
Decimal Binary Hex
10
1010
A
11
1011
B
12
1100
C
13
1101
D
14
1110
E
15
1111
F
Hex to Decimal Conversion
To convert Hex numbers to decimal we
need to follow these four steps:
1.Write
down the place values, starting from the
right hand side
2.Write
each hex digit under its place value
3.Multiply
4.Add
each hex digit by its place value
up the total number
Hex to Decimal Conversion
Example
Convert 3CD16 to its decimal equivalent.
Step 1 Place value
162
161
160
256
16
1
3
C
D
256 × 3
= 768
16 × 12
= 192
1 × 13
= 13
768 +
192 +
13 =
Step 2 Hex
Step 3
Step 4 Decimal
Answer: 3CD16 = 97310
973
Decimal to Hex Conversion
To convert a decimal number to hex
 Successively divide the decimal number
by 16 and record the remainder
 Use the numerator for the next division
until the result of the division is 0
 The remainder of each division makes
up the hex number
Decimal to Hex Conversion
Example 1
Convert 4110 to hex
16
16
41
2 with a remainder of
0 with a remainder of
Answer : 4110 = 2916
9
2
Decimal to Hex Conversion
Example 3
Convert 10910 to hex
16 109
16
6 with a remainder of 13
0 with a remainder of 6
Answer : 10910
= 6 1316
= 6D16
Decimal to Hex Conversion
An alternative method is to use the
place values. To convert a decimal
number to hex follow these steps:
1. Write down the place values up to the one
which is just greater than the decimal
number we need to convert
Decimal to Hex Conversion
Let us consider the decimal number 356, we
would need to write down the place values
up to 4096
Place values
163 162 161 160
4096 256 16 1
Decimal to Hex Conversion
2. Work through the place values
deciding on weather to place a 0
or a value from 1 - 15 under the
place value.
Decimal to Hex Conversion
Since 356 is less than 4096 we write a 0
under the 4096. We would need a 256 so we
write a 1 under the 256.
Place values
Hex number
163 162 161 160
4096 256 16 1
0
1
Decimal to Hex Conversion
This would leave us with 356 – 256 = 100.
Now we are left with 100 to distribute under
the other place values. Since 100 ÷ 16 =
6 r 4 we write a 6 under 16. This leaves us
with 4 ones. Hence we put a 4 under 1.
163 162 161 160
Place values
4096 256 16 1
Hex number
0
1
6
4
So 35610 = 16416
Binary to Hex Conversion
To convert numbers from binary to hex
and vice versa, we need to use the
conversion table shown below
Binary Hex
Binary Hex
Binary Hex
0000
0
0110
6
1011
B
0001
1
0111
7
1100
C
0010
2
1000
8
1101
D
0011
3
1001
9
1110
E
0100
4
1010
A
1111
F
0101
5
Binary to Hex Conversion
For each digit in the hex number, write
down the equivalent 4-bit binary digit.
Example
Convert C316 to its binary equivalent
Hexadecimal
Binary
C
1100
Answer : C316 = 110000112
3
0011
Hex to Binary Conversion
Divide the binary number into 4-bit
groups starting from the right (LSB). If
we end up with a group of less than 4
bits on the left, add 0s to fill up the
required places. Then translate each
group into its equivalent hexadecimal
number below.
Hex to Binary Conversion
Example
Convert 10111010102 to hexadecimal
Binary
Hexadecimal
0010
2
Answer : 10111010102 = 2EA16
1110
E
1010
A
Binary
Arithmetic
Addition of Binary Numbers
13 +
17
30
Note:
3 + 7 = 10 which
we write as 0
carry 1.
01101 +
10001
11110
Similarly in binary:
1 + 1 = 0 carry 1.
Addition of Binary Numbers
13 +
17
1
31
Note:
3 + 7 + 1= 11
which we write
as 1 carry 1.
01101 +
10001
00001
11111
Similarly in binary:
1 + 1 + 1 = 1 carry 1.
Numeric Overflow
13 +
17
11
41
01101 +
10001
01011
101001
Note: overflow occurs if a number too
Numeric
large
to be represented
For
The number
41 cannot is
beencountered.
represented using
example
a 5with
bit register
is used
to store the
5 bits since
5 bits we
can represent
the
decimal
number
41.
range from
0 to 31.
Negative Numbers
Negative numbers can be represented in
binary using one of the following ways:
• Sign and Magnitude Codes (SM)
• Two's Complement (2C)
Sign and Magnitude Codes
• The Most Significant Bit is used to
represent the sign of the number
• The other bits represent the magnitude of
the number.
Example: Sign and Magnitude
 Convert
24 to Sign and Magnitude
representation using an 8-bit register format.
In binary
24
11000
Using 7 bits
24
24
0011000
SM
O since number is positive
00011000
Example: Sign and Magnitude
 Convert
- 24 to Sign and Magnitude
representation using an 8-bit register format.
In binary
24
11000
Using 7 bits
24
24
0011000
SM
1 since number is negative
10011000
Two's Complement (2C)

The bits have the same place values as
binary numbers.
 However the Most Significant Bit is also used
to represent the sign of the number.
Example: Two's Complement

Complement 67 using a 9-bit register
In binary
67
010000011
Reverse Bits
101111100
Add 1
101111101
This gives -67
Range for 2C Representation
1-bit register
1-bit register
3-bit register
2-bit register
3-bit2-bit
register
register
0 00 represents
0 represents
0 0
0 0 1 represents 1
1 represents -1
0 1 0 represents 2
,0
0 1 -1
1 represents
3
1 0 0 represents 0-4
1 0 1 represents 1-3
1 1 0 represents -2
1 1 1 represents -1
-4, -3,-2,-2,-1,-1,0,0,1 1, 2, 3
Range for 2C Representation

In general an N-bit
register can
represent binary
numbers in the range
- 2N-1 ... + 2N-1- 1.
Binary Subtraction
Example: Subtract 54 from 103
103 - 54
Can be written as
103 + (- 54)
Step 1: Check how many bits you need
103
8 bits
(range for +ve numbers is 0 …2n-1)
-54
7 bits (range for 2C numbers is 2n-1 to + 2n-1-1)
Therefore we are going to use 8 bits in both
cases.
Binary Subtraction
Step 2: Convert - 54 to a 2C binary number
-54
1 1 0 0 1 0 1 02
Step 3: Convert 103 to a binary number
103
0 1 1 0 0 1 1 12
Step 4: Add binary 103 to - 54
0 1 1 0 0 1 1 12 +
1 1 0 0 1 0 1 02
Overflow bit 1 00 00 11 11 00 00 00 112
2
The 9th bit or
overflow bit will
be ignored
thus the 8-bit
answer is
correct.
Binary Multiplication
Decimal System
• Shifting the number
left multiplies that
number by 10
420 = 42 x 10.
Binary System
• Shifting the number
left multiplies that
number by 2
111000 = 11100 x 2