Skip to content

Commit 1a77cf5

Browse files
Yajunesh M RYajunesh M R
authored andcommitted
feat: Add BitRotate utility for circular bit rotations
1 parent 3976ad2 commit 1a77cf5

2 files changed

Lines changed: 24 additions & 25 deletions

File tree

src/main/java/com/thealgorithms/bitmanipulation/BitRotate.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Utility class for performing circular bit rotations on 32-bit integers.
55
* Bit rotation is a circular shift operation where bits shifted out on one end
66
* are reinserted on the opposite end.
7-
*
7+
*
88
* <p>This class provides methods for both left and right circular rotations,
99
* supporting only 32-bit integer operations with proper shift normalization
1010
* and error handling.</p>
@@ -29,24 +29,24 @@ private BitRotate() {
2929
* @param shift the number of positions to rotate left (must be non-negative)
3030
* @return the result of left rotating the value by the specified shift amount
3131
* @throws IllegalArgumentException if shift is negative
32-
*
33-
* @example
32+
*
33+
* @example
3434
* // Binary: 10000000 00000000 00000000 00000001
35-
* rotateLeft(0x80000001, 1)
35+
* rotateLeft(0x80000001, 1)
3636
* // Returns: 3 (binary: 00000000 00000000 00000000 00000011)
3737
*/
3838
public static int rotateLeft(int value, int shift) {
3939
if (shift < 0) {
4040
throw new IllegalArgumentException("Shift amount cannot be negative: " + shift);
4141
}
42-
42+
4343
// Normalize shift to the range [0, 31] using modulo 32
4444
shift = shift % 32;
45-
45+
4646
if (shift == 0) {
4747
return value;
4848
}
49-
49+
5050
// Left rotation: (value << shift) | (value >>> (32 - shift))
5151
return (value << shift) | (value >>> (32 - shift));
5252
}
@@ -59,25 +59,25 @@ public static int rotateLeft(int value, int shift) {
5959
* @param shift the number of positions to rotate right (must be non-negative)
6060
* @return the result of right rotating the value by the specified shift amount
6161
* @throws IllegalArgumentException if shift is negative
62-
*
62+
*
6363
* @example
64-
* // Binary: 00000000 00000000 00000000 00000011
64+
* // Binary: 00000000 00000000 00000000 00000011
6565
* rotateRight(3, 1)
6666
* // Returns: -2147483647 (binary: 10000000 00000000 00000000 00000001)
6767
*/
6868
public static int rotateRight(int value, int shift) {
6969
if (shift < 0) {
7070
throw new IllegalArgumentException("Shift amount cannot be negative: " + shift);
7171
}
72-
72+
7373
// Normalize shift to the range [0, 31] using modulo 32
7474
shift = shift % 32;
75-
75+
7676
if (shift == 0) {
7777
return value;
7878
}
79-
79+
8080
// Right rotation: (value >>> shift) | (value << (32 - shift))
8181
return (value >>> shift) | (value << (32 - shift));
8282
}
83-
}
83+
}

src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.bitmanipulation;
22

33
import static org.junit.jupiter.api.Assertions.*;
4+
45
import org.junit.jupiter.api.Test;
56

67
/**
@@ -23,10 +24,10 @@ void testRotateLeftBasic() {
2324
void testRotateLeftWithCarry() {
2425
// Test bits carrying from left to right
2526
// Binary: 10000000_00000000_00000000_00000001
26-
int value = 0x80000001;
27+
int value = 0x80000001;
2728
// After left rotate by 1: 00000000_00000000_00000000_00000011
2829
assertEquals(3, BitRotate.rotateLeft(value, 1));
29-
30+
3031
// Binary: 11000000_00000000_00000000_00000000
3132
value = 0xC0000000;
3233
// After left rotate by 1: 10000000_00000000_00000000_00000001
@@ -74,7 +75,7 @@ void testRotateRightWithCarry() {
7475
int value = 3;
7576
// After right rotate by 1: 10000000_00000000_00000000_00000001
7677
assertEquals(0x80000001, BitRotate.rotateRight(value, 1));
77-
78+
7879
// Binary: 00000000_00000000_00000000_00000001
7980
value = 1;
8081
// After right rotate by 1: 10000000_00000000_00000000_00000000
@@ -146,16 +147,14 @@ void testRotateAllZeros() {
146147
@Test
147148
void testRotateLeftNegativeShift() {
148149
// Negative shifts should throw IllegalArgumentException
149-
Exception exception = assertThrows(IllegalArgumentException.class,
150-
() -> BitRotate.rotateLeft(42, -1));
150+
Exception exception = assertThrows(IllegalArgumentException.class, () -> BitRotate.rotateLeft(42, -1));
151151
assertTrue(exception.getMessage().contains("negative"));
152152
}
153153

154154
@Test
155155
void testRotateRightNegativeShift() {
156156
// Negative shifts should throw IllegalArgumentException
157-
Exception exception = assertThrows(IllegalArgumentException.class,
158-
() -> BitRotate.rotateRight(42, -5));
157+
Exception exception = assertThrows(IllegalArgumentException.class, () -> BitRotate.rotateRight(42, -5));
159158
assertTrue(exception.getMessage().contains("negative"));
160159
}
161160

@@ -166,10 +165,10 @@ void testRotateLeftRightComposition() {
166165
// Rotating left then right by same amount should return original value
167166
int original = 0x12345678;
168167
int shift = 7;
169-
168+
170169
int leftRotated = BitRotate.rotateLeft(original, shift);
171170
int restored = BitRotate.rotateRight(leftRotated, shift);
172-
171+
173172
assertEquals(original, restored);
174173
}
175174

@@ -178,10 +177,10 @@ void testRotateRightLeftComposition() {
178177
// Rotating right then left by same amount should return original value
179178
int original = 0x9ABCDEF0;
180179
int shift = 13;
181-
180+
182181
int rightRotated = BitRotate.rotateRight(original, shift);
183182
int restored = BitRotate.rotateLeft(rightRotated, shift);
184-
183+
185184
assertEquals(original, restored);
186185
}
187186

@@ -191,4 +190,4 @@ void testRotateLeft31IsSameAsRotateRight1() {
191190
int value = 0x55555555;
192191
assertEquals(BitRotate.rotateLeft(value, 31), BitRotate.rotateRight(value, 1));
193192
}
194-
}
193+
}

0 commit comments

Comments
 (0)