1. The Binary Representation of Positive and Negative Numbers
1.1 The binary representation of the positive number 5 :
Assuming we have an int
type number with a value of 5, we know that its representation in a computer would typically be a 32-bit binary value.
In this case, the binary representation of the number 5 would be:
00000000 00000000 00000000 00000101
Please note that this representation assumes a little-endian byte order, where the least significant bit is stored first.
1.2 The binary representation of the negative number -5:
The binary representation of the negative number -5, in a typical 32-bit signed integer representation, would be:
11111111 11111111 11111111 11111011
This representation follows the two’s complement method, where the most significant bit (leftmost bit) is set to 1 to indicate a negative value. The remaining bits represent the magnitude of the number in binary form. When converted to hexadecimal, it becomes “0xFFFFFFFB
“.
Why ?
Negative numbers in computers are typically represented using two’s complement notation.
In two’s complement representation, to express a negative number like -5, you follow these steps:
- Start with the binary representation of the corresponding positive number (5 in this case), which is “
00000000 00000000 00000000 00000101
“. - Invert all the bits (change 0s to 1s and 1s to 0s): “
11111111 11111111 11111111 11111010
“. - Add 1 to the result obtained in step 2: “
11111111 11111111 11111111 11111011
“.
So, the correct binary representation of -5 in a 32-bit signed integer format using two’s complement is “11111111 11111111 11111111 11111011
“.
2. Tips
- Positive numbers have the same representation in both the one’s complement and two’s complement systems.
- In the one’s complement representation, the negative number’s one’s complement is obtained by flipping all the bits (excluding the sign bit) of its corresponding positive number.
- In the two’s complement representation, the negative number’s two’s complement is obtained by taking the one’s complement of its corresponding positive number and then adding 1 to the least significant bit.