How can one read an integer bit by bit in Java?

Using bitwise operators:

int getBit(int n, int k) {
    return (n >> k) & 1;
}

Explanation (in bits):

n
100010101011101010 (example)
n >> 5
000001000101010111 (all bits are moved over 5 spots, therefore
&                   the bit you want is at the end)
000000000000000001 (0 means it will always be 0,
=                   1 means that it will keep the old value)
1

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)