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 x.ie 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)

Comments

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

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

    ReplyDelete
  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 code..do psot wht you think..!

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

    ReplyDelete

Post a Comment

Popular posts from this blog