How to set/unset a bit at specific position of a long in Java ?
For example,
long l = 0b001100L ; // bit representation
I want to s
I would choose BigInteger for this...
class Test {
public static void main(String[] args) throws Exception {
Long value = 12L;
BigInteger b = new BigInteger(String.valueOf(value));
System.out.println(b.toString(2) + " " + value);
b = b.setBit(1);
b = b.clearBit(2);
value = Long.valueOf(b.toString());
System.out.println(b.toString(2) + " " + value);
}
}
and here is the output:
1100 12
1010 10