I have a byte for example:
"10000110"
which is 97
in decimal.
I don't know the correct ordering, some places it is written "01100001"
, the point is that I'm speaking about the decimal number 97 which is stored in a single byte in our app.
We don't need bigger numbers than 100 in our app in this place of data and we would like to store an additional boolean data in the last (8th) bit in the corresponding byte, so..
So this way I will have my boolean data from the 8th bit, and after that I can write the 8th bit to zero, so I can read my byte to get the decimal number. (because we only use values from 0-100 the last bit should always be zero.)
I don't know if it is clear enough, please let me know if you have any questions.
How could I write a 0 to the last bit of a byte in Java?
97 decimal is 61 (hexadecimal) or 1100001 (binary)
If you want to use the high-order bit (10000000 binary or 80 hex) to store other information, you can do this as follows:
byte b = 97;
// Set the high-order bit to 1
byte c = b | 0x80;
// Determine if the high-order bit is set or not
boolean bitIsSet = (c & 0x80) != 0;
// clear the high-order bit (remove the high-order bit to get the value)
byte d = c & 0x7F;
I hope this is clear and answers your question.