Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching the "dropping" bit value

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!

like image 970
NoobEditor Avatar asked Apr 23 '14 15:04

NoobEditor


1 Answers

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

int a = 5;
int lowestBit = a & 1;
a >>= 1;
like image 56
Arkku Avatar answered Nov 03 '22 00:11

Arkku