Overflowing Short in java

前端 未结 4 1667
太阳男子
太阳男子 2020-11-30 15:23

I have one question about the short data type in Java. I know that the range for short is between -32768 to 32767.

So, if I tried to add two short value

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 15:58

        0111 0101 0011 0000
      + 0111 0101 0011 0000
      ______________________
        1110 1010 0110 0000 
    

    Java's short is encoded in two's complement. In two's complement the Most Significant Bit is considered as the sign bit, 0 is positive and 1 and negative.

    1110 1010 0110 0000 = -5536 in two's complement
    

提交回复
热议问题