# Coding Interview Question: Rotate Bits ## Question

Given a number, write a function to rotate the bits (ie circular shift).

eg.

```rotate(0xFFFF0000, 8) = 0x00FFFF00
rotate(0x13579BDF, 12) = 0xBDF13579
rotate(0b10110011100011110000111110000000, 17) = 0b00011111000000010110011100011110```

Once you think that you’ve solved the problem, click below to see the solution.

As always, remember that practicing coding interview questions is as much about how you practice as the question itself. Make sure that you give the question a solid go before skipping to the solution. Ideally if you have time, write out the solution first by hand and then only type it into your computer to verify your work once you've verified it manually. To learn more about how to practice, check out this blog post.

## Solution

How was that problem? You can check out the solution in the video below.

Here is the source code for the solution shown in the video:

```private static final int BITS_IN_INTEGER = 32;

public int rotateClockwise(int x, int N) {
N = N % BITS_IN_INTEGER;
return (x >> N | x << (BITS_IN_INTEGER - N))
}

public int rotateCounterClockwise(int x, int N) {
N = N % BITS_IN_INTEGER;
return (x << N | x >> (BITS_IN_INTEGER - N))
}```  