Understanding two’s complement2 min read
Have you ever wondered why, in Java and other languages, one byte can only represent to as the maximum value and why cannot we have . There is indeed a reason for that, in Java, some primitive types (int, short, long, byte) are signed two’s complement. In this article, we will learn how two’s complement can be used to represent both positive and negative decimal values in the binary representation.
Given a positive number, two’s complement is an operation to reverse this number into a negative number, and the most significant bit (the leftmost one) indicates whether this number is positive or negative (0 means positive, and 1 means negative). For example, I have the number 7 and want to find the binary representation of its negative, i.e. -7. Here is the general procedure (to make it less verbose, we only use 4 bits to represent our binary number):
- Get the binary representation of the number 7 =>
- Flip all the bits (0 becomes 1 and 1 becomes 0) =>
- Add 1 to the flipped value =>
That’s how you get the number -7 in its binary form. Along the way, we have the number 7 with the binary value of since it’s two’s complement value, we need to add an extra 0 at the most significant bit to signify it’s a positive value.
Here, to verify that 1001 is indeed -7 in the decimal format, we don’t calculate the decimal representation as we usually do, but instead, we will add all the bits up but subtract the most significant bit:
Now we have some knowledge of how two’s complement works, we can come back to the initial question of why Java can only represent 127 as the maximum value instead of 128 for the byte data type. Because each byte contains 8 bits and bytes in Java are two’s complements, meaning that we have to reserve 1 bit for the sign, and the rest 7 bits represent the magnitude, here will represent -1, not 128. Going backward, we can still represent -128 as the lower bound as we know is indeed -128.
To find the decimal representation of any two’s complement value, we can use this general formula:
Or if the formula above looks complicated, we also can use this procedure to compute the decimal value for a two’s complement; let’s say we want to convert to decimal:
- First, we find the complement of by flipping all the bits, which is
- Add one to it,
- Convert to decimal, which is 4
- Apply the sign that we have at the beginning; the result we get is -4.