In: Computer Science
Rotating bits in C
This rotates bits to the left which means the bits that were shifted off are then moved to the right side.
I need B5DE1FAA to become 7787EAAD
uiOrig = B5DE1FAA
k = 6
unsigned int rotateLeft(unsigned int uiOrig, int k){
}
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly
indented for better understanding.
#include <stdio.h>
unsigned int rotateLeft(unsigned int uiOrig, int k)
{
// left shift the bits k times.
unsigned int shiftedResult= (uiOrig<<k);
// now take the last k bits from original number and move it to
front using
// right shift >> operator and | or operator
// here 8 is the number of bits in a byte.
shiftedResult |= uiOrig >> (sizeof(uiOrig)*8 - k);
// return the obtained result
return shiftedResult;
}
int main()
{
// test the method
unsigned int uiOrig=0xB5DE1FAA;
int k=6;
unsigned int shiftedResult=rotateLeft(uiOrig,k);
printf("The Result is: %X",shiftedResult);
return 0;
}
==========
SCREENSHOT: