yes, bit mask can suppress sign extension, as it changes the number from negative to the positive, you see:
byte b = -1;
char c = (char)(b & 0xff);
b is originally a byte, and it's negative, after it bitwise AND with the bit mask 0xff, it then becomes positive and thus will no longer be sign extended.
yes, as 0xff is an int primitive type, int fact, it is 0x000000ff, it's not a signed int. that it bitwise AND b would get the result into the int type, and then a narrowing primitive conversion to char.
the process:
1. byte b is promoted to int, after bitwise AND, it becomes 0x000000ff
2. after casting to char, it becomes 0x00ff which is 255
the operands would be promoted to an int, cause there's a law that all arithmetic operations do this to operands of type short, byte or char. this promotion is a widening primitive conversion, so no information is lost.
if it is operated like this without the bit mask:
byte b = -1;
char c = (char)b;
because byte is a signed type, sign extension occurs when converting the byte value –1 to a char . The resulting char value has all 16 bits set, so it is equal to 216 – 1, or 65,535.
referenced by JavaPuzzler Solution 31 and Puzzle 6 Multicast