Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

How are negative numbers represented — sign-magnitude, one's complement, two's complement

How are negative numbers represented—sign-magnitude, one's complement, two's complement

Computers only know 0 and 1, so how are negative numbers represented?

For example, the decimal number 5 can be written in binary as 00000101, but -5 should be represented how?

The most straightforward idea is to set aside one bit for the sign, but this raises new questions: how should negative numbers take part in addition? Why is the range of an 8-bit integer -128 to 127, rather than -127 to 128?

To understand these questions, you need to know the three ways computers represent signed integers:sign-magnitude, one's complement, and two's complement.

Below, taking 8-bit binary as an example, we step through their differences with diagrams and interactive demos.

1. How does 8-bit binary represent positive and negative numbers?

8-bit binary has a total of 2^8 = 256 different bit patterns.

Interpreted as unsigned integers, they can represent 0 to 255; interpreted as two's complement, they can represent -128 to 127.

Structure diagram of 8-bit binary (using sign-magnitude as an example)

+5 sign-magnitude

0

0

0

0

0

1

0

1

-5 sign-magnitude

1

0

0

0

0

1

0

1

**Sign bit 0: positive number

**Sign bit 1: negative number

**The remaining 7 bits: absolute value

Note: what is shown here is sign-magnitude. Although two's complement can also identify the sign from the highest bit, that bit carries a negative weight in numeric computation and cannot simply be understood as an independent sign bit.

2. Sign-magnitude: the most intuitive representation

Sign-magnitude uses the highest bit for the sign and the remaining bits for the absolute value.

Its problem is that there are two zeros:

Positive zero and negative zero in sign-magnitude

+0

0

0

0

0

0

0

0

0

-0

1

0

0

0

0

0

0

0

Two different binary bit patterns represent the same value 0, increasing the complexity of numeric comparison and arithmetic.

In addition, sign-magnitude cannot simply add all the binary bits directly to perform signed addition.

For example, computing directly with 8-bit sign-magnitude 5 + (-3), would give 10001000, which interpreted as sign-magnitude is -8, while the correct answer should be 2.

Therefore, sign-magnitude arithmetic needs extra handling of operand signs and addition/subtraction of absolute values.

3. One's complement: invert the binary bits

The rule of one's complement is:

Taking -5 as an example:

The process of computing the one's complement of -5

+5 in binary

0

0

0

0

0

1

0

1

Invert every bit: 0 becomes 1, and 1 becomes 0.

-5 one's complement

1

1

1

1

1

0

1

0

00000101 → 11111010

One's complement still has two representations of zero:

+0 = 00000000

-0 = 11111111

Therefore, one's complement likewise cannot make full use of all bit patterns to represent distinct integers.

4. Two's complement: invert and add 1

Two's complement solves the problem of duplicate representations of positive and negative zero in sign-magnitude and one's complement.

For negative numbers, a common way to compute two's complement is:

First write the fixed-width binary of the absolute value, then invert each bit, and finally add 1.

Diagram: computing the two's complement of -5

8-bit two's complement from +5 to -5

① +5

0

0

0

0

0

1

0

1

2. Invert each bit

1

1

1

1

1

0

1

0

3. Add 1

1

1

1

1

1

0

1

1

00000101 (+5)

11111010 (inverted)

11111011 (add 1, giving the two's complement of -5)

Therefore, the 8-bit two's complement of -5 is 11111011.

Comparison of the three representations

Sign-magnitude, one's complement, and two's complement of -5

Sign-magnitude

10000101

Sign bit + absolute value

One's complement

11111010

Invert the positive number's bit pattern

Two's complement

11111011

Invert and add 1

5. Why can two's complement do addition and subtraction directly?

The core principle of two's complement is modular arithmetic.

8-bit binary has 256 bit patterns, and fixed-width addition is equivalent to taking modulo 256 Take the modulus.

For example, computing 5 - 3:

Two's complement addition of 5 + (-3)

+5

00000101

Two's complement of -3

11111101

→

Result

00000010

  00000101


1 00000010

  ↑

Discard the carry beyond 8 bits

Result: 00000010 = 2

It can also be understood using decimal modular arithmetic:

5 + (256 - 3) = 258

258 mod 256 = 2

This is why two's complement can handle subtraction with a unified fixed-width addition mechanism.

6. 4-bit two's complement ring demo

With only 4-bit binary, there are 16 bit patterns.

Interpreted as two's complement, these 16 bit patterns correspond to -8 through 7.

The interactive chart below lets you observe how two's complement cycles through fixed-width bit patterns as the value changes.

Interactive demo: 4-bit two's complement

-3

Current two's complement:1101

Corresponding unsigned value:13

From the ring chart we can see:

7. Interactive two's complement calculator

Enter an integer in the range -128 to 127 to see the corresponding 8-bit sign-magnitude, one's complement, and two's complement.

8-bit integer encoding calculator

Sign-magnitude

One's complement

Two's complement

**Special note:** the range of 8-bit sign-magnitude and one's complement is -127 to 127, and they cannot represent -128. 8-bit two's complement, however, can represent -128, with the bit pattern 10000000.

8. Why does the highest bit of two's complement represent a negative number?

In 8-bit two's complement, each bit can be understood as having a different numeric weight.

Unlike unsigned integers, the highest bit of two's complement has a weight of -128, not +128.

Bit weights of 8-bit two's complement

Bit position 7 6 5 4 3 2 1 0
Bit weight -128 64 32 16 8 4 2 1
Bits of -1 1 1 1 1 1 1 1 1

11111111

= -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1

= -1

Therefore, the highest bit in two's complement is not merely an independent sign flag; it also takes part in the computation of the whole value.

9. Full comparison of sign-magnitude, one's complement, and two's complement

Value Sign-magnitude One's complement Two's complement
+5 00000101 00000101 00000101
-5 10000101 11111010 11111011
+0 00000000 00000000 00000000
-0 10000000 11111111 No independent representation exists
+127 01111111 01111111 01111111
-127 11111111 10000000 10000001
-128 Cannot be represented Cannot be represented 10000000

10. Diagram of two's complement overflow

The range of 8-bit two's complement is -128 to 127. If the true mathematical result exceeds this range, signed overflow occurs.

8-bit modular arithmetic: 127 + 1

127

0

1

1

1

1

1

1

1

+1

0

0

0

0

0

0

0

1

Low 8-bit result

1

0

0

0

0

0

0

0

The mathematical result is 128, which exceeds the maximum value 127 of 8-bit two's complement. Keeping the low 8 bits gives 10000000, which interpreted as 8-bit two's complement is -128.

Note that this is only the result of a fixed 8-bit operation truncated modulo 256.

11. Implementing two's complement conversion in Python

Python can simulate two's complement of a specified bit width using bitwise operations.

Example

def

to_twos_complement

(

n

,

bits

=

8

)

:

   

"""Convert an integer to a two's complement string of the specified bit width."""

   

if

bits

<

1

:

       

raise

ValueError

(

"Bit width must be greater than or equal to 1"

)

    minimum

=

(

1

<<

(

bits -

1

)

)

    maximum

=

(

1

<<

(

bits -

1

)

)

1

   

if

not

minimum

<=

n

<=

maximum:

       

raise

ValueError

(

"Integer exceeds the two's complement representation range"

)

    mask

=

(

1

<<

bits

)

1

   

return

format

(

n & mask

,

f

"0{bits}b"

)

def

from_twos_complement

(

binary

)

:

   

"""Convert a two's complement string to a decimal integer."""

   

if

not

binary

or

any

(

c

not

in

"01"

for

c

in

binary

)

:

       

raise

ValueError

(

"Please enter a valid binary string"

)

    unsigned

=

int

(

binary

,

2

)

    bits

=

len

(

binary

)

   

if

binary

[

0

]

==

"1"

:

       

return

unsigned -

(

1

<<

bits

)

   

return

unsigned

for

n

in

[

5

,

5

,

20

,

0

,

127

,

128

]

:

    binary

=

to_twos_complement

(

n

)

    restored

=

from_twos_complement

(

binary

)

   

print

(

f

"{n:4d} -> {binary} -> {restored}"

)

Execution result:

   5 -> 00000101 -> 5

  -5 -> 11111011 -> -5

 -20 -> 11101100 -> -20

   0 -> 00000000 -> 0

 127 -> 01111111 -> 127

-128 -> 10000000 -> -128

12. Frequently asked questions

Why does two's complement have only one representation of zero?

Because in fixed n-bit two's complement, the only bit pattern for 0 is all zeros. Inverting zero and adding 1, then truncating to the fixed width, still yields all zeros.

Why is -128 still -128 after inverting the bits and adding 1?

In 8-bit two's complement, the bit pattern of -128 is 10000000. After inverting each bit and adding 1, keeping the low 8 bits still gives 10000000.

The reason is that +128 is outside the representable range of 8-bit two's complement, so the smallest negative number has no representable positive opposite.

Are two's complement and binary the same concept?

No. Binary is the basis of counting and encoding that uses 0 and 1 to represent data; two's complement is a signed-integer encoding scheme for interpreting fixed-width binary bit patterns.

Summary

Sign-magnitude, one's complement, and two's complement are all encoding schemes for signed integers.

Sign-magnitude is fairly intuitive but has the problems of positive zero, negative zero, and complex sign handling; one's complement represents negative numbers by inverting bits but still has two zeros; two's complement keeps only one representation of zero and allows modular arithmetic to uniformly handle fixed-width addition and subtraction.

When understanding two's complement, the points most worth remembering are: