Computer storage for numbers
Tags: c, binary, mathematics, programming
Published: 2020-03-29 05:34:28


Computers use a binary system (0 and 1) since they are managed by electricity, This means that using the current of electricity instructions are given which is expressed with a 1 a `YES` and with a 0 a `NO`. In this way, computers will process information more quickly.

***
                                1 == "YES" && 0 == "NO"
***

## How we represent numbers on the computer?

To get a clear idea about binary numbers and how to convert binary numbers to decimal numbers ([This link is useful to understand](linkhttps://okdiario.com/howto/como-pasar-binario-decimal-2044589)) since you have a clear concept of how numbers are represented mathematically with 1 and 0 let’s go with the examples.

10 is equivalent to 1010 then the computer needs to represent that 1010, so to speak to be easy to understand the machine opens several boxes to keep that number, that number is not only 1010, 1010 the machine will see it reflected as a `0000000000001010`, this happens because the computer handles a 34-bit system that would be our 34 boxes to store our 10, In conclusion, we have a number 10 that we need the computer to recognize then this opens a space of 34 boxes (bits) so that she can store the number 10.

An important fact of this binary number is that the last number will take it if it is negative or positive, this means that if we have a number like this `11111111110110` means that we will have a number (-10) later we will explain the -10 why.

## Mathematical operations

Mathematical operations with binaries are important to clarify the last subject of two’s complement, then if we add two binary numbers this is going to be the result:

```c
  0000000000001010 (10)
+ 0000000000001010 (10)
----------------
  0000000000010100 = (20)
```
```c
  0000000000000101 (5)
+ 0000000000001010 (10)
----------------
  0000000000001111 = (15)
```
Now to the subtraction of binaries, we know that a negative number represented in binary the last digit will have (1)
```c
  0000000000000101 (5)
+ 1000000000001010 (-10)
----------------
  1000000000001111 = (32783)
```
The result does not give us because it is not the way we represent the binary numbers, so if we need to pass our number to negative we need to revert the 1 and 0 of our original number, dare an example to make the topic clearer

```c
  0110 (6) ---> 1001 --> (-6)
```
This is called one complement, but then if we do the operation again

```c
  0000000000000101 (5)
+ 1000000000001001 (-6)
----------------
  0000000000001101 = (13)
```
is still failing and is not accurate, so we need the two’s complement so that the operation can be accurate. then I will give an example of how a number is represented
```c
  0011 (3) ---> 1101 --> (-3)
```
```c
  0000000000000101 (5)
+ 1111111111111010 (-6)
----------------
  1111111111111111 = (-1)
```
we realize that then the mathematical operation if it gives so that the computer to do mathematical operations with negative numbers, has to represent them differently to be able to store them in memory and interact with them.

The following picture is graphically how the computer can save negative values.

![This image is from https://www.log2base2.com/storage/how-integers-are-stored-in-memory.html ](https://www.log2base2.com/images/storage/negative-integer-storage.png)

The following video will help you understand the subject if you have doubts.
{% youtube 4qH4unVtJkE %}

[This link ](https://www.youtube.com/watch?v=4qH4unVtJkE) show mathematically why two’s complement in more detail

this link take the topic more [deeper ](https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php)