How to get the bit after shifted?

H

howa

e.g.


int i = 5; // i.e. 0000...101

// shift i to the right

int j = i >> 1; // how to get the `one` being shifted?

thanks.
 
M

mlimber

howa said:
e.g.


int i = 5; // i.e. 0000...101

// shift i to the right

int j = i >> 1; // how to get the `one` being shifted?

I think you are asking how to save the LSB of i. If so, the answer is
to use logical-and, but it must be done before the shift:

int k = i & 1; // Save a copy of the LSB
int j = i >> 1; // Now the LSB is gone

Cheers! --M
 
E

Eric

mlimber said:
I think you are asking how to save the LSB of i. If so, the answer is
to use logical-and, but it must be done before the shift:

int k = i & 1; // Save a copy of the LSB
int j = i >> 1; // Now the LSB is gone

Cheers! --M

I believe you mean bitwise-and?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top