Storing integers into memory using two’s complement

Storing integers into memory using two’s complement

As we might know by now computers store data types in uniform representations and then transformed back into their original form when retrieved. This universal representation are represented in terms of “0’s” and “1’s”, which are called a bit (Binary digiT).

No hay texto alternativo para esta imagen


With positive numbers we don’t use the plus (+) sign before it but we do need to include the minus (-) sign if we want to represent a negative number.

This doesn’t happen in binaries, as said before, it only handles “0’s” and “1’s”, and so how does the machine interpret negative numbers? Here is where the two’s complement comes in.

“In mathematics, positive numbers (including zero) are represented as unsigned numbers. That is we do not put the +ve sign in front of them to show that they are positive numbers.
However, when dealing with negative numbers we do use a -ve sign in front of the number to show that the number is negative in value” [1]

Unsigned integer storage

Unsigned integers can never be negative thus ranging from 0 to an infinite positive number. The machine stores this numbers in its binary representation and adds 0’s to the left to complete its padding to complete the 8 bits.

So as an example a number like 45 will be represented as 0 0 1 0 1 1 0 1

No hay texto alternativo para esta imagen

Decimal number 45 represented as bynary

Two’s complement

The two’s complement is used to represent negative numbers in its binary form.

So to do this, you start by inversing the binary number which in our example is 170:

Ex:

No hay texto alternativo para esta imagen

Decimal number 45 in its binary representation

And invert it which becomes:

No hay texto alternativo para esta imagen

The inverse of 45 in binary

“In two’s complement representation the leftmost bit defines the sign of the integer. If it is 0, the integer is positive. If it is 1, the integer is negative.”

The next step to finding the two’s complement, is to simply have to add a one (1)to the inverted number.

So our result will be:

No hay texto alternativo para esta imagen

two’s complement of 45 (-45)

This binary representation gives us the equivalent of negative 45 (-45) in binary form.

So in conclusion, it is possible to represent negative numbers in its binary form by following the steps mentioned before, it may not be easy to understand at the beginning but I hope this article will help you and with time you get used to it.


  1. https://www.electronics-tutorials.ws/binary/signed-binary-numbers.html


要查看或添加评论,请登录

Javier Gutiérrez的更多文章

  • In Python everything is an object

    In Python everything is an object

    And to be more exact an immutable or mutable one. Introduction As the title says everything in Python is an object that…

  • The differences between static and dynamic libraries.

    The differences between static and dynamic libraries.

    Hello again, here we are writing and reading about libraries once again, in this case I will be explaining you the…

  • What happens when you type ls -l in the shell?

    What happens when you type ls -l in the shell?

    The result and output of using the “ls” command with the long format option it may be easy to explain but what’s behind…

  • Static libraries, what are they, how they work?

    Static libraries, what are they, how they work?

    To better understand what a static library is we should first define a library. There are two types of libraries in c…

  • What happens when you type gcc main.c

    What happens when you type gcc main.c

    To have a better understanding of what gcc does it is important to explain how a computer works. Computers don’t with…

社区洞察

其他会员也浏览了