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