In Java I created a byte consisting of only 'on' bits, so:
byte a = (byte) 0xff;
System.out.println(a);
However, when this prints I get the result -1 instead of 255 (=15*16+15). I understand that the first bit in an integer is for the sign, but integers are 32 bit while this byte is only 8 bit.
If I use the following code, the expected results is given:
byte a = (byte) 0xff;
System.out.println(a & 0xff);
This really surprises me! How can a bitwise and operation of a byte with itself suddenly return a different result? Can anybody explain this effect?