I\'m trying to set bits in Java byte variable. It does provide propper methods like .setBit(i). Does anybody know how I can realize this?
I can iterate
The technique you need is to isolate the chosen bit and either set or clear it. You already have the expression to isolate the bit since you're using that to test it above. You can set the bit by ORing it in, or clear the bit by bitwise AND with the 1's complement of the bit.
boolean setBit;
my_byte = setBit
? myByte | (1 << i)
: myByte & ~(1 << i);