Negative Binary Numbers


Youtube


#Negative aka Signed Numbers in Two’s Complement

Signed numbers are numbers that can take on negative values. It’s called signed because it can take on a “negative sign” as well as a positive sign. This notation of representing negative numbers is called two’s complement.

To get the binary representation of a negative number, say -6, we have to:

  1. Write the number positive version in binary
    6 in binary
    8421
    0110
  2. Flip all the bits
    0 1 1 0 (6) becomes...
    _______
    1 0 0 1 (FLIPPED)
  3. Add one to the flipped result
         
              1
          1 0 0 1 
        + 0 0 0 1
        _________
                0
        
         
              1
          1 0 0 1
        + 0 0 0 1
        _________
          1 0 1 0 (-6)
        

    Answer: -6 in binary is 1010.
    Let’s talk about two’s complement and what logistics drive 1010 = -6

#How Two’s complement encoding works

-8 + 2 = -6 (4 bit-width)
8421
1010
-32 + 16 + 8 + 2 = -6 (6 bit-width)
32168421
111010
-128 + 64 + 32 + 16 + 8 + 2 = -6 (8 bit-width)
1286432168421
11111010


Notice: Only the most significant bit(left most highlighted in red) holds is of a negative weight.

Given a bit-width of 4, the decimal number -610 = 10102
Given a bit-width of 6, the decimal number -610 = 1110102
Given a bit-width of 8, the decimal number -610 = 111110102

When representing negative numbers, the width matters, because only the most signifncant bit(left most as highlighted in red) holds the negative weight needed to split the positive and negative numbers as seen in the table way below. It’s important to notice that all negative numbers have a leading bit of 1.

Let’s try a few examples with variable bit-widths:

Check your understanding!

What is -3 in binary?(given a bitwidth of 4)
-8 + 4 + 0 + 1 = -3
8421
1101

What is -15 in binary? (given a bitwidth of 5)
-16 + 0 + 0 + 0 + 0 + 1 = -15
168421
10001

What is -20 in binary? (given a bitwidth of 6)
-32 + 0 + 8 + 4 + 0 + 0 = -20
32168421
101100


More Two’s Complement Exercises

#Multiple Meanings


In English we have words like

“bark”:

  1. A noise that a dog makes.
  2. What trees are mare out of.

“sex”:

  1. either of the two main categories (male and female) into which humans and most other living things are divided on the basis of their reproductive functions.
  2. an activity

One word, multiple meanings!

Likewise in the world of bits, the bit pattern: 11010102 can mean:

  1. The letter “j”
  2. The number 10610
  3. The number -2210
  4. The hexadecimal number D216

Computers only see zeros and ones, its how and what we humans decide to interpret them that gives them meaning/value.

#Multiple interpretations of bits


Given the bit-width of 4:

-610 in binary is 10102, but wait...

1010 in binary is 10102, also?!

They both use the same pattern of bits? What gives?!

Consider all the possibilities of unsigned and signed 4 bit numbers:

unsigned
DecimalBinary
00000
10001
20010
30011
40100
50101
60110
70111
81000
91001
101010
111011
121100
131101
141110
151111
signed
DecimalBinary
-81000
-71001
-61010
-51011
-41100
-31101
-21110
-11111
00000
10001
20010
30011
40100
50101
60110
70111


Again, as highlighted in red, the most significant bit(left most bit) in a signed number 2bitwidth-1 takes on a negative weight (in this case it’s 8).

8 + 2 = 10 (unsigned)
8421
1010
-8 + 2 = -6 (signed)
8421
1010


Now knowing that bit patterns can have 2 (or more) meanings, let’s go the other way around get some practice converting bit patterns to signed and unsigned decimal representations.

Check your understanding!

What is 0111 in both signed and unsigned decimal? (given a bitwidth of 4)
0 + 4 + 2 + 1 = 7 (signed)
8421
0111
0 + 4 + 2 + 1 = 7 (unsigned)
8421
0111

answer: 7,7 ! they’re the same because its in the safe range of bit patterns they share in common



What is 101100 in both signed and unsigned deicmal? (given a bitwidth of 6)
-32 + 0 + 8 + 4 + 0 + 0 = -20 (signed)
32168421
101100
32 + 0 + 8 + 4 + 0 + 0 = 44 (unsigned)
32168421
101100

answer: -20,44



What is 10001 in both signed and unsigned decimal? (given a bitwidth of 5)
-16 + 0 + 0 + 0 + 0 + 1 = -15 (signed)
168421
10001
16 + 0 + 0 + 0 + 0 + 1 = 17 (unsigned)
168421
10001

answer: -15,17



More Two’s complement signed and unsigned exercises

The safe range of converting numbers back and forth between the worlds of unsigned and signed in a 4 bit system is: 0 to 7. This is where the bitpatterns are exactly the same. Everywhere else, they differ and mismatch which can cause wrap around issues and wrong math which we will cover extensively in the next section. Notice however that when you cast between signed to unsigned and vice versa, the bit patterns do not change but the resulting decimal value does.

If we want to represent -14, it’s only possible in bit-width of 5 and above. Lets see this in action:

Extra Examples

Step 1: Write the number positively in binary

14 in binary
8421
1110

Step 2: Flip all the bits

1110
FLIP
0001

Step 3: Add one to the flipped result

     
          1
      0 0 0 1 
    + 0 0 0 1
    _________
            0
    
     
          1
      0 0 0 1
    + 0 0 0 1
    _________
      0 0 1 0 (2)
    

I mistakenly did this in a 4 bit width, and got the wrong answer of 2. Consulting the table above, -14 doesn’t fit in a bit width of 4. Again! You must always consider the width when dealing with negative numbers. Extend the width to 5 bits and re-doing the steps above will work:

0 + 8 + 4 + 2 + 0 = 14
168421
01110

You should end up with:

-16 + 0 + 0 + 2 + 1 = -14
168421
10010


How does the computer tell apart signed and unsigned numbers?

It doesn’t! We manaully tell the computer by declaring the type signed or unsigned. In scripting languages like Python, Javascript, etc, this is hidden from you by way of having all numbers be signed by default. In other languages like C, you have to explicitly declare how many bits a number has, and whether the number can be signed or unsigned, which will flip a switch on the CPU that tells it to interpret a binary number to be negative by considering the left most bit to have a negative weight.