Lesson 2. Binary Arithmetic
Lesson Objective
- Be able to explain highest and lowest assignable Number.
- Be able to add together up to three binary numbers.
- Apply binary shifts to multiply and divide numbers.
- Explain underflow and overflow and describe the circumstances in which they occur.
Lesson Notes
Binary Addition - THE RULES!!
Work right to left and apply these simple rules:
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 0 = 1
- 1 + 1 = 0 Carry 1
- 1 + 1 + 1 = 1 Carry 1
Here are examples of two 8 bit numbers being added together:
|
|
|
|
1 |
1 |
1 |
|
|
|
|
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
14 |
+ |
1 |
0 |
1 |
0 |
0 |
0 |
1 |
0 |
162 |
|
1 |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
176 |
...
|
1 |
|
|
|
1 |
1 |
1 |
|
|
|
0 |
1 |
0 |
0 |
0 |
1 |
1 |
1 |
71 |
+ |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
1 |
79 |
|
1 |
0 |
1 |
0 |
1 |
0 |
0 |
0 |
150 |
Overflow Error
When and extra bit is created to represent a number.
Here is an example of an overflow error:
1 |
|
|
1 |
1 |
1 |
1 |
|
|
|
|
1 |
1 |
0 |
0 |
1 |
1 |
0 |
0 |
204 |
+ |
1 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
157 |
1 |
0 |
1 |
1 |
0 |
1 |
0 |
0 |
1 |
361 |
Adding 3 Binary Numbers
Method 1.
Add the first two, then add the third to the result.
We will carry out the addition 1011 + 0111 + 101
We can can see the 1011 (11) + 0111 (7) + 101 (5) = 23
|
1 |
1 |
1 |
1 |
|
|
|
|
|
1 |
0 |
1 |
1 |
11 |
|
|
+ |
0 |
1 |
1 |
1 |
7 |
|
|
1 |
0 |
0 |
1 |
0 |
|
|
|
+ |
|
1 |
0 |
1 |
5 |
|
|
1 |
0 |
1 |
1 |
1 |
23 |
|
Method 2.
Logical Binary Shifts
Left Shift = Multiply. Each shift is the number multiplied by a power of 2
0 Shift |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
Original |
1 Shift |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
*2 |
2 Shift |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
*4 |
3 Shift |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
0 |
*8 |
Right Shift = Divide. Each shift is the number division by a power of 2
0 Shift |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
Original |
1 Shift |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
/2 |
2 Shift |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
/4 |
3 Shift |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
/8 |
Binary Multiplication
How do I multiply with number that aren't 2, 4, 8, 16, 32, 64, 128, 256...?
|
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|
|
0 |
0 |
0 |
1 |
1 |
0 |
0 |
1 |
25 |
x |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
10 |
|
1 |
1 |
0 |
0 |
1 |
0 |
0 |
0 |
200 |
+ |
0 |
0 |
1 |
1 |
0 |
0 |
1 |
0 |
50 |
|
1 |
1 |
1 |
1 |
1 |
0 |
1 |
0 |
250 |
Take the first number as the multipler. Multiply the multiplier by each digit of the multiplicand to achieve intermediate products, whose last digit is in the position of the corresponding multiplicand digit. Then add the intermediate values.
Example:
First x8, Left Shift 25 by 3 (11001000)
Then x2, Left Shift 25 by 1 (00110010)
Final add them together (11001000 + 00110010 = 11111010)
Underflow and Overflow
Overflow occurs when the result of a calculation is too large to be held in the number of bits allocated.
For example, adding two integers in an 8-bit byte (ignore the sign bit).
1 |
|
|
|
|
|
1 |
1 |
|
|
|
1 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
129 |
+ |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
131 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
260 |
Underflow occurs when a number is too small to be represented in the number of bits allocated.
It may occur if a very small number is divided by a number greater than 1.
Example: Shows a 8 bit binary number shifting one space to the right.
|
1 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
|
รท2 |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |