Skip to content
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
Expand Up @@ -4,29 +4,29 @@
* Author: lukasb1b (https://github.com/lukasb1b)
*/

public class SingleBitOperators {
public class final SingleBitOperators {
/**
* Flip the bit at position 'bit' in 'num'
*/
public static int flipBit(int num, int 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
*/
public static int setBit(int num, int bit) {
public static int setBit(final int num,final int bit) {
return num | (1 << bit);
}
/**
* Clears the bit located at 'bit' from 'num'
*/
public static int clearBit(int num, int bit) {
public static int clearBit(final int num,final int bit) {
return num & ~(1 << bit);
}
/**
* Get the bit located at 'bit' from 'num'
*/
public static int getBit(int num, int bit) {
public static int getBit(final int num,final int bit) {
return ((num >> bit) & 1);
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package com.thealgorithms.bitmanipulation;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class SingleBitOperatorTest {

@Test
public void flipBitTest() {
assertEquals(1, SingleBitOperators.flipBit(3, 1));
assertEquals(11, SingleBitOperators.flipBit(3, 3));
assertEquals(1, SingleBitOperator.flipBit(3, 1));
assertEquals(11, SingleBitOperator.flipBit(3, 3));
}

@Test
public void setBitTest() {
assertEquals(5, SingleBitOperators.setBit(4, 0));
assertEquals(4, SingleBitOperators.setBit(4, 2));
assertEquals(5, SingleBitOperator.setBit(4, 0));
assertEquals(4, SingleBitOperator.setBit(4, 2));
}

@Test
public void clearBitTest() {
assertEquals(5, SingleBitOperators.clearBit(7, 1));
assertEquals(5, SingleBitOperators.clearBit(5, 1));
assertEquals(5, SingleBitOperator.clearBit(7, 1));
assertEquals(5, SingleBitOperator.clearBit(5, 1));
}

@Test
public void getBitTest() {
assertEquals(0, SingleBitOperators.getBit(6, 0));
assertEquals(1, SingleBitOperators.getBit(7, 1));
assertEquals(0, SingleBitOperator.getBit(6, 0));
assertEquals(1, SingleBitOperator.getBit(7, 1));
}
}