Skip to content

Commit ef7f2e9

Browse files
authored
Enhance docs, add more tests in SingleBitOperations (#5860)
1 parent 647a82a commit ef7f2e9

File tree

2 files changed

+88
-29
lines changed

2 files changed

+88
-29
lines changed
Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,68 @@
11
package com.thealgorithms.bitmanipulation;
22

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+
*
419
* Author: lukasb1b (https://github.com/lukasb1b)
520
*/
6-
721
public final class SingleBitOperations {
822
private SingleBitOperations() {
923
}
24+
1025
/**
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
1231
*/
1332
public static int flipBit(final int num, final int bit) {
1433
return num ^ (1 << bit);
1534
}
35+
1636
/**
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
1842
*/
1943
public static int setBit(final int num, final int bit) {
2044
return num | (1 << bit);
2145
}
46+
2247
/**
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
2453
*/
2554
public static int clearBit(final int num, final int bit) {
2655
return num & ~(1 << bit);
2756
}
57+
2858
/**
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
3064
*/
3165
public static int getBit(final int num, final int bit) {
32-
return ((num >> bit) & 1);
66+
return (num >> bit) & 1;
3367
}
3468
}

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

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,60 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
69

7-
public class SingleBitOperationsTest {
10+
class SingleBitOperationsTest {
811

9-
@Test
10-
public void flipBitTest() {
11-
assertEquals(1, SingleBitOperations.flipBit(3, 1));
12-
assertEquals(11, SingleBitOperations.flipBit(3, 3));
12+
@ParameterizedTest
13+
@MethodSource("provideFlipBitTestCases")
14+
void testFlipBit(int input, int bit, int expected) {
15+
assertEquals(expected, SingleBitOperations.flipBit(input, bit));
1316
}
1417

15-
@Test
16-
public void setBitTest() {
17-
assertEquals(5, SingleBitOperations.setBit(4, 0));
18-
assertEquals(4, SingleBitOperations.setBit(4, 2));
19-
assertEquals(5, SingleBitOperations.setBit(5, 0));
20-
assertEquals(14, SingleBitOperations.setBit(10, 2));
21-
assertEquals(15, SingleBitOperations.setBit(15, 3));
22-
assertEquals(2, SingleBitOperations.setBit(0, 1));
18+
private static Stream<Arguments> provideFlipBitTestCases() {
19+
return Stream.of(Arguments.of(3, 1, 1), // Binary: 11 -> 01
20+
Arguments.of(3, 3, 11) // Binary: 11 -> 1011
21+
);
2322
}
2423

25-
@Test
26-
public void clearBitTest() {
27-
assertEquals(5, SingleBitOperations.clearBit(7, 1));
28-
assertEquals(5, SingleBitOperations.clearBit(5, 1));
24+
@ParameterizedTest
25+
@MethodSource("provideSetBitTestCases")
26+
void testSetBit(int input, int bit, int expected) {
27+
assertEquals(expected, SingleBitOperations.setBit(input, bit));
2928
}
3029

31-
@Test
32-
public void getBitTest() {
33-
assertEquals(0, SingleBitOperations.getBit(6, 0));
34-
assertEquals(1, SingleBitOperations.getBit(7, 1));
30+
private static Stream<Arguments> provideSetBitTestCases() {
31+
return Stream.of(Arguments.of(4, 0, 5), // 100 -> 101
32+
Arguments.of(4, 2, 4), // 100 -> 100 (bit already set)
33+
Arguments.of(0, 1, 2), // 00 -> 10
34+
Arguments.of(10, 2, 14) // 1010 -> 1110
35+
);
36+
}
37+
38+
@ParameterizedTest
39+
@MethodSource("provideClearBitTestCases")
40+
void testClearBit(int input, int bit, int expected) {
41+
assertEquals(expected, SingleBitOperations.clearBit(input, bit));
42+
}
43+
44+
private static Stream<Arguments> provideClearBitTestCases() {
45+
return Stream.of(Arguments.of(7, 1, 5), // 111 -> 101
46+
Arguments.of(5, 1, 5) // 101 -> 101 (bit already cleared)
47+
);
48+
}
49+
50+
@ParameterizedTest
51+
@MethodSource("provideGetBitTestCases")
52+
void testGetBit(int input, int bit, int expected) {
53+
assertEquals(expected, SingleBitOperations.getBit(input, bit));
54+
}
55+
56+
private static Stream<Arguments> provideGetBitTestCases() {
57+
return Stream.of(Arguments.of(6, 0, 0), // 110 -> Bit 0: 0
58+
Arguments.of(7, 1, 1) // 111 -> Bit 1: 1
59+
);
3560
}
3661
}

0 commit comments

Comments
 (0)