I am trying to create a CRC32 of a string in java. I was able to do that with java.util.zip.CRC32. Now I want to update 1st bit of the crc to 0 or 1 based on some conditions. Can anyone tell me how to do this?
This should help for the highest bit (it is not clear which one is the first one):
long crc = crc32.getValue();
crc &= 0x7FFFFFFF; //sets the highest bit to 0
if (yourCondition) {
crc |= 0x80000000; //sets the highest bit to 1
}
You can use bitmasking to clear bit 1 first and the add in the bit you want.
CRC32 crc = ...;
boolean condition = ...;
int crc32 = (int) crc.getValue();
int bit = condition ? 1 : 0;
crc32 = (crc32 & ~1) | bit;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With