Representing a signed 8-bit integer?
IF F6 base hex is a signed 8-bit integer, how much开发者_JAVA百科 does it represent in decimal?
F6
is the binary value 11110110
.
In a Two's complement system, the first bit is used to indicate the sign. If it's 0, the remaining 7 digits represent 0-127. If it's 1, then you use bitwise not to flip the other 7 bits, then add 1 and negate the result.
So:
11110110 // Negative, because the first position is 1
1110110 // Removed the leading 1
0001001 // Flip the remaining 7 bits
8 + 1 // Convert bits to decimal values (bits 4 and 1 from the right)
9 + 1 = 10 // Add the 1
Therefore, F6 is -10.
Assuming a "normal" 2's complement, it's -10. In 1's complement it would be -11. In signed magnitude it would be -118.
You can think of 2's Compliment like this...the first bit is considered "negative" so in an 8-bit number, normally you'd add it up like this for unsigned integers.
11011011
= 1*(2^7)+1*(2^6)+0*(2^5)+1*(2^4)+1*(2^3)+0*(2^2)+1*(2^1)+1*(2^0) = 219
With 2's Compliment...
11011011
= -1*(2^7)+1*(2^6)+0*(2^5)+1*(2^4)+1*(2^3)+0*(2^2)+1*(2^1)+1*(2^0) = -37
^ Note the negative
The first bit is considered negative, in 8-bit this means 2^7 if set is negative and the rest is added to the negative to make it less negative. If all bits are 1, then it will be -1.
The answer is -10.
Yet another way to think about two's complement numbers. Adding or removing 2 power number of bits change nothing. Then for any number x: x = x + 256 = x - 256 (or x + n * 256 for any n).
Now, when you get a number, you must follow some convention to find out the right n. The convention for 2's complement is just : if the most significant bit is 1 then it's a negative number (n=-1), if it's 0 it's a positive number (n=0).
Practically you just convert x to it's decimal value.
F6 = 15*16+6 = 246.
As it's greater that 128 (decimal value of the most significant bit), you remove 256.
F6 = -10 as others also found.
精彩评论