Data Conversion Section Links

  1. Home Page
  2. Introduction
  3. Goals
  4. Why Digitize?
  5. Number Representation
  6. Digital to Analog
  7. Analog to Digital
  8. Applications
  9. Literature Citations

Two's Complement Binary Numbers

Choose Subtopic
Binary 2's Complement Binary Hexadecimal Why ADCs Use Integer Math The Nyquist Sampling Theorem

Integers not only have magnitude. They also have a sign. How do we deal with that in binary?

Straight Binary

The first option is to ignore sign and just use all the available numbers, running from 0 to 2N-1 where N is the number of bits in the number. That's what we already discussed.

Sign Plus Magnitude Binary

A second approach is to use one bit of the number (usually the left-most bit, what was originally the most significant bit with value 2N-1) to represent sign. This leaves N-1 bits to represent value. This is only rarely seen in the instrumentation world. Why?

People who think of positive and negative numbers as funadmentally disjoint are comfortable with Sign Plus Magnitude Binary coding. Those who see the integer number line as describing a single, uniform set of numbers that happens to have 0 as its midpoint may find Sign Plus Magnitude Binary awkward.

Examples: for 4 bit numbers, 0110 = +6, 1110 = -6 since 0 in the lead bit = +, 1 in the lead bit = -.

Binary Addition

We need a brief aside. For straight binary numbers, addition works just as one would expect using place value. 0 + 0 = 0 (no surprise). 0 + 1 = 1 + 0 = 1. In binary, there is no way to have a displayed number other than 0 or 1, so 1 + 1 = 0 (in the 1's place) with 1 carried to the next most significant place (the 2's place). This is just like base 10 where 9 + 9 = 18; we have 8 in the 1's place, and the extra 10 gets carried one column to the left. What if we make thing a little more complicated?

10 + 01 = 01 + 10 = 11 (in base 10, 2 + 1 = 1 + 2 = 3)

10 + 10 = 100 (or, in base 10, 2 + 2 = 4)

Two's Complement Binary

Two's Complement signed integers use a coding that is intuitive for positive numbers but that takes some getting used to for negative numbers. If the most significant (left-most) bit is 0, the number is construed as positive, and the coding looks just like straight binary. If the lead bit is 1, the number is negative, but not just reading the bits is misleading. To convert a binary number from positive to negative, we Complement (change all the 0's to 1's) and Increment (add 1). We can go from negative to positive the same way. Here's an example (using 4 bits again).

+6 = 0110.

Complement → 1001

Increment → 1010

Why would we want to use 1010 = -6. First, let's demonstrate that complement + increment returns +6 as an answer:

Complement → 0101

Increment → 0110

What should happen if we add + 6 and -6? We should get 0, regardless of how we write the problem. What happens if, in the middle of our calculations, we ignore the meaning of place value and simply add the representation for + 6 and the representation for -6 AS IF the most significant bit were just like all the others?

   0110

+ 1010

1 0000

The addition overflowed into the 5th bit, but the lower 4 bits give us 0. What we have shown is that subtraction in the two's complement system can be done using addition. In designing digital electronics, this is a huge simplification. Let's try 6-5

6: 0110

5: 0101

Complement and increment 5: -5 = 1011

Add 6 and -5:

   0110

+ 1011

1 0001

Again, we had an overflow to the fifth bit, but we got the right answer (6 - 5 = 1). What happens if we take 5 - 6?

5: 0101

6: 0110

Complement and increment 6: -6 = 1010

   0101

+ 1010

0 1111

We get -1 and there's no overflow to the fifth bit! We thus see that the fifth bit (or, in general, the bit one to the left of the most significant bit in the numbers with which we're working) = 1 if the minuend is bigger than the subtrahend, and that bit = 0 if the subtrahend is bigger than the minuend. This is more information rich than sign-magnitude binary, and takes less puzzling to do subtraction. That is why, inside computers, two's complement representation is nearly universal.

Exercise: For an 8 bit binary word, write down -3 in two's complement binary. 
You might start by writing down +3, then complementing and incrementing. 

Exercise: Compute 3-5 and 5-3 (answers obviously ±2) in two's complement binary. 

By now, you've figured out that 111111 is -1, 111110 is -2, and so on. If we have a 4 bit number, 1001 is (complement: 0110; increment: 0111) -7. But what is 1000? By convention, that's -8, NOT 8. Why? A lead 1 is taken as negative (just as in Sign Plus Magnitude Binary). So if we have N bits in the binary representation, we can express numbers from -2N-1 to +2N-1 - 1. For N = 4, that's -8 to +7.

In a Nutshell: Two's complement binary allows representation of both positive and negative integers, allows for easy sign change, and allows subtraction (or addition of numbers with signs) more easily than sign plus magnitude coding.

Offset Binary

One other coding scheme is occasionally used. Suppose the actual numbers (base 10 equivalent) run from -128 to +127. If we simply added 128 to every number, then the range becomes 0 to 255, the same as straight binary. This is called offset binary. The coding for -128 is 0000 0000, the coding for 0 is 1000 0000, and the coding for +127 is 1111 1111. If one is aware in advance that this coding is used, it is common to subtract 2N-1 from each number as it is read into a computer so that it can be stored as a signed integer, either sign-magnitude or two's complement. We deal with this convention no farther here, but one will occasionally spot a data converter that uses this convention. Usually, a converter using one of the other conventions is available and is more commonly used.

Next