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.
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.
MSB = 0 ---> Positive number.
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:
- First, find the equivalent positive number in a binary system:
2. Then flip each bit of the number and add 1 in the LSB.
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.
Hope you find this post useful and please feel free to comment, ask, or if you find anything incorrect, let me know.