To answer the question in the title, to set the 32nd bit of an int
you can use the bitwise OR operator:
myInteger |= (1 << 31);
If you don't want this to "mess up" the string representation, that's an issue of how you convert the integer into string. For example the Integer.toHexString
method treats ints as unsigned:
print(Integer.toHexString(-3)); // output: fffffffd
UPDATE: Use the unsigned left shift >>>
when formatting numbers: The regular left shift >>
extends the sign bit, so that a left shift of a negative number stays negative and doesn't "suddenly" become a huge positive number.
return String.format("%x %x %x %x", entryDataHI >>> 16, entryDataHI & 0xFFFF,
entryDataLO >>> 16, entryDataLO & 0xFFFF);
Alternatively you can use masking which you already seem to be familiar with:
return String.format("%x %x %x %x", (entryDataHI >> 16) & 0xFFFF, entryDataHI & 0xFFFF,
(entryDataLO >> 16) & 0xFFFF, entryDataLO & 0xFFFF);