Using 2s compliment in binary numbers, Summaries of Computer science

Using 2s compliment in binary numbers

Typology: Summaries

2025/2026

Uploaded on 03/29/2026

boris-karloff-1
boris-karloff-1 ๐Ÿ‡ฌ๐Ÿ‡ง

9 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Two's Complement Binary Computer Science โ€” Student Guide
Two's Complement Binary
How computers store and work with negative numbers
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)
01011010
Red = sign bit. This pattern starts with 0, so it is positive. With 8-bit two's complement the range is
โˆ’128 to +127.
Bits Range of values
4-bit โˆ’8 to +7
8-bit โˆ’128 to +127
16-bit โˆ’32,768 to +32,767
32-bit โˆ’2,147,483,648 to +2,147,483,647
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
pf3

Partial preview of the text

Download Using 2s compliment in binary numbers and more Summaries Computer science in PDF only on Docsity!

Two's Complement Binary

How computers store and work with negative numbers

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.