I have a question about how two's complement works when casting an int to a byte. For instance, when casting an int whose value is between 127 and 256, I get negative values, and I'd like to understand how. I get that the high-order bits get thrown out, so anything between 256 and 512 results in the byte variable holding the int value minus 256; but I'm curious about how to work out the negative values in binary.
For instance, I'm curious about how the following piece of code works:
int i = 140;
byte b = (byte) i;
System.out.println(b); // outputs -116
140 is 10001100 in binary, and in two's complement binary from what I gather. In binary, how does 10001100 become the two's complement binary value necessary to output -116?