representar-numeros-negativos-en-binario

How to represent negative integers in binary

  • 7 min

In the previous article, we saw how to represent positive integers (which was quite easy). Now we are going to see how to represent negative integers, which is a bit “more involved”.

In binary, we typically represent binary numbers in two’s complement. It’s a technique that simplifies calculations when working with negative numbers (although it’s a bit more complicated for humans to understand).

But, instead of just explaining what two’s complement is, let’s try to reason why we use this system and how it was arrived at.

Here comes a “heavy” explanation of how the two’s complement system came about
If you’re not interested and just want to see how it works, skip to the next section

Representing Negative Numbers in Binary

Let’s suppose we are “inventing” how to represent negative numbers in binary. Let’s see the logical steps that would eventually lead us to two’s complement.

Your Friend Two’s Complement

Summary of everything we’ve seen, two’s complement is a form of representation of negative numbers in the binary system.

At first glance it seems complicated, but it’s used a lot because it allows us to operate with negative numbers in a simple way (it makes calculations much easier).

If we want to convert a positive number to negative, in binary system representation using two’s complement:

  • We set the leftmost bit to 1, which represents the sign
  • We invert the rest of the bits
  • We add 1 to the above

What is a Binary Number Worth?

I come back to this, because it’s something that confuses a lot of people. What is a binary number worth? Imagine you have this number:

10011001
Copied!

How much is this number worth?

  • Some of you will tell me 153
  • Others will tell me -103

How can that be? Because a binary number doesn’t represent anything, unless you tell me what system it uses to represent it.

The binary representation is the same. But the number they represent is NOT the same

To know what number it is, I need you to say what you are representing, an integer, an integer with negatives, if it’s a float.