Matthew L.
asked 11/03/21Why is signed char 0xff = -1?
I don't understand it very well.
1 Expert Answer
Inactive Tutor answered 11/04/21
Signed integers are represented in two's complement. It's easier to think about this in terms of bits vs. the hexadecimal equivalent, so let's consider the decimal integers +127 and -127 represented by 8 bits:
The sign is stored in the high-ordered bit for integers--a 0 in the high-ordered bit indicates a positive number whereas a 1 would indicate the value is a negative number. That's why a signed integer stored in 8 bits has a decimal range of -128 to +127. +127 has all bits turned on except the high-ordered bit. If we added 1 to this value, the high-ordered bit would turn on indicating a -128 signed integer. If we add one bit to get the two's complement, we would end up with -127 (even though the unsigned decimal conversion would be 129)
How do we find the two's complement of a particular number? One way is to get the one's complement and then add 1 to it...getting the two's complement using the integer +2 as an example:
Note that the addition of one to the ones column carries over to the twos column to the left of it.
Now, let's look a the bits for the decimal integers +1 and -1:
And let's see how working through the one's complement "plus one" to arrive at the two's complement:
That is a short explanation on why 0xFF (or 1111 1111) is -1 as a signed integer.
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Inactive Tutor
This implies that it is coded in 2's complement. the leading 1 means it is negative. To convert it to positive, you invert all the bits ~0xff = 0x0, then add 1. So the number is -1 in 2's complement form.12/22/21