I'm creating a mask and setting higher bits in a short like this:
enum FLAGS {FLAG1, FLAG2, FLAG3, FLAG4, FLAG5, FLAG6};
public static void setFlag(short len, FLAGS flag) {
short mask = 1 << (Short.SIZE - flag.ordinal() - 1);
len |= mask;
}
I printed the values:
len: 0000001111111100
mask : 1000000000000000
after OR'ing with mask: 11111111111111111000001111111100
I understand that when we do bit manipulation on shorts they're upgrded to int to avoid overflow, but why are all the higher bits set then? How can I simply set any of the first 6 bits without any funny casting?