int a = 412; byte c = (byte) a; //-100 int b = c & 0xFF; //156 

Is it possible in this case to get the original 412?

  • 8 bits can only take 256 different values. - zRrr
  • Well, as I understand it, if more than 256 a number appears with - (-100 I don’t know how) - Nick Kulese
  • It does not always work negative. Only when, when circumcised, the most significant bit will be = 1 - Oleksiy Morenets

1 answer 1

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.