Two's Complement - Binary
Two's Complement Logo

Navigation:

Home
About
Java Games
Binary
Hexidecimal
Contact

Binary

You have obvoiusly heard of binary before. It is that odd cryptic computer language, with ones and zeroes. It looks something like this: 01100101

Binary is actually not cryptic and not odd. Binary also is not only exclusive to computers. Binary is a form of counting. It is base 2.

How Humans Count

Humans count in base 10. This is completely reasonable, since we have ten fingers. There are 10 digits in our counting system: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. When we need to count with more than one digit, because we already have all combinations of one digit, we use two digits. Example: 0, ..., 8, 9, 10 - We start all over again with the first digit. The second digit increases again once we reach 19, and so on.

Computers Count the Same Way!(almost)

Computers count in base 2. They can't count like us because they only have two "fingers." These fingers are electrical impulses. On means 1, off means 0. You've probably heard that before, too. There are 2 digits in their counting system: 0 and 1. So how can they count? Same way as us! They start with 0, and then go to 1. Nothing cryptic about that, is there. But now they ran out of digits, just like we do when we get to 9. But do they panic? No! They do this: 10. 10 in base 2 = 2 in base 10. Odd, isn't it. Anyway, they continue counting: 11 = 3. And once again they must carry the one: 100 = 4. And so on: 101 = 5, 110 = 6, 111 = 7, 1000 = 8, 1001 = 9, 1010 = 10.

Converting Back... And Forth...

Now there must be a better way to convert numbers back and forth without counting. Well there is.

128

64

32

16

8

4

2

1

0

1

1

0

0

1

0

1

Note: Usually you will see binary numbers grouped in 8 digits

Ok, to convert to decimal, just add up all the numbers above the 1's. This binary number happens to be 101. To convert to binary, Look for the highest number on the top row that can be subtracted from your number whithout your number becoming negative. Subtract it, make the corresponding binary digit a 1, and repeat until your number is zero. Example for 75:
75 - 128 < 0
75 - 64 = 11
Our binary number looks like this so far: 01000000 11 - 32 < 0
11 - 16 < 0
11 - 8 = 3
Our binary number looks like this so far: 01001000 3 - 4 < 0
3 - 2 = 1
Our binary number looks like this so far: 01001010 1 - 1 = 0
This is the result in binary: 01001011

Two's Complement

Here's when two's complement actually means. It is how you make negative binary numbers. First off, you must know when or when not two's complement is being applied. Otherwise, you will get haywire results. The highest value a binary number can go is sliced in half when using two's complement. It is because the first binary digit is used to tell whether it is negative or not. For an example, lets use 75 from the previous one: 01001011. The first thing we must do is find the complement. Complement means "opposite." ComplIment is the opposite of insult. To find the complement, invert all of the bits(a bit is a binary digit) so it looks like this: 10110100. Now to complete the effect, add one. Now you have negative 75: 10110101! But how can you really see if it is negative? This is how:

  75
+-75
   0

So in theory, 01001011 + 10110101 should equal 00000000. Lets find out:

Poof!
it's
gone!

Carry
the
1

Carry
the
1

Carry
the
1

Carry
the
1

Carry
the
1

Carry
the
1

Carry
the
1

 

 

0

1

0

0

1

0

1

1

+

1

0

1

1

0

1

0

1

=

0

0

0

0

0

0

0

0



The extra 1 vanishes in hyperspace, and we have 00000000! If you wanted to see the value of a negative number, you would apply two's complement: 10110101 becomes 01001010 and just add 1: 01001011 which is 75. Just stick a - on there and you know 10110101 = -75!

Binary in the Real World

So far, we have only talked about binary in computers. However, in real life, things are slightly different. Two's complement doesn't apply in real life. You write it just like you would a decimal number: -101. And there are no "groupings" of numbers, like 8, 16, and 32. That means that the first digit is either 1 or the number is 0. Examples:
75 = 1001011 - Notice there are only 7 digits
-75 = -1001011 - Notice the negative sign!
2 = 10
-3 = -11
0 = 0