int a = 412; byte c = (byte) a; //-100 int b = c & 0xFF; //156 Is it possible in this case to get the original 412?
int a = 412; byte c = (byte) a; //-100 int b = c & 0xFF; //156 Is it possible in this case to get the original 412?
412 is 32 bits 0000_0000_0000_0000_0000_0001_1001_1100 in binary format. Reduction to a byte will take the right byte (8 bits), i.e. 1001_1100 This is -100 (decimal) How to translate - invert all bits and add 1 (in binary form, of course). 0110_0011 + 1 = 0110_0100 - this will be the modulus of your negative number. Open in Windows calculator (Programmer) and experiment there)) Well, bringing back to 32 bits will give the same set of bits, complemented by zeros on the left: 0000_0000_0000_0000_0000_0000_1001_1100 is the number 156. And the FF mask in this case is superfluous. You still do not get more than 8 bits from a byte.
Source: https://ru.stackoverflow.com/questions/921788/
All Articles