File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments