-128 as binary literal in Java

后端 未结 2 1412
暗喜
暗喜 2021-01-12 02:04

Based on the fact that a byte type in java is a signed 8 bit two\'s complement integer, why doesn\'t the second way of declaring a byte work?

b         


        
2条回答
  •  无人共我
    2021-01-12 02:41

    0b10000000 is an int literal (= 0b00000000000000000000000010000000) which equals to +128. byte holds 8 bits and cannot represent +128. However, you can achieve this as follows:

    byte notok = (byte) 0b10000000;
    

提交回复
热议问题