Casting long to byte in Java
问题 I am unable to understand the following: In java, long l = 130L; byte b = (byte)l; If I print the value of b, why do I get -126? What is the bit representation of long l? 回答1: Bytes are signed in Java - so the range of values is -128 to 127 inclusive. The bit pattern for 130 as a long, when simply truncated to 8 bits, is the bit pattern for -126 as a byte. As another example: int x = 255; byte b = (byte) x; // b is now -1 回答2: A byte is a sequence of 8 bits, which makes 2^8 cases = 256. Half