Java有符号转无符号
Q:Java中byte为有符号字节,byte num = -3,求无符号byte的n为多少?
A:-3原码为 1000 0011,反码(符号位不变,其余取反)为 1111 1100,补码(反码+1)为 1111 1101 ,如果没有符号位,那么 1111 1101 = 253,也就是答案为253。
class Solution{ public static void main(String[] args){ byte num = -3; int r = Byte.toUnsignedInt(num); System.out.println(r); //输出253的int } } //java源码: public static int toUnsignedInt(byte x) { return ((int) x) & 0xff; }