• C

how to rotate bit in a bit shift operators

how to rotate bits using bit shift operators
prasad2315Asked:
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.

Infinity08Commented:
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
prasad2315Author Commented:
how to rotate bits using bit operators
0
Infinity08Commented:
That's what I showed, no ? Which part wasn't clear ?
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

evilrixSenior 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;
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
ozoCommented:
unsigned int value = 0x12345678;
value = (value << 5) | (value >> (sizeof(value)*CHAR_BIT-5);
0
Infinity08Commented:
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;
}

Open in new window

0
Infinity08Commented:
Note that most of the extra masking is a bit overkill, but it doesn't hurt to explicitly do it ;)
0
evilrixSenior Software Engineer (Avast)Commented:
>> You could be more precise in your questions next time
To be fair, the Q did state 'rotate'.
0
evilrixSenior 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.