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).
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
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:
Decimal number 45 in its binary representation
And invert it which becomes:
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:
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.
- https://www.electronics-tutorials.ws/binary/signed-binary-numbers.html