开发者

Converting hex to binary and one and two's complement in 16 bits

开发者 https://www.devze.com 2023-04-10 11:15 出处:网络
I am trying to convert FFAD (hex) to a decimal value and then do one and two\'s complement on it. FFAD is represented as a 16 bit integer.

I am trying to convert FFAD (hex) to a decimal value and then do one and two's complement on it. FFAD is represented as a 16 bit integer. When I convert FFAD to base 2 I get 1111111110101101.

My problem is how I know if it is a negative number or not?

I have the binary, now to do ones complement normally I would change the last bit from a 0 to a 1 and then flip all the bits, but as a 16开发者_StackOverflow bit integer I don't have any more available bits. Since the 16th bit is a 1 does that mean it is a negative number? How would I do the complement of that? I am just all around confused with this problem and any pointers would be greatly appreciated.


Its negative because it is left padded with ones. Left padding with zeros would indicate a positive number. For example, here are the decimal values for three bit numbers:

  • 011 = 3
  • 010 = 2
  • 001 = 1
  • 000 = 0
  • 111 = -1
  • 110 = -2
  • 101 = -3
  • 100 = -4

Notice, numbers that are left padded with ones are negative and numbers that are left padded with zeros are positive.

Standard procedure for 2's compliment is to negate your bits, and then add 1.

1111111110101101 becomes 0000000001010010. Then you add 1 to get 0000000001010011.

0

精彩评论

暂无评论...
验证码 换一张
取 消