• C

# how to rotate bit in a bit shift operators

how to rotate bits using bit shift operators
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
An example :

unsigned int value = 0x12345678;

will create an unsigned int with this binary value :

0001 0010 0011 0100 0101 0110 0111 1000

You can then use the binary shift operator to shift this value 5 bit positions to the left for example :

value <<= 5;

value will now be (in binary) :

0100 0110 1000 1010 1100 1111 0000 0000

You can also use this :

unsigned int value2 = value << 3;

so that value2 will contain :

0011 0100 0101 0110 0111 1000 0000 0000

Similarly for right shifts ...
0
Author Commented:
how to rotate bits using bit operators
0
Commented:
That's what I showed, no ? Which part wasn't clear ?
0
Senior Software Engineer (Avast)Commented:
Do you mean as the bits drop off one end they are appended back to the other? The shift operators don't work this way but you can implement it yourself by saving the bit that is about to drop off the end, shifting and then appending the bit back on the other end.

Below is a very simple example of doing this with an unsigned char type when left shifting.
``````int main()
{
unsigned char c = 0x01;

int i = 8;
for( ; i ; --i)
{
unsigned char b = c & 0x80;
b >>=7;
c <<= 1;
c |= b;
}
}
``````
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Senior Software Engineer (Avast)Commented:
0
Commented:
unsigned int value = 0x12345678;
value = (value << 5) | (value >> (sizeof(value)*CHAR_BIT-5);
0
Commented:
You could be more precise in your questions next time ;) Makes it easier for us to understand what you want :)

Here's some functions that might be handy :

``````#include <limits.h>

#define BIT_SIZE(type) (sizeof(type) * CHAR_BIT)

#define MASK(bit_cnt) ((((unsigned int) 0x01) << (bit_cnt)) - ((unsigned int) 0x01))

unsigned int rotate_right(unsigned int value, int pos) {
unsigned int ret = (value >> pos) & MASK(BIT_SIZE(unsigned int) - pos);
ret |= (value & MASK(pos)) << (BIT_SIZE(unsigned int) - pos);
return ret;
}

unsigned int rotate_left(unsigned int value, int pos) {
unsigned int ret = (value & MASK(BIT_SIZE(unsigned int) - pos)) << pos;
ret |= (value >> (BIT_SIZE(unsigned int) - pos)) & MASK(pos);
return ret;
}
``````
0
Commented:
Note that most of the extra masking is a bit overkill, but it doesn't hurt to explicitly do it ;)
0
Senior Software Engineer (Avast)Commented:
>> You could be more precise in your questions next time
To be fair, the Q did state 'rotate'.
0
Senior Software Engineer (Avast)Commented:
I probably should add that the code I posted above was meant as a simple example that you could step through to see this in action and it is by no means meant to demonstrate an effective or efficient mechanism for doing this. The examples posted by I8 and ozo are good examples of how to do this.

-Rx.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.