Promotion of byte to int or long

前端 未结 1 620
清酒与你
清酒与你 2020-12-12 06:16

If I have an byte value 00101011 and I want to represent it as int value I have: 00000000000000000000000000101011, what seems to be clear for me. B

相关标签:
1条回答
  • 2020-12-12 07:01

    The byte value 11010100 represents a negative number (-44), because the most significant bit is set. When this undergoes primitive widening conversion, it must still represent the same negative value in the two's complement representation. This done using sign extension. That means that all new bits are the same as the original sign bit.

                               11010100 => -44
    11111111 11111111 11111111 11010100 => -44
    

    If this did not occur, then the sign bit would no longer be a sign bit, and it would be interpreted "normally", and the value would no longer be the same.

    00000000 00000000 00000000 11010100 => 212
    
    0 讨论(0)
提交回复
热议问题