|
1 | 1 | package com.thealgorithms.bitmanipulation;
|
2 | 2 |
|
3 |
| -/* |
| 3 | +/** |
| 4 | + * A utility class for performing single-bit operations on integers. |
| 5 | + * These operations include flipping, setting, clearing, and getting |
| 6 | + * individual bits at specified positions. |
| 7 | + * |
| 8 | + * Bit positions are zero-indexed (i.e., the least significant bit is at position 0). |
| 9 | + * These methods leverage bitwise operations for optimal performance. |
| 10 | + * |
| 11 | + * Examples: |
| 12 | + * - `flipBit(3, 1)` flips the bit at index 1 in binary `11` (result: `1`). |
| 13 | + * - `setBit(4, 0)` sets the bit at index 0 in `100` (result: `101` or 5). |
| 14 | + * - `clearBit(7, 1)` clears the bit at index 1 in `111` (result: `101` or 5). |
| 15 | + * - `getBit(6, 0)` checks if the least significant bit is set (result: `0`). |
| 16 | + * |
| 17 | + * Time Complexity: O(1) for all operations. |
| 18 | + * |
4 | 19 | * Author: lukasb1b (https://github.com/lukasb1b)
|
5 | 20 | */
|
6 |
| - |
7 | 21 | public final class SingleBitOperations {
|
8 | 22 | private SingleBitOperations() {
|
9 | 23 | }
|
| 24 | + |
10 | 25 | /**
|
11 |
| - * Flip the bit at position 'bit' in 'num' |
| 26 | + * Flips (toggles) the bit at the specified position. |
| 27 | + * |
| 28 | + * @param num the input number |
| 29 | + * @param bit the position of the bit to flip (0-indexed) |
| 30 | + * @return the new number after flipping the specified bit |
12 | 31 | */
|
13 | 32 | public static int flipBit(final int num, final int bit) {
|
14 | 33 | return num ^ (1 << bit);
|
15 | 34 | }
|
| 35 | + |
16 | 36 | /**
|
17 |
| - * Set the bit at position 'bit' to 1 in the 'num' variable |
| 37 | + * Sets the bit at the specified position to 1. |
| 38 | + * |
| 39 | + * @param num the input number |
| 40 | + * @param bit the position of the bit to set (0-indexed) |
| 41 | + * @return the new number after setting the specified bit to 1 |
18 | 42 | */
|
19 | 43 | public static int setBit(final int num, final int bit) {
|
20 | 44 | return num | (1 << bit);
|
21 | 45 | }
|
| 46 | + |
22 | 47 | /**
|
23 |
| - * Clears the bit located at 'bit' from 'num' |
| 48 | + * Clears the bit at the specified position (sets it to 0). |
| 49 | + * |
| 50 | + * @param num the input number |
| 51 | + * @param bit the position of the bit to clear (0-indexed) |
| 52 | + * @return the new number after clearing the specified bit |
24 | 53 | */
|
25 | 54 | public static int clearBit(final int num, final int bit) {
|
26 | 55 | return num & ~(1 << bit);
|
27 | 56 | }
|
| 57 | + |
28 | 58 | /**
|
29 |
| - * Get the bit located at 'bit' from 'num' |
| 59 | + * Gets the bit value (0 or 1) at the specified position. |
| 60 | + * |
| 61 | + * @param num the input number |
| 62 | + * @param bit the position of the bit to retrieve (0-indexed) |
| 63 | + * @return 1 if the bit is set, 0 otherwise |
30 | 64 | */
|
31 | 65 | public static int getBit(final int num, final int bit) {
|
32 |
| - return ((num >> bit) & 1); |
| 66 | + return (num >> bit) & 1; |
33 | 67 | }
|
34 | 68 | }
|
0 commit comments