Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3f11d73

Browse files
author
Ram swaroop
committed
rotate bits done
1 parent cc00807 commit 3f11d73

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package me.ramswaroop.bits;
2+
3+
/**
4+
* Created by IntelliJ IDEA.
5+
*
6+
* @author: ramswaroop
7+
* @date: 6/8/15
8+
* @time: 5:12 PM
9+
* <p/>
10+
* A ROTATION (OR CIRCULAR SHIFT) is an operation similar to
11+
* shift except that the bits that fall off at one end are put
12+
* back to the other end.
13+
* <p/>
14+
* For example, 000…11100101 becomes 00..0011100101000 if the number
15+
* is rotated 3 times towards left and becomes 101000..0011100 if the
16+
* number is rotated 3 times towards right.
17+
*/
18+
public class RotateBits {
19+
20+
public static int leftRotateBits(int n, int times) {
21+
return n << times | n >>> (32 - times);
22+
}
23+
24+
public static int rightRotateBits(int n, int times) {
25+
return n >>> times | n << (32 - times);
26+
}
27+
28+
public static void main(String a[]) {
29+
System.out.println(leftRotateBits(5, 3));
30+
System.out.println(leftRotateBits(234324, 3));
31+
System.out.println(rightRotateBits(5, 3));
32+
System.out.println(rightRotateBits(234324, 3));
33+
}
34+
}

0 commit comments

Comments
 (0)