We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
You can get k-1 if pos <= n is TRUE. And you can get pos by ((k-1) | (k-1+1)) , that is , ((k-1) | k). Otherwise , you just need to follow the process above when k is ODD (because k-1 is ODD) , then you get the answer k-2.
In brief , you can just do the test ((k-1) | k) <= n to determine the answer.
Day 29: Bitwise AND
You are viewing a single comment's thread. Return to all comments →
When k is ODD , k-1 is EVEN , k-1 can always be reached by
(k-1) & k
.That is ,
((k-1) | k)
is alwaysk
. And((k-1) | k) <= n
is always TRUE.When k is EVEN , k-1 is ODD , k-1 can only be reached if and only if
((k-1) | k) <= n
is TRUEWhy?
You can get k-1 if
pos <= n
is TRUE. And you can get pos by((k-1) | (k-1+1))
, that is ,((k-1) | k)
. Otherwise , you just need to follow the process above when k is ODD (because k-1 is ODD) , then you get the answer k-2.In brief , you can just do the test
((k-1) | k) <= n
to determine the answer.