forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitRotate.java
More file actions
83 lines (73 loc) · 2.96 KB
/
BitRotate.java
File metadata and controls
83 lines (73 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.thealgorithms.bitmanipulation;
/**
* Utility class for performing circular bit rotations on 32-bit integers.
* Bit rotation is a circular shift operation where bits shifted out on one end
* are reinserted on the opposite end.
*
* <p>This class provides methods for both left and right circular rotations,
* supporting only 32-bit integer operations with proper shift normalization
* and error handling.</p>
*
* @see <a href="https://en.wikipedia.org/wiki/Bit_rotation">Bit Rotation</a>
*/
public final class BitRotate {
/**
* Private constructor to prevent instantiation.
* This is a utility class with only static methods.
*/
private BitRotate() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
/**
* Performs a circular left rotation (left shift) on a 32-bit integer.
* Bits shifted out from the left side are inserted on the right side.
*
* @param value the 32-bit integer value to rotate
* @param shift the number of positions to rotate left (must be non-negative)
* @return the result of left rotating the value by the specified shift amount
* @throws IllegalArgumentException if shift is negative
*
* @example
* // Binary: 10000000 00000000 00000000 00000001
* rotateLeft(0x80000001, 1)
* // Returns: 3 (binary: 00000000 00000000 00000000 00000011)
*/
public static int rotateLeft(int value, int shift) {
if (shift < 0) {
throw new IllegalArgumentException("Shift amount cannot be negative: " + shift);
}
// Normalize shift to the range [0, 31] using modulo 32
shift = shift % 32;
if (shift == 0) {
return value;
}
// Left rotation: (value << shift) | (value >>> (32 - shift))
return (value << shift) | (value >>> (32 - shift));
}
/**
* Performs a circular right rotation (right shift) on a 32-bit integer.
* Bits shifted out from the right side are inserted on the left side.
*
* @param value the 32-bit integer value to rotate
* @param shift the number of positions to rotate right (must be non-negative)
* @return the result of right rotating the value by the specified shift amount
* @throws IllegalArgumentException if shift is negative
*
* @example
* // Binary: 00000000 00000000 00000000 00000011
* rotateRight(3, 1)
* // Returns: -2147483647 (binary: 10000000 00000000 00000000 00000001)
*/
public static int rotateRight(int value, int shift) {
if (shift < 0) {
throw new IllegalArgumentException("Shift amount cannot be negative: " + shift);
}
// Normalize shift to the range [0, 31] using modulo 32
shift = shift % 32;
if (shift == 0) {
return value;
}
// Right rotation: (value >>> shift) | (value << (32 - shift))
return (value >>> shift) | (value << (32 - shift));
}
}