Binary arithmetic Week 9 Lesson 1 What do
Binary arithmetic: Week 9 Lesson 1
What do you need to know? Binary arithmetic: • addition • shifts: arithmetic • shifts: logical • overflow.
Addition 1 4+ 5 0001 0 0+ 0101 1 1+ 2 0001 0 0 0 1+ 1 1 + 1 = 2; so write a 0 down and carry 1 over into the next column on the left 0010 3 7+ 10 0011 0 1 1 1+ 1 1010 In column 2, 1 + 1 = 1 lot of 4 plus ; so write a 1 down and carry 1 over into the next column on the left
Shifts: arithmetic Shifts are also known as ‘bitwise operations’ Left-shift 0 0 0 1 1 1 = 0 0 1 1 1 0 0 1 0 1 1 1 Right-shift = 0 0 1 0 1 1 0 (decimal 23) This has the effect of multiplying by 2. (decimal 46) A new 0 is shifted in. (decimal 23) This has the effect of dividing by 2. (decimal 11) The most significant bit (MSB) value is always maintained; in this example a 0 is inserted.
Shifts: logical Shifts are also known as ‘bitwise operations’ Left-shift (decimal 23) This has the effect of multiplying by 2. (decimal 46) A new 0 is shifted in. 0 0 0 1 1 1 (decimal 23) This has the effect of dividing by 2. 0 0 1 0 1 1 (decimal 11) In a logical shift a 0 is always inserted. 0 0 0 1 1 1 = Right-shift 0 0 1 1 1 0 0 0 = So arithmetic and logical shifts appear to have the same effect, however there is a different result with negative numbers (see later).
Overflow 5 15+ 20 0 1 1 1+ 1 1 1 0 0 5 1 0 1 4+ 1 0 0+ 9 1 0 0 1 Solution: use the most significant bit as an overflow flag which is set to 1 when an overflow occurs. Drawback: there are fewer bits available to represent numbers.
- Slides: 6