How computers store negative and positive numbers in memory using only 0s & 1s.

How computers store negative and positive numbers in memory using only 0s & 1s.

In computer science, all data types are transformed into a uniform representation of a bit pattern. A bit is a basic unit of information in computing and digital communications.

The bit represents a logical state with one of two possible values: 
0 | 1.

To store more information, it's possible to groups eight contiguous bits to form a byte according to the modern standard. As a consequence, it possible to represent the sum of the power of two to 8. The amount of possible combinations doubles with each binary digit added, permitting the values 0 through 255.

No hay texto alternativo para esta imagen

MSB stands for Most Significant Bit and LSB for Least Significant Bit, used to identify the bit positions in a binary number.

As image show, to transform the binary number : 10010110 to a decimal number it's possible to sum the equivalent in each position: 128 + 0 + 0 + 16 + 0 + 4 + 2 + 0 = 150.

What is two's complement?

Two's complement is the way almost every computer represent integers.

Two's complement is a mathematical operation on binary numbers as a method to represent a binary signed number in computing. Using N bits, all integers from −(2N − 1) to 2N − 1 − 1 can be represented.

The most significant bit determines the sign of the number and is called the sign bit.

No hay texto alternativo para esta imagen


MSB = 0 ---> Positive number.



No hay texto alternativo para esta imagen


MSB = 1 ---> Negative number.



Conversion from Two's Complement

Let's work with an example, an let imagine how it's possible to show -1:

  1. First, find the equivalent positive number in a binary system:
No hay texto alternativo para esta imagen

2. Then flip each bit of the number and add 1 in the LSB.

No hay texto alternativo para esta imagen

3. Finally, as shown in the image, the MSB is equal to 1; therefore, it represents a negative number; in this case, the binary equivalent of -1 using two's complement method.

No hay texto alternativo para esta imagen


Hope you find this post useful and please feel free to comment, ask, or if you find anything incorrect, let me know.



To view or add a comment, sign in

More articles by Martin M. Saavedra

  • What happens when you type an URL a press ENTER:

    You enter the URL in the browser: This article will use the following URL holbertonschool.com to explain the whole…

  • What is IoT? Main aspects, concerns and trends.

    What is IoT? Throughout this article, it will cover the basis of IoT, its definition, evolution through time, foremost…

  • Python: everything is object

    Python is an interpreted, high level, and general-purpose programming language created by Guido van Rossum; released in…

  • Dynamic Libraries in C

    This post is part of a set of articles exploring different edges of the intricacies of computer science. In this one, I…

  • Worst abuse of the C preprocessor. IOCCC 1986 edition.

    One of the most popular obfuscation contests is the International Obfuscated C Code Contest (abbreviated IOCCC), a…

  • C Static Libraries 101.

    In this post, I will cover Static Libraries in C. Why use libraries, how they work, and how to create them and use them.

  • From hello.c to hello.out. The compiling process.

    This blog post will explain the compilation of source code's intricacies using C source code and the GCC compiler as a…

  • Hard & Symbolic links

    Explaining what are hard and symbolic links, how to create them, and what is the difference between the two. Links in…

  • Explaining ls *.c command in Linux

    The current blog post is going to be a series of technical posts as part of Holberton School curriculum. The goal is to…

  • El primer paso hacia la mejora continua: la metodología de las 5S.

    Un método de 5 pasos para transformar la cultura y el espacio de trabajo de tu empresa, generando productos de mayor…

    1 Comment

Others also viewed

Explore content categories