Question

What am I looking for?

Let's consider int a = 5 for example

Its binary is : 101

So when we do

a >> 1

then the rightmost bit, i.e., 1 in this case, would drop off, I want to catch it in some variable..i.e.,

1st iteration k = 1 
2nd iteration k = 0 
3rd iteration k = 1

In this case I have a hard-coded value but it can be any arbitrary user input for number.

If I do b = a >> 1 then b= a/2 so m clueless here!

Why am I asking this question?

For example, take program for conversion of decimal to binary, in that case, if I can catch the falling bit, then the program would be done in 3-4 lines max...

there are off course, other logic’s for which I need to know if it's possible!

Was it helpful?

Solution

Store the lowest bit in a variable before shifting, e.g.,

int a = 5;
int lowestBit = a & 1;
a >>= 1;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top