What method would you use to determine if the the bit that represents 2^x is a 1 or 0 ?
Bitwise AND Operator (&) is a binary operator, which operates on two operands and checks the bits, it returns 1, if both bits are SET (HIGH) else returns 0. Here, NUM is the number whose bit you want to check and N is the bit number, (1<<N) SET the particular bit at Nth position.
Method 1 (Using Left Shift Operator) 1) Left shift given number 1 by k-1 to create a number that has only set bit as k-th bit. temp = 1 << (k-1) 2) If bitwise AND of n and temp is non-zero, then result is SET else result is NOT SET.
Determine if all even place bits (counting from left to right) are set to 1. For instance, 0101 0101 would count whereas 1011 1000 would not count. If the the bit has 1's in all even places, return 1, or else return 0.
Bitwise AND operator. 00001000, which is equal to 8 in decimal. decides whether the bit was ON or OFF.
I'd use:
if ((value & (1L << x)) != 0) { // The bit was set }
(You may be able to get away with fewer brackets, but I never remember the precedence of bitwise operations.)
Another alternative:
if (BigInteger.valueOf(value).testBit(x)) { // ... }
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