Skip to content

refactor: Enhance docs, add more tests in SingleBitOperations #5860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,60 @@

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
);
}
}