Rotate by n bit positions

Write a function that rotates (NOT shifts) to the right by n bit positions the bits of an unsigned char no bits are lost in this process.
Your answer should print out the result in binary form 
Your output should be like this:
x = 10100111 (binary)
   x rotated by 3 = 11110100 (binary)


  1. x = ((2^n-1)&n)<<(length(x)-n))|(x>>n)

  2. @Softy342 your solution is not clear can you please explain it..

  3. sorry i meant that we will create a mask , here it is n so mask = 2^n - 1 (n 1s) , which will be anded with x to get the n lsbs whcih will be shifted by length of the x - n , then it will be ORRED with the x shifted by n ...just a pseudo code..not the actual c psot wht you think..!

  4. @softy342 it will work fine..:)


Post a Comment

Popular posts from this blog