

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Using 2s compliment in binary numbers
Typology: Summaries
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Computers store everything as 1s and 0s. Positive whole numbers are straightforward in binary โ but what about negative numbers? You can't write a minus sign in binary! The standard solution used in virtually every modern computer is two's complement , a clever system that lets addition and subtraction work with the same circuitry for both positive and negative numbers.
1. The Problem with Negative Numbers With 8 bits we can store the unsigned values 0โ255. If we want to store negative numbers too, we need to use one bit to indicate the sign. Two's complement reserves the most significant bit (MSB) โ the leftmost bit โ as the sign bit : 0 = positive, 1 = negative. MSB (sign) Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 (LSB)
Red = sign bit. This pattern starts with 0, so it is positive. With 8-bit two's complement the range is โ128 to +. Bits Range of values 4-bit (^) โ8 to + 8-bit (^) โ128 to + 16-bit (^) โ32,768 to +32, 32-bit (^) โ2,147,483,648 to +2,147,483, Formula: For n bits: range is โ2nโ1^ to +2nโ1^ โ 1.
2. How to Find the Two's Complement To convert a positive number into its negative two's complement equivalent, follow two steps. We'll convert +19 into โ19 using 8 bits. 1 Write the positive number in binary, then flip every bit (one's complement) +19 in 8-bit binary: 00010011 Flip every bit (0โ1): 11101100 โ this is the one's complement 2 Add 1 to the result 11101100 + 00000001 = 11101101 So โ19 in 8-bit two's complement is 11101101. Notice the MSB is 1 โ confirming it's negative. โ ๐ก Shortcut method
Starting from the right , copy bits up to and including the first 1 , then flip all remaining bits to the left. For 00010011 : copy 011 (rightmost 1 and everything right of it), flip the rest โ 11101 , giving 11101101. Same answer, less work! ๐ก Two's complement is its own inverse Applying the same two steps to 11101101 (โ19) gives back 00010011 (+19). The process works in both directions.
3. How to Read a Negative Two's Complement Number Given a binary pattern with MSB = 1, you need to find its decimal value. We'll decode 11010110. 1 Recognise it's negative (MSB = 1), then apply two's complement to find the magnitude Original: 11010110 Flip all bits: 00101001 Add 1: 00101010 Convert to decimal: 32 + 8 + 2 = 42 2 Apply the sign 11010110 in 8-bit two's complement = โ42 โ ๐ก Alternative: the weighted MSB method Treat the MSB as having value โ128 (for 8-bit), then add the remaining bits normally. For 11010110 : โ128 + 64 + 0 + 16 + 0 + 4 + 2 + 0 = โ42. Same answer โ useful for quick mental checks. 4. Addition & Subtraction โ The Big Payoff The real beauty of two's complement is that ordinary binary addition works for both positive and negative numbers โ the CPU needs only one adder circuit. Any carry out of the MSB is simply discarded. Example: 19 + (โ7) = 12 +19 = 00010011 โ7 = 11111001 Sum = 100001100 โ 9 bits Discard carry: 00001100 Check: 00001100 โ = 8 + 4 = 12 โ The carry bit out of the MSB is simply ignored โ the hardware does this automatically.