开发者

2's complement question

开发者 https://www.devze.com 2023-02-27 03:08 出处:网络
I have a q related to 2\'s complement. Say I have a signed 16 bit hexadecimal in 2\'s complement representation

I have a q related to 2's complement. Say I have a signed 16 bit hexadecimal in 2's complement representation for example, take 0xfaa

i开发者_JAVA百科f it's 16 bit, i need to expand it cause it only has 12 bits now. I know i have to expand the left side, so it'd be made into 0xffaa.

that means that my number is negative, is that correct?


If I understand your question, then you are correct.

If it's a 12-bit two's complement negative number, then you take the high-order bit -- bit 11, as it's usually called -- and copy it into all bits to its left to fill the high-order 4 bits of its 16-bit equivalent. The 12-bit value you have -- 0xfaa -- becomes the 16-bit value 0xffaa, just as you have said in your question.

In binary, the 12-bit value is

    111110101010
    ^
    +---- bit 11 

and becomes the 16-bit value

 1111111110101010
 ^   ^
 |   +-------- bit 11 
 +------------ bit 15

That the two words are of different lengths in no way changes the fact that the two numbers are equal.


You would make the most significant digit a 0, so it would be 0x0faa. If you assume and f goes there you have changed the value of the number.

0

精彩评论

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