how to rotate bit in a bit shift operators

how to rotate bits using bit shift operators
prasad2315Asked:
Who is Participating?
 
evilrixConnect With a Mentor 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;
	}
}

Open in new window

0
 
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
The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

 
Infinity08Commented:
That's what I showed, no ? Which part wasn't clear ?
0
 
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
All Courses

From novice to tech pro — start learning today.