I am trying to implement a rotate left function that rotates an integer x left by n bits
I find a way for doing a rotate that can be useful when you work with some bit who the size is fixed and you knew it:
int rotate_bit_left(int bit_rotating){
int LastMax = 64;
bit_rotating = (bit_rotating>=LastMax)? ((bit_rotating << 1 ) | 0x01) : bit_rotating << 1 ;
return bit_rotating;
/*
Here LastMax is 64 because in the exemple I work in the Ascii table where the max is 0111 1111 and this value can be adapted with the value of the last one bit in decimale
*/
}
This function can be changed to a right rotate
int rotate_bit_right(int bit_rotating){
bit_rotating = ((bit_rotating%2)==1)? ((bit_rotating >> 1)| 0x80 ): bit_rotating >> 1 ;
return bit_rotating;
/*
Because if is a odd number then the last bit is one and we have to put it in the first place
*/
}
Notice that 0x01 and 0x80 must be changed if your worked in an other case than Ascii table to Hexa number with a pattern like this : 0...0001 for the left rotate 100....0 for the right rotate