When I was trying to learn from an existing program, I could not understand what the following two lines of code try to do?
for(i=0;0==(x&1);++i)x>>=1;
if(0==(x-=y)) return y<<i;
Any explanations would be appreciated.
for(i=0;0==(x&1);++i)x>>=1
Finds the least significant bit set to 1 in an integer
if(0==(x-=y)) return y<<i;
Subtracts y from x, and if the result is 0, returns y shifted over (toward the more significant bits) by i bits.
for(i=0;0==(x&1);++i)x>>=1;
This code x>>=1 is shifting the bits of x to the right one place. This will continue as long as 0==(x&1) is true, which means that the right-most bit of x is a 0. i is the number of bits shifted.
if(0==(x-=y)) return y<<i;
This code subtracts y from x. Then, if x is 0 the code returns y shifted to the left by i bits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With