To check if a the j-th bit of a number i is set,
we can see if ( i & 1 << j ) is non-zero.
(1 << j) means left shift …or in simple terms find 2^j.
To be precise if the bit is set, then the result would be 2^j.
This is because in the number 2^j there is only 1-bit and that is the j-th bit,
eg:2^1=2 Binary–>10 only "1"st bit is set(0 based from right)…(Set means bit is “1”)
if it’s ANDing with i results in a non-zero number then the j-th bit is set in i.
Anding --> a & b is 1 only wen both bit positions are 1…
Take an example:–>
To check if 3 rd bit of Number 11010 is set or not
find 2^3=8 Binary–>01000
Anding with that number itself
Anding we get
Which is non zero …Hence its set…
Take same number.And find weather bit 2 is set or not
11010 && 2^2
which is zero hence bit is not set(Reset)…
Using this concept u can easily code in java using bitwise operators…(& and << Bitwise operators)
Hope This helps…