Find next higher number with same number of 1's in binary form
AND
x&(-x) => gives last set bit from right .
ADDITION
x + ( x & - x ) => sets first unset bit after first continuous set bits from right to left.
XOR
x ^ ( x + ( x & - x ) ) => gives first continuous set bits from right to left with 1 extra set bit after continuous set bits.
DIVISION
( x ^ ( x + ( x & - x ) ) ) / ( x& -x ) => brings continuous set bits to right.
SHIFT RIGHT
( x ^ ( x + ( x & - x ) ) ) / ( x& -x ) >>2 => removes 1 extra set bits and 1 more as single 1 has to be at higher position.
OR
((x+(x&-x))|(((x^(x+(x&-x))))/(x&(-x))>>2)) => gives next higher number .
Comments
Post a Comment