Why is absolute of Integer.MIN_VALUE equivalent to Integer.MIN_VALUE

前端 未结 6 1280
感动是毒
感动是毒 2020-12-31 12:12

In java when I say Integer i = Math.abs(Integer.MIN_VALUE). I get the same value as the answer,which means i contains Integer.MIN_VALUE

6条回答
  •  礼貌的吻别
    2020-12-31 12:56

    This is because of the way two's-complement number systems work. Integer.MIN_VALUE corresponds to 0x80000000. The standard way to negate it is to take its ones' complement (0x7FFFFFFF in this case) and add 1, and in this case it would overflow back to 0x80000000.

提交回复
热议问题