Enhance docs, add more tests in SingleBitOperations (#5860)

This commit is contained in:
Hardik Pawar
2024-10-26 14:44:20 +05:30
committed by GitHub
parent 647a82a997
commit ef7f2e97e3
2 changed files with 88 additions and 29 deletions

View File

@ -1,34 +1,68 @@
package com.thealgorithms.bitmanipulation;
/*
/**
* A utility class for performing single-bit operations on integers.
* These operations include flipping, setting, clearing, and getting
* individual bits at specified positions.
*
* Bit positions are zero-indexed (i.e., the least significant bit is at position 0).
* These methods leverage bitwise operations for optimal performance.
*
* Examples:
* - `flipBit(3, 1)` flips the bit at index 1 in binary `11` (result: `1`).
* - `setBit(4, 0)` sets the bit at index 0 in `100` (result: `101` or 5).
* - `clearBit(7, 1)` clears the bit at index 1 in `111` (result: `101` or 5).
* - `getBit(6, 0)` checks if the least significant bit is set (result: `0`).
*
* Time Complexity: O(1) for all operations.
*
* Author: lukasb1b (https://github.com/lukasb1b)
*/
public final class SingleBitOperations {
private SingleBitOperations() {
}
/**
* Flip the bit at position 'bit' in 'num'
* Flips (toggles) the bit at the specified position.
*
* @param num the input number
* @param bit the position of the bit to flip (0-indexed)
* @return the new number after flipping the specified bit
*/
public static int flipBit(final int num, final int bit) {
return num ^ (1 << bit);
}
/**
* Set the bit at position 'bit' to 1 in the 'num' variable
* Sets the bit at the specified position to 1.
*
* @param num the input number
* @param bit the position of the bit to set (0-indexed)
* @return the new number after setting the specified bit to 1
*/
public static int setBit(final int num, final int bit) {
return num | (1 << bit);
}
/**
* Clears the bit located at 'bit' from 'num'
* Clears the bit at the specified position (sets it to 0).
*
* @param num the input number
* @param bit the position of the bit to clear (0-indexed)
* @return the new number after clearing the specified bit
*/
public static int clearBit(final int num, final int bit) {
return num & ~(1 << bit);
}
/**
* Get the bit located at 'bit' from 'num'
* Gets the bit value (0 or 1) at the specified position.
*
* @param num the input number
* @param bit the position of the bit to retrieve (0-indexed)
* @return 1 if the bit is set, 0 otherwise
*/
public static int getBit(final int num, final int bit) {
return ((num >> bit) & 1);
return (num >> bit) & 1;
}
}

View File

@ -2,35 +2,60 @@ package com.thealgorithms.bitmanipulation;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class SingleBitOperationsTest {
class SingleBitOperationsTest {
@Test
public void flipBitTest() {
assertEquals(1, SingleBitOperations.flipBit(3, 1));
assertEquals(11, SingleBitOperations.flipBit(3, 3));
@ParameterizedTest
@MethodSource("provideFlipBitTestCases")
void testFlipBit(int input, int bit, int expected) {
assertEquals(expected, SingleBitOperations.flipBit(input, bit));
}
@Test
public void setBitTest() {
assertEquals(5, SingleBitOperations.setBit(4, 0));
assertEquals(4, SingleBitOperations.setBit(4, 2));
assertEquals(5, SingleBitOperations.setBit(5, 0));
assertEquals(14, SingleBitOperations.setBit(10, 2));
assertEquals(15, SingleBitOperations.setBit(15, 3));
assertEquals(2, SingleBitOperations.setBit(0, 1));
private static Stream<Arguments> provideFlipBitTestCases() {
return Stream.of(Arguments.of(3, 1, 1), // Binary: 11 -> 01
Arguments.of(3, 3, 11) // Binary: 11 -> 1011
);
}
@Test
public void clearBitTest() {
assertEquals(5, SingleBitOperations.clearBit(7, 1));
assertEquals(5, SingleBitOperations.clearBit(5, 1));
@ParameterizedTest
@MethodSource("provideSetBitTestCases")
void testSetBit(int input, int bit, int expected) {
assertEquals(expected, SingleBitOperations.setBit(input, bit));
}
@Test
public void getBitTest() {
assertEquals(0, SingleBitOperations.getBit(6, 0));
assertEquals(1, SingleBitOperations.getBit(7, 1));
private static Stream<Arguments> provideSetBitTestCases() {
return Stream.of(Arguments.of(4, 0, 5), // 100 -> 101
Arguments.of(4, 2, 4), // 100 -> 100 (bit already set)
Arguments.of(0, 1, 2), // 00 -> 10
Arguments.of(10, 2, 14) // 1010 -> 1110
);
}
@ParameterizedTest
@MethodSource("provideClearBitTestCases")
void testClearBit(int input, int bit, int expected) {
assertEquals(expected, SingleBitOperations.clearBit(input, bit));
}
private static Stream<Arguments> provideClearBitTestCases() {
return Stream.of(Arguments.of(7, 1, 5), // 111 -> 101
Arguments.of(5, 1, 5) // 101 -> 101 (bit already cleared)
);
}
@ParameterizedTest
@MethodSource("provideGetBitTestCases")
void testGetBit(int input, int bit, int expected) {
assertEquals(expected, SingleBitOperations.getBit(input, bit));
}
private static Stream<Arguments> provideGetBitTestCases() {
return Stream.of(Arguments.of(6, 0, 0), // 110 -> Bit 0: 0
Arguments.of(7, 1, 1) // 111 -> Bit 1: 1
);
}
}