Information Technology Topic
Signed Integers and Two's Complement
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- A signed integer reserves the leftmost bit as the sign bit: 0 means positive and 1 means negative; in sign and magnitude the other bits hold the size of the number
- In two's complement a negative number is made by writing the positive value in binary, inverting every bit, then adding 1; 8 bits then hold -128 to 127 with only one zero
- Subtraction becomes addition: A - B is worked out as A + (-B) in two's complement, and any carry out of the leftmost column is dropped
Section 1Why we need signed integers
In the topics so far, an 8-bit number could only be 0 to 255, because every bit was a place value. Real data also has negative whole numbers: a bank balance below zero, a change in stock, a temperature below zero. The fix is to use the leftmost bit to show the sign. A bit pattern then has to be read as signed or unsigned, and the same 8 bits can mean different numbers. This topic uses whole numbers in 8 bits. There are two ways to store a negative number: sign and magnitude, and two's complement.
Section 2Sign and magnitude
The leftmost bit is the sign bit: 0 for positive and 1 for negative. The remaining 7 bits are the magnitude, which means the size of the number. +45 is 00101101. -45 is 10101101 (the sign bit changes, the 7 magnitude bits stay the same). With 7 magnitude bits the range is -127 to +127. The problem: there are two zeros, 00000000 (+0) and 10000000 (-0). That wastes a pattern and makes the arithmetic circuits more awkward.
Section 3Two's complement: making a negative number
Here's the trick for finding the negative of a number in two's complement: 1. Write the positive number in binary using all 8 bits. 2. Invert every bit (0 becomes 1, 1 becomes 0). 3. Add 1. Example, -45: +45 = 00101101 Invert = 11010010 Add 1 = 11010011 So -45 is 11010011. Positive numbers are written exactly as they are in ordinary binary, with a leftmost 0.
Section 4Reading a two's complement number
Look at the leftmost bit. A 0 means the number is positive and you read it as normal. A 1 means the number is negative. To find the size of a negative number, use the same steps again: invert and add 1. For 11010011: invert gives 00101100, add 1 gives 00101101 = 45, so the number is -45. A second method: give the leftmost bit the place value -128 instead of +128. For 11010011: -128 + 64 + 16 + 2 + 1 = -45. Both methods give the same answer, so use one to check the other.
Section 5Range of 8-bit two's complement
With 8 bits there are 256 patterns. In two's complement they stand for the numbers -128 to +127. The largest is 01111111 = 127. The smallest is 10000000 = -128. The pattern 00000000 is the only zero, and 11111111 is -1. With n bits the range is -2^(n-1) to 2^(n-1) - 1. For 4 bits that is -8 to 7. Compare sign and magnitude: 8 bits give -127 to +127 and two zeros. So two's complement fits one more number into the same space, and that number is -128.
Section 6Subtracting by adding
In two's complement, A - B is worked out as A + (-B). The same adding circuit can then do both addition and subtraction. This is the complement-and-add method. Example, 58 - 23 in 8 bits: +23 = 00010111 Invert = 11101000 Add 1 = 11101001, so -23 = 11101001 58 + (-23): 00111010 + 11101001 = 1 00100011 The sum has 9 bits. The carry out of the leftmost column is dropped, which leaves 00100011 = 35. And 58 - 23 = 35.
Section 7When the answer does not fit
A signed 8-bit result must stay between -128 and 127. If two positive numbers add to more than 127, the answer spills into the sign bit and a positive answer looks negative. Example: 100 + 100 in 8 bits. 01100100 + 01100100 = 11001000. That pattern reads as -56, which is wrong because 200 is bigger than 127. This is overflow again. Watch out: adding two positives and getting a negative, or two negatives and getting a positive, always means the answer has overflowed.
Examples
A shop in Portmore records a stock change of -37 items as an 8-bit number. (a) Write 37 as an 8-bit binary number. (b) Write -37 in sign and magnitude. (c) Write -37 in two's complement, and check it by reading the pattern back.
(a) 37 = 32 + 4 + 1, so 37 is 00100101. (b) Sign and magnitude: change the sign bit to 1 and keep the magnitude, so -37 is 10100101. (c) Two's complement: +37 = 00100101 Invert = 11011010 Add 1 = 11011011 So -37 is 11011011. Check: the leftmost bit has place value -128, so -128 + 64 + 16 + 8 + 2 + 1 = -37. The pattern reads back as -37.
A Mandeville shop has 58 units of an item in stock and sells 23. The computer works out 58 - 23 in 8-bit two's complement. (a) Write 58 and -23 in 8 bits. (b) Add them and state the 8-bit result after dropping any carry out. (c) Check by converting the result to decimal.
(a) 58 = 32 + 16 + 8 + 2, so 58 is 00111010. 23 is 00010111. Invert it to get 11101000, then add 1 to get 11101001, so -23 is 11101001. (b) 00111010 + 11101001: Carry | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 58 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 0 -23 | 1 | 1 | 1 | 0 | 1 | 0 | 0 | 1 Sum | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 1 There is also a carry out of the leftmost column, which is dropped. The 8-bit result is 00100011. (c) 00100011 = 32 + 2 + 1 = 35, and 58 - 23 = 35. The leftmost bit is 0, so the answer is positive, as expected.
Now let's water it
Session runs notes to flashcards to quiz back-to-back.
Where this comes from
- CSEC Information Technology Syllabus (CXC 30/G/SYLL 08, revised 2000, 2008, 2013) · Caribbean Examinations Council (CXC) · Section 1, Specific Objective 9 · Syllabus scope
- NEETS Module 13, Introduction to Number Systems and Logic Circuits (NAVEDTRA 14185, September 1998) · US Navy (Naval Education and Training Professional Development and Technology Center) · Chapter 1, Complementary subtraction (ones and twos complement, discarding the carry), pages 1-19 to 1-22, checked against (https://vtda.org/bits/other/US_Navy/NEETS/US%20Navy%20NEETS%20-%20Module%2013-Introduction%20to%20Number%20Systems%20and%20Logic%20NAVEDTRA%2014185.pdf) · Facts