Professional Documents
Culture Documents
Agenda
Intro to number representatio
Conversion between different bases (unsigned numbers
Binary additio
Signed numbers (signed magnitude vs. 2’s complement
Binary addition - Over o
Binary multiplicatio
2. Decimal Expansio
We would work it the other way around by dividing by 10 and considering the remainders
1
l
fl
w
- Preferred way of conversion from decimal: (If you’re familiar with the powers of 2, a
reasonable way to convert from decimal to binary is to subtract powers of 2 until you get 0.
54: 32 is biggest, 22 left; 16 next, 6 left; 4, 2, done. 54= 32+16+4+2 => 110110
81: 64 + 16 + 1. 1010001
- The other way: Least signi cant bit to Most signi cant bit (mod by 2, which is the same as
considering the remainder after dividing by 2. Repeat till the quotient is 0
54: 0 => 27, 1 => 13, 1 => 6, 0 => 3, 1 => 1, 1 => 0 (110110
8
71 = 64+4+2+
(1000111
2
0
1
1
0
1
)
0
1
0
1
1
1
fi
)
fi
3. Division Algorith
Division Algorithm Theorem: If b and n are integers with b > 0, there are unique integers q
and r such that n = qb + r and 0 <= r < b
Pf: Every number clearly falls on the number line within exactly one interval [qb, (q+1)b) for
some q. [intervals b apart] The difference n - qb is smaller than b but not smaller than 0, so it
satis es the requirements for r
n = ak * b k + ak−1 * b k−1 + … + a1 * b 1 + a0 b 0
=> In other words, the formula works for all integer bases >= 2
Pf. (Positive numbers) By the Division algorithm, we can divide by b and get a unique
remainder for the last digit. The rest of the number is qb, which can be represented by shifting
the representation for q one place to the left. We could keep doing this until there are no digits
left to represent. Since this can produce only one number, it must be unique
(Negative numbers) The division algorithm on the absolute value can produce the correct digits,
then add a - to make the number negative again
• 28= 256. A chunk of 8 bits is a byte, and 256 is the number of values a byte can take on
(0-255)
• 24 = 16. This is the number of values possible in half a byte, or a nibble (0-15), which will be
useful for our study of hex
• 210 = 1024. Kilobytes (~1000 bytes) don’t come up much anymore, but this is still a handy way
of estimating how big binary values are
230 = 210 * 210 * 210 = about 1,000,000,00
3
fi
.
5. Binary additio
The CPU does lots of things. How could it, for instance, add numbers with logic gates
• Many things are represented in binary in computers, and T or F can be mapped to 1 and 0
• Consider a piece of the machinery that handles just two input bits in the addition
• It needs to take two bits as input and have the behavior at right for the sum and the carr
Binary addition is similar to decimal addition (align the ones and tens
0101 (5) + 0111 (7) = 1100 (12
0011 (3) + 0011 (3) = 110 (6
1010 + 0101 = 111
00001111+00000001=0001000
Process:(a)convert the two decimal numbers to binary, (b) add the two binary numbers,
and (c) convert the result back to decimal to check the answer
* Construction of half-adder from truth table: XOR for sum bit, AND for carr
4
n
What happens when we try to carry out and don’t have the bits? If everything represents a
positive number, over ow
6. Binary multiplicatio
Perform the following multiplications in binary. (1) convert each decimal number to binary, (2)
perform the multiplication in binary, and (3) convert the binary result back to decimal to check
your result
41×
101001
× 111
10100
101001
101001
100011111
Check Solution: 100011111 256+16+8+4+2+1 28
5
7
fl
The number may be preceded by 0x to signal that it is hex and not binary or decimal:
0x20 = 2016 = 3210.
6
0
1
2
3
4
5
6
7
8
9
1
0
2
3
4
5
.
)
1
1
0
1
0
1
0
1
0
1
0
0
1
0
1
0
y
0
)
0
The convenient thing for CS is that we can very easily convert between binary and hex
• Every 4 bits can be directly read as a hex digit, and this will be the correct hexadecimal
number
• For example, 010100102 can be read as
0101 = 5 and 0010 = 2 so the hex should be 0x52
Check: 01010010 = 64 + 16 + 2 = 8210.
0x52 = 5*16+2 = 8210
• 111111002 can be read as
1111 1100 = (15,12) = 0xFC
Check: 111111002 = 128 + 64 + 32 + 16 + 8 + 4 = 2521
0xFC = 15*16+12=25210
7
=
=
0
+
+
:
s
.
Base Base
0
0
0
1
1
1
2
2
2
8
1
0
1
2
0
2
0
1
2
fi
2
3
1
:
0
42 = 32 + 8 +2 0010101
-42 = 1010101
79 = 64 + 8 + 4 + 2 + 1 = 0100111
-79 = 1100111
0 = 00000000 = 1000000
9
)
?
=
0
2. Two’s Complement
Positive Numbers: The standard binary representation of the number
Negative numbers: get the binary representation of the positive number, ip the bits, and add
1
Examples:
42 = 32 + 8 +2 = 0010 1010
Binary of Positive Value Flip the bits Two’s Complemen
(Add 1 after ipping the
bits when number is
negative)
42 00101010 00101010
-42 00101010 11010101 11010110
-79 01001111 10110000 10110001
-88 01011000 10100111 10101000
To convert back, ip all the bits and add 1, remember it's negative
=>Exercise:
Give the 8-bit two’s complement representations of the following integers: 55, 83, −79, −88
55 = 32+16+4+2+1 = 00110111
83 = 64+16+2+1 = 0101001
• 8-bit two’s complement: -128 <-> 127 (smallest 1000 000 largest 0111 1111
If we list out all the positive/negative two’s complement numbers in order, you can see
how the pattern starts to emerge:
10
.
fl
fl
t
0
.
-
.
fl
)
01111111 127
0111111 126
0111110 125
...
00000010 2
00000001 1
00000000 0
11111111 -1
11111110 -2
11111101 -3
11111100 -4
11111011 -5
...
10000011 -125
10000010 -126
10000001 -127
10000000 -128
Consider +4 and –4 (using a two's 8-bit representation); that is, 0000 0100 and 1111 1100
The sum of any positive number and the negative of the same number should be 0. That is, 4 +
(-4) =
0000 010
+ 1111 110
(1) 0000 0000
The sum here is zero within the constraints of the number of bits used (8 bits). The (1), which is
the last carry is discarded
11
fl
0
1
0
:
0000000
4 + (the complement of 4) + 1 =
4 + [(the complement of 4) + 1] =
4 + [-4] =
Therefore, -4 = the complement of 4 +
You can try it with any other number, it always works :
=>Exercise:
-27 <=> 1110010
-35 <=> 1101110
-71 <=> 1011100
-98 <=> 1001111
-103 <=> 1001100
79 = 0100111
-42 = 11010110
37 00100101 throw away the carry
Let’s perform the following addition in binary (assume n=4). Detect whether there is
over ow.
12
+
fl
0
1
1
0
1
fl
1
fl
1
What about
79 = 0100111
+79 = 01001111
10011110 = -(01100001 + 1)
= -01100010 = -(64 + 32 + 2)
= -98 = 256 - 158 OVERFLO
Detecting Over o
3- Adding a negative number and a positive number (Start with one leading 0 and one leading 1)
NO OVERFLO
13
1
fl
w
fl
fl
W
Note: Another easy way to check for an over ow is by comparing the last 2 carries , if they are
different then there is an over ow. In other words, the carry into 1MSB != carry out of MSB
14
fi
fl
fl
fl
If the 2 numbers you are adding are positive then the most signi cant bit (MSB) for both is 0. If
the carry in is 1, then the sum will be 1 which implies that the sum is a negative and the carry out
will be 0. Here, the carry in and carry out are different
Same thing can be observed if the numbers you are adding up are negative . In this case the the
MSB for both is a 1. If the carry in is 0, then 0+1+1= 0 with the carry out 1. This will imply that
the sum is a positive number which is not possible when you add up 2 negative numbers. Here
again, the carry in and carry out are different
Conclusion: Now that we've seen Two's complement: say you have a problem with a
program you just wrote. You are looping using a counter that is counting down from 40
to 0 and then stopping. However, the program keeps crashing. When you look at the
variable in the debugger the value is 0xFFFFFFFF. What is going on? Why is it
important to understand Two's Complement in this case?
15
.
fi