Menu

Binary

Denary to Negative Binary Conversion

Contents

  1. Two's Complement
  2. Two's Complement Range and Overflow
  3. Negative Denary: Flip Method

Two's complement is the method computers use to store signed integers — whole numbers that can be positive, zero or negative — in binary using a fixed number of bits.

An unsigned integer stores only zero and positive values: in eight bits it covers 0 to 255. A signed integer uses the same eight bits but uses half of the 256 patterns for negative values, so patterns with a 1 in the leftmost bit mean a negative value when read as two's complement. This is why the same pattern can mean one value when read as unsigned and another when read as signed.

With eight-bit two's complement, every value, positive or negative, is written as exactly eight bits, so you keep any leading zeros or leading ones. A fixed width matters because the processor needs to know how many bits belong to each number.

The sign bit

In an eight-bit two's complement number, the leftmost bit is the sign bit. A 0 there means the value is zero or positive; a 1 means it is negative.

Eight-bit patternSign bitRead as unsignedRead as two's complement
011001000100+100
110001111199a negative value

A negative value is not made by simply changing the leftmost bit of its positive pattern to 1 and leaving the rest unchanged; that idea is sign and magnitude, and it is not two's complement. Here the bits are arranged together as one pattern so the computer can add and subtract positive and negative values using the same circuits it already has. You can find the exact pattern for a negative number using the flip-and-add-one method.

Because the leftmost bit gives the sign, eight-bit two's complement has fewer positive patterns available than eight-bit unsigned.

An 8-bit two's complement number always uses exactly eight bits, and that fixed width limits which denary values it can hold. The range is -128 to +127 inclusive: 256 different bit patterns, one for each value from -128 up to +127.

Why the range is not symmetrical

The leftmost bit decides the sign. Every pattern that starts with 1 is negative, and there are 128 such patterns, so the negative values run from -128 up to -1. The 128 patterns that start with 0 cover zero and the positive values 0 to +127. Zero takes one pattern of its own, which is why you get 127 positive values but 128 negative ones.

Denary valueInside the range?Reason
-128YesThe smallest value
0YesZero is included
+127YesThe largest value
+128NoOverflow: outside the range

-128 fits, but +128 does not

The bottom of the range is -128, so -128 has a valid code. The value +128 is outside the range. Eight bits have 256 codes and all of them are already used, so there is no spare code for +128. Trying to store a value outside -128 to +127 in eight bits is called overflow.

When a calculation overflows

Overflow can happen even when the two values being added are both inside the range. For example, 90 and 90 are both valid, but 90 + 90 = 180, which is outside -128 to +127. In eight bits, 90 is 01011010. Adding it to itself gives the eight-bit result 10110100. Read as a two's complement number, that pattern means -76, not +180. The answer has wrapped round to a different value, so the result is not a valid larger positive answer.

A bar from -128 to +127 split at the sign bit: patterns starting 1 are -128 to -1, patterns starting 0 are 0 to +127, with +128 as overflow. An arc shows 90 + 90 = 180 wrapping round to -76.

Adding 1 to the largest positive value, +127, does something similar: the carry ripples through every 1 bit, the sign bit turns to 1, and the pattern is read as the smallest negative value instead of a bigger positive one. That too is an overflow.

To write a negative denary number in 8-bit two's complement, convert the positive version of the number to binary, flip every bit, then add 1. This is the flip method, and it works for any negative value from -1 to -128.

The three steps

  1. Write the positive number in 8 bits. Ignore the minus sign, convert the number to binary and pad it with leading zeros so it is exactly 8 bits long.
  2. Flip every bit. Each 0 becomes 1 and each 1 becomes 0, including the leading zeros.
  3. Add 1. Add 1 to the flipped pattern in binary, carrying where 1 + 1 makes 10, and keep the answer to 8 bits.

Worked example: -58

Start with positive 58. Using the place values, 58 = 32 + 16 + 8 + 2, so 58 is 00111010.

Step8-bit pattern
1. Positive 58 in binary00111010
2. Flip every bit11000101
3. Add 111000110

In step 3 the rightmost bit is 1, so 1 + 1 gives 0 carry 1. The carry goes into the next column, where 0 + 1 gives 1, and every other bit stays as it is. So -58 is 11000110.

Check your answer

A negative number always starts with 1, so if your answer starts with 0 something has gone wrong. To be sure, add up the two's complement place values where there is a 1, remembering that the leftmost bit is worth -128: -128 + 64 + 4 + 2 = -58.

Another one to try: 100 is 01100100, which flips to 10011011, and adding 1 gives 10011100. Check: -128 + 16 + 8 + 4 = -100.

Also useful