Binary Review Lecture 2 Section 2.4 Robb T. Koether Hampden-Sydney College Wed, Aug 28, 2019 Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 1 / 40
Binary ReviewLecture 2
Section 2.4
Robb T. Koether
Hampden-Sydney College
Wed, Aug 28, 2019
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 1 / 40
1 Binary ReviewBinary NumbersSigned IntegersBinary Arithmetic
2 Detecting OverflowUnsigned Addition OverflowUnsigned Subtraction OverflowSigned Addition OverflowSigned Subtraction Overflow
3 Assignment
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 2 / 40
Outline
1 Binary ReviewBinary NumbersSigned IntegersBinary Arithmetic
2 Detecting OverflowUnsigned Addition OverflowUnsigned Subtraction OverflowSigned Addition OverflowSigned Subtraction Overflow
3 Assignment
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 3 / 40
Outline
1 Binary ReviewBinary NumbersSigned IntegersBinary Arithmetic
2 Detecting OverflowUnsigned Addition OverflowUnsigned Subtraction OverflowSigned Addition OverflowSigned Subtraction Overflow
3 Assignment
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 4 / 40
Binary review
When studying the processor at the hardware level we need todeal with binary numbers.Binary has two symbols: 0 and 1.A binary symbol is called a bit.
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 5 / 40
Binary review
Bits are grouped to store more information.1 nibble = 4 bits.1 byte = 8 bits.1 halfword = 2 bytes = 16 bits.1 word = 4 bytes = 32 bits.
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 6 / 40
Binary review
It is confusing to look at strings of 0s and 1s.1111111111111111111111111111111111111110000011111111000001111111111111100000111111110000011111111111111000001111111100000111111111111111111111111111111111111111111111111111111111111111111111111111000111111111111111111000111111110000000111111111100000001111111111100000000000000000011111111111111111100000000001111111111111111111111111111111111111111111
The problem is too many of the same symbol.
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 7 / 40
Binary review
Let one symbol represent a group of bits.1-bit can represent 2 different things.2-bits can represent 4 different things.3-bits can represent 8 different things.4-bits can represent 16 different things.
In general, n-bits can represent 2n different things.
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 8 / 40
Binary review
Four bits (half a byte) represent sixteen things.Hexadecimal consists of 16 different symbols.0, . . . , 9, A, B, C, D, E, F.
Binary Hex Symbol Binary Hex Symbol0000 0 1000 80001 1 1001 90010 2 1010 A0011 3 1011 B0100 4 1100 C0101 5 1101 D0110 6 1110 E0111 7 1111 F
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 9 / 40
Binary review
To convert from binary to hexadecimalGroup bits in fours from the right end of the string. (Add leading 0sif necessary.)Substitute the corresponding hexadecimal digit.
11111010110011102 = 11112 | 10102 | 11002 | 11102
= F | A | C | E= FACE16.
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 10 / 40
Binary review
To convert from hexadecimal to binary, reverse the process.
FACE16 = F | A | C | E= 11112 | 10102 | 11002 | 11102
= 11111010110011102
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 11 / 40
Outline
1 Binary ReviewBinary NumbersSigned IntegersBinary Arithmetic
2 Detecting OverflowUnsigned Addition OverflowUnsigned Subtraction OverflowSigned Addition OverflowSigned Subtraction Overflow
3 Assignment
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 12 / 40
Storing Negative Integers
To store negative integers, we divide the range 0 to 232− 1 in half.The lower half 0 to 231 − 1 (hex 00000000 to hex 7FFFFFFF)represents the positive integers 0 to 231 − 1.The upper half 231 to 232 − 1 (hex 80000000 to hex FFFFFFFF)represents the negative integers.
Negative integers are interpreted in two’s complement notation.
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 13 / 40
3-Bit Signed and Unsigned Integers
Unsigned000 0001 1010 2011 3100 4101 5110 6111 7
Signed000 0001 1010 2011 3100 −4101 −3110 −2111 −1
3-bit integers, unsigned and signed
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 14 / 40
3-Bit Unsigned Integers
Unsigned00000000000000000000000000000000 000000000000000000000000000000001 100000000000000000000000000000010 2
: :01111111111111111111111111111111 231 − 110000000000000000000000000000000 231
10000000000000000000000000000001 231 + 110000000000000000000000000000010 231 + 2
: :11111111111111111111111111111111 232 − 1
32-bit unsigned integers
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 15 / 40
3-Bit Signed Integers
Signed00000000000000000000000000000000 000000000000000000000000000000001 100000000000000000000000000000010 2
: :01111111111111111111111111111111 231 − 110000000000000000000000000000000 −231
10000000000000000000000000000001 −231 + 110000000000000000000000000000010 −231 + 2
: :11111111111111111111111111111111 −1
32-bit signed integers
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 16 / 40
3-Bit Signed and Unsigned Integers
Unsigned
00000000000000000000000000000000 000000000000000000000000000000001 100000000000000000000000000000010 2
: :01111111111111111111111111111111 231 − 110000000000000000000000000000000 231
10000000000000000000000000000001 231 + 110000000000000000000000000000010 231 + 2
: :11111111111111111111111111111111 232 − 1
Signed
00000000000000000000000000000000 000000000000000000000000000000001 100000000000000000000000000000010 2
: :01111111111111111111111111111111 231 − 110000000000000000000000000000000 −231
10000000000000000000000000000001 −231 + 110000000000000000000000000000010 −231 + 2
: :11111111111111111111111111111111 −1
32-bit integers, unsigned and signed
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 17 / 40
Two’s Complement
Negative integers are interpreted in two’s complement notation.The 1 in bit 31 (MSB) indicates a negative integer.
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 18 / 40
3-Bit Signed and Unsigned Integers
Unsigned000 0001 1010 2011 3100 4101 5110 6111 7
Signed000 0001 1010 2011 3100 −4101 −3110 −2111 −1
3-bit integers, unsigned and signed
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 19 / 40
Two’s Complement Example
Example (Two’s Complement Example)The absolute value of the negative integer (i.e., the correspondingpositive integer) is obtained by reversing all the bits and thenadding 1.
What negative number is represented by1111 1111 1111 1111 1111 0000 1100 1110?
Reverse all the bits:0000 0000 0000 0000 0000 1111 0011 0001
Add 1:0000 0000 0000 0000 0000 1111 0011 0010
The value is −3890.
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 20 / 40
Two’s Complement Example
Example (Two’s Complement Example)The absolute value of the negative integer (i.e., the correspondingpositive integer) is obtained by reversing all the bits and thenadding 1.What negative number is represented by
1111 1111 1111 1111 1111 0000 1100 1110?
Reverse all the bits:0000 0000 0000 0000 0000 1111 0011 0001
Add 1:0000 0000 0000 0000 0000 1111 0011 0010
The value is −3890.
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 20 / 40
Two’s Complement Example
Example (Two’s Complement Example)The absolute value of the negative integer (i.e., the correspondingpositive integer) is obtained by reversing all the bits and thenadding 1.What negative number is represented by
1111 1111 1111 1111 1111 0000 1100 1110?Reverse all the bits:
0000 0000 0000 0000 0000 1111 0011 0001
Add 1:0000 0000 0000 0000 0000 1111 0011 0010
The value is −3890.
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 20 / 40
Two’s Complement Example
Example (Two’s Complement Example)The absolute value of the negative integer (i.e., the correspondingpositive integer) is obtained by reversing all the bits and thenadding 1.What negative number is represented by
1111 1111 1111 1111 1111 0000 1100 1110?Reverse all the bits:
0000 0000 0000 0000 0000 1111 0011 0001
Add 1:0000 0000 0000 0000 0000 1111 0011 0010
The value is −3890.
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 20 / 40
Two’s Complement Example
Example (Two’s Complement Example)The absolute value of the negative integer (i.e., the correspondingpositive integer) is obtained by reversing all the bits and thenadding 1.What negative number is represented by
1111 1111 1111 1111 1111 0000 1100 1110?Reverse all the bits:
0000 0000 0000 0000 0000 1111 0011 0001
Add 1:0000 0000 0000 0000 0000 1111 0011 0010
The value is −3890.
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 20 / 40
Outline
1 Binary ReviewBinary NumbersSigned IntegersBinary Arithmetic
2 Detecting OverflowUnsigned Addition OverflowUnsigned Subtraction OverflowSigned Addition OverflowSigned Subtraction Overflow
3 Assignment
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 21 / 40
Binary Arithmetic
Perform the following additions as 8-bit unsigned integers;00001011+ 0000011011110001+ 0000010111110001+ 11111101
In which case(s) was there overflow?How is overflow detected?
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 22 / 40
Binary Arithmetic
Perform the following additions as 8-bit signed integers;00001011+ 0000011001111011+ 0111011011110001+ 0000010111110001+ 1111110110000111+ 10001101
In which case(s) was there overflow?How is overflow detected?
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 23 / 40
Binary Arithmetic
Subtraction is performed by adding the two’s complement of thesubtrahend.Perform the following subtractions as 8-bit unsigned integers;
00001111− 0000011000000011− 00000110
In which case(s) was there overflow?How is overflow detected?
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 24 / 40
Binary Arithmetic
Perform the following subtractions as 8-bit signed integers;00001111− 0000011000000011− 0000011001111100− 1111011010000101− 00001110
In which case(s) was there overflow?How is overflow detected?
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 25 / 40
Outline
1 Binary ReviewBinary NumbersSigned IntegersBinary Arithmetic
2 Detecting OverflowUnsigned Addition OverflowUnsigned Subtraction OverflowSigned Addition OverflowSigned Subtraction Overflow
3 Assignment
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 26 / 40
Outline
1 Binary ReviewBinary NumbersSigned IntegersBinary Arithmetic
2 Detecting OverflowUnsigned Addition OverflowUnsigned Subtraction OverflowSigned Addition OverflowSigned Subtraction Overflow
3 Assignment
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 27 / 40
Unsigned Addition Overflow
x y x + yMSB MSB MSB Overflow?
0 0 0 No0 0 1 No0 1 0 Yes0 1 1 No1 0 0 Yes1 0 1 No1 1 0 Yes1 1 1 Yes
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 28 / 40
Unsigned Addition Overflow
x y x + yMSB MSB MSB Overflow?
0 0 0 No0 0 1 No0 1 0 Yes0 1 1 No1 0 0 Yes1 0 1 No1 1 0 Yes1 1 1 Yes
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 29 / 40
Outline
1 Binary ReviewBinary NumbersSigned IntegersBinary Arithmetic
2 Detecting OverflowUnsigned Addition OverflowUnsigned Subtraction OverflowSigned Addition OverflowSigned Subtraction Overflow
3 Assignment
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 30 / 40
Unsigned Subtraction Overflow
x y x − yMSB MSB MSB Overflow?
0 0 0 No0 0 1 Yes0 1 0 Yes0 1 1 Yes1 0 0 No1 0 1 No1 1 0 No1 1 1 Yes
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 31 / 40
Unsigned Subtraction Overflow
x y x − yMSB MSB MSB Overflow?
0 0 0 No0 0 1 Yes0 1 0 Yes0 1 1 Yes1 0 0 No1 0 1 No1 1 0 No1 1 1 Yes
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 32 / 40
Outline
1 Binary ReviewBinary NumbersSigned IntegersBinary Arithmetic
2 Detecting OverflowUnsigned Addition OverflowUnsigned Subtraction OverflowSigned Addition OverflowSigned Subtraction Overflow
3 Assignment
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 33 / 40
Signed Addition Overflow
x y x + yMSB MSB MSB Overflow?
0 0 0 No0 0 1 Yes0 1 0 No0 1 1 No1 0 0 No1 0 1 No1 1 0 Yes1 1 1 No
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 34 / 40
Signed Addition Overflow
x y x + yMSB MSB MSB Overflow?
0 0 0 No0 0 1 Yes0 1 0 No0 1 1 No1 0 0 No1 0 1 No1 1 0 Yes1 1 1 No
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 35 / 40
Outline
1 Binary ReviewBinary NumbersSigned IntegersBinary Arithmetic
2 Detecting OverflowUnsigned Addition OverflowUnsigned Subtraction OverflowSigned Addition OverflowSigned Subtraction Overflow
3 Assignment
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 36 / 40
Signed Subtraction Overflow
x y x − yMSB MSB MSB Overflow?
0 0 0 No0 0 1 No0 1 0 No0 1 1 Yes1 0 0 Yes1 0 1 No1 1 0 No1 1 1 No
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 37 / 40
Signed Subtraction Overflow
x y x − yMSB MSB MSB Overflow?
0 0 0 No0 0 1 No0 1 0 No0 1 1 Yes1 0 0 Yes1 0 1 No1 1 0 No1 1 1 No
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 38 / 40
Outline
1 Binary ReviewBinary NumbersSigned IntegersBinary Arithmetic
2 Detecting OverflowUnsigned Addition OverflowUnsigned Subtraction OverflowSigned Addition OverflowSigned Subtraction Overflow
3 Assignment
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 39 / 40
Assignment
AssignmentRead Section 2.4.
Robb T. Koether (Hampden-Sydney College) Binary Review Wed, Aug 28, 2019 40 / 40