Two's complement and negative numbers
Written by Maximiliano Alonso for Holberton School project.

Two's complement and negative numbers

Introduction

In mathematics, when we need to write the sign of a number we put the sign character at the beginning of the number, more specifically we use unsigned numbers to represent the positive ones and we put the minus (-) character at the beginning to represent the negatives ones.

For example, if we want to write the number three on its positive and negative form, we should write:

No hay texto alternativo para esta imagen

However, in digital circuits there is no provision made to put a plus or even a minus sign character to a number, since digital systems operate with binary numbers that are represented in terms of “0’s” and “1’s”. When used together in microelectronics, these “1’s” and “0’s”, called a bit (being a contraction of BInary digiT), fall into several range sizes of numbers which are referred to by common names, such as a byte or a word.

Following the example, if we want to write the number three in binary on its positive form (for example in 4 bits) we should write:

No hay texto alternativo para esta imagen

But what happen when we need to write the negative form of the number in binary? As I said before, there is not the possibility to write the number on this way:

No hay texto alternativo para esta imagen

What we see above is a wrong expression, so here is when we start to talk about the two’s complement.

Two’s Complement 

Two's complement is a mathematical operation on binary numbers, and is an example of a radix complement. It is used in computer science as a method of signed number representation.

This method uses the Most Significant Bit to indicate if the numbers is positive or negative, when the MSB is “1”, the number is signed as negative.

In our example the MSB is the fourth bit:

No hay texto alternativo para esta imagen

Two's complement is the most common method of representing signed integers on computers, and more generally, fixed point binary values. In this scheme, if the binary number 0011 encodes the signed integer 3, then its two's complement, 1101, encodes the inverse: −3. In other words, the sign of most integers (all but one of them) can be reversed in this scheme by taking the two's complement of its binary representation.

So, let’s see how it works.

Conversion to Two's Complement

Following the example, we were trying to represent the number three on its negative form in binary so there are a series of steps to follow in order to do that.

1st step: write the positive form of the number in binary.

No hay texto alternativo para esta imagen

2nd step: flip all the “0’s” and “1’s”.

No hay texto alternativo para esta imagen

3rd step: add one to the previous number.

No hay texto alternativo para esta imagen

So, this is how the number three on its negative form would be written in binary.

Let’s take a look to our number:

No hay texto alternativo para esta imagen

If we pay attention, the MSB is “1” and it is used to indicate the (-) sign of the number, but if we do the arithmetic with the value, we got the -3 result as we wanted (- 8 + 4 + 1 = - 3). 

Inverting and adding one it's actually just a mathematical shortcut of a rather straightforward computation.

Arithmetic with Two's Complement

One of the nice properties of two's complement is that addition and subtraction is made very simple. With a system like two's complement, the circuitry for addition and subtraction can be unified, whereas otherwise they would have to be treated as separate operations.

Let’s see it with an example:

No hay texto alternativo para esta imagen

For example, let’s add 2 to 2

No hay texto alternativo para esta imagen

Now, let’s subtract 4 from 4, wich is exactly the same that adding – 4 to 4.

No hay texto alternativo para esta imagen

As we can see the arithmetic works correctly.

In summary we have talked about what is the two's complement, how it works, how we can get negatives numbers in binary using it and how addition and subtraction works perfectly.

Thank you for taking the time of reading this article, I hope you had found it interesting and helpful.

To view or add a comment, sign in

More articles by Maximiliano Alonso

  • PIN! - Plan It Now

    When we had to choose a project to develop as MVP, we wanted to create something that would be useful to satisfy a need…

    1 Comment
  • What happens when you type google.com in your browser and press enter?

    Introduction Have you ever wondered what happens when you type google.com in your browser and press enter? In this…

  • IoT - Internet of Things

    What is IoT? IoT stands for Internet of things, but what do we refer when we say the internet of the things? This…

  • What is recursion?

    Introduction I clearly remember the first time I saw the recursion concept in Holberton. We were learning how to…

  • Mutable, Immutable... everything is object!

    Introduction In my previous article I talked about object-oriented programming and what classes and instances are. But…

  • Class and instance attributes

    Before I start talking about classes and instances, I would like to share a brief introduction about what…

    1 Comment
  • Dynamic libraries vs Static libraries

    In my previous article, I talked about what a library is in C, why we use them, and specifically how to create and how…

  • What happens when you type `ls -l *.c` in the shell?

    What happens when you type `ls -l *.c` in the Shell If we had had to answer this question ten days ago, without…

    5 Comments
  • C - Static libraries

    In computer science, a library is a collection of non-volatile resources used by computer programs, often for software…

  • Compilation process in c - What happens when you type gcc main.c?

    In this blog I’m going to explain you the compilation process in C, how many steps it has and how it works. But before…

    1 Comment

Others also viewed

Explore content categories