## “Two’s complement” and how negative numbers in binary are identified

As we know our computer can only interpret the numbers 1 and 0. 0 is represented by no voltage or ground while 1 is represented by low voltage or high voltage. The CPU of a regular computer consists of billions of transistors that can either represent a 0 or a 1.

So when your CPU only can read 0’ s and 1’s how could you represent negative numbers?

In the following I am going to explain how this process is made possible through “Two’s compliment”. The by far best method to display negative numbers in a binary system.

Using a third symbol as a “-” is doomed to fail, as many implementations of non-binary systems have proven in the past.

It has been tried to use ternary systems instead of binary systems.

In these implementations 0 was represented by no voltage, 1 by low voltage and 2 by high voltage. The idea sounds plausible but in reality all implementations of this technique were inexact processes. They always had misinterpretations. When you are working with millions of voltage signals per second it is difficult to differentiate between high voltage and low voltage. That is why Binary code is the norm for electronic devices. They have proven themselves to be extremely precise and exact.

First I am going to give you a quick introduction on how machine code works.

Machine language is simple. No voltage means 0 and voltage means 1.

These signals are referred to as bits.

The smallest unit a physician will work with are quarks. They are the elements that electrons consist of.

But don’ t worry I can assure you if you are working in the IT Segment the smallest unit you are possibly going to work with are bits.

If you buy an internet plan you will see how many bits you can maximally get per second. But don’t confound them with bytes. A byte is8 bits.

4 bits are a nibble. 8 bits are a byte and a word is 2 bytes (16 bits), a Doubleword is 4 bytes (32 bits), and a quadword is 8 bytes (64 bits).

Now you may have heard of Hexadecimal. To very interested people I recommend to calibrate their brains to start thinking in Hexadecimal instead of Decimal. Hexadecimal is a number system with the base 16.

Now why is Hexadecimal so useful? Because one number in hex represents 1 Nibble in binary. For example 1111 can be written as F. So you only have to learn the first 15 numbers in binary and hex and you easily can write human-readable binary numbers. If you are going to start with reverse engineering and try to reverse assembly code you will encounter the benefits of being able to read hex.

Now how do we represent negative numbers?

There is an excellent method for doing that.

First you have to know that the first bit of a stored number always represents if the number is positive or negative. If the first bit is a 0 this means that your number is positive (for example 00011). If the first bit is a 1 it means that your number is negative (for example 10001).

The method to make a positive number negative or a negative number positive is called “Two’s complement” and it works in two easy steps.

Let’s say we have this nibble and want to reverse it: 0101.

- Step number one is to invert the number. Therefore 0101 becomes 1010.
- Now you are going to add 1 to the inverted number. Therefore 1010 becomes 1011.

And that is it, you converted the positive number into a negative number.

To check your result you can add your results together and see if the outcome is 0. // (a + (-a) = 0)

_0101 +

_1011

_______

_0000

The actual outcome should be 1000 but because our storage size for bits is predefined and not flexible (in this case 1 nibble (4 bits)) every bit outside our domain won’t be taken into account. Therefore 10000 will be treated as 0000.

I hope you guys have a great day and dive deep into this wonderful and fascinating world of coding.