Looking at the int 44 — I need Math.CEIL (log(2) 44)
of binary places to represent 44
.
(answer is 6 places)
6 places :
___ ___ ___ ___ ___ ___
32 16 8 4 2 1
But how can I check that (for example) the bit of 8
is checked or not ?
A simple solution will be do to :
((1<<3) & 44)>0
so this will check if the bit is set.
But please notice that behind the scenes the computer translates 44
to its binary representation and just check if bit is set via bitwise operation.
Another solution is just to build the binary myself via toString(2)
or mod%2
in a loop
Question
Mathematically Via which formula, I can test if n'th
bit is set ?
(I would prefer a non loop operation but pure single math phrase)