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:
Write the number positive version in binary
6 in binary 8 4 2 1 0 1 1 0 Flip all the bits
0 1 1 0 (6) becomes...
_______
1 0 0 1 (FLIPPED)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 | 4 | 2 | 1 |
---|---|---|---|
1 | 0 | 1 | 0 |
32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|
1 | 1 | 1 | 0 | 1 | 0 |
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
1 | 1 | 1 | 1 | 1 | 0 | 1 | 0 |
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 | 2 | 1 |
---|---|---|---|
1 | 1 | 0 | 1 |
What is -15 in binary? (given a bitwidth of 5)
16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|
1 | 0 | 0 | 0 | 1 |
What is -20 in binary? (given a bitwidth of 6)
32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|
1 | 0 | 1 | 1 | 0 | 0 |
More Two’s Complement Exercises
#Multiple Meanings
In English we have words like
“bark”:
- A noise that a dog makes.
- What trees are mare out of.
“sex”:
- 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.
- an activity
One word, multiple meanings!
Likewise in the world of bits, the bit pattern: 11010102 can mean:
- The letter “j”
- The number 10610
- The number -2210
- 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:
Decimal | Binary |
---|---|
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
8 | 1000 |
9 | 1001 |
10 | 1010 |
11 | 1011 |
12 | 1100 |
13 | 1101 |
14 | 1110 |
15 | 1111 |
Decimal | Binary |
---|---|
-8 | 1000 |
-7 | 1001 |
-6 | 1010 |
-5 | 1011 |
-4 | 1100 |
-3 | 1101 |
-2 | 1110 |
-1 | 1111 |
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
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 | 4 | 2 | 1 |
---|---|---|---|
1 | 0 | 1 | 0 |
8 | 4 | 2 | 1 |
---|---|---|---|
1 | 0 | 1 | 0 |
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)
8 | 4 | 2 | 1 |
---|---|---|---|
0 | 1 | 1 | 1 |
8 | 4 | 2 | 1 |
---|---|---|---|
0 | 1 | 1 | 1 |
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 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|
1 | 0 | 1 | 1 | 0 | 0 |
32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|
1 | 0 | 1 | 1 | 0 | 0 |
answer: -20,44
What is 10001 in both signed and unsigned decimal? (given a bitwidth of 5)
16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|
1 | 0 | 0 | 0 | 1 |
16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|
1 | 0 | 0 | 0 | 1 |
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
8 | 4 | 2 | 1 |
---|---|---|---|
1 | 1 | 1 | 0 |
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 _________ 01 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:
16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|
0 | 1 | 1 | 1 | 0 |
You should end up with:
16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|
1 | 0 | 0 | 1 | 0 |
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.