Skip to content

Commit e5d33f3

Browse files
lukasb1bvil02
andauthored
Add SingleBitOperations (#4415)
* SingleBitOperators * Tests * Update SingleBitOperatorTest.java * Update SingleBitOperators.java * Update SingleBitOperators.java * Update SingleBitOperators.java * Update SingleBitOperatorTest.java * deliting files * Update SingleBitOperators.java * Update SingleBitOperatorTest.java * Update SingleBitOperators.java * Update SingleBitOperators.java * Update SingleBitOperatorTest.java * Update SingleBitOperatorTest.java * Update and rename SingleBitOperators.java to SingleBitOperator.java * Update SingleBitOperatorTest.java * Update SingleBitOperator.java * Update SingleBitOperator.java * Update SingleBitOperator.java * style: declare private default constructor * fix: remove `SetBitTest.java` * Update and rename SingleBitOperator.java to SingleBitOperations.java * Update SingleBitOperatorTest.java * Update SingleBitOperations.java * Update and rename SingleBitOperatorTest.java to SingleBitOperationsTest.java --------- Co-authored-by: Piotr Idzik <[email protected]>
1 parent ea0eef1 commit e5d33f3

File tree

6 files changed

+66
-47
lines changed

6 files changed

+66
-47
lines changed

src/main/java/com/thealgorithms/bitmanipulation/ClearBit.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/main/java/com/thealgorithms/bitmanipulation/SetBit.java

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/*
4+
* Author: lukasb1b (https://github.com/lukasb1b)
5+
*/
6+
7+
public final class SingleBitOperations {
8+
private SingleBitOperations() {
9+
}
10+
/**
11+
* Flip the bit at position 'bit' in 'num'
12+
*/
13+
public static int flipBit(final int num, final int bit) {
14+
return num ^ (1 << bit);
15+
}
16+
/**
17+
* Set the bit at position 'bit' to 1 in the 'num' variable
18+
*/
19+
public static int setBit(final int num, final int bit) {
20+
return num | (1 << bit);
21+
}
22+
/**
23+
* Clears the bit located at 'bit' from 'num'
24+
*/
25+
public static int clearBit(final int num, final int bit) {
26+
return num & ~(1 << bit);
27+
}
28+
/**
29+
* Get the bit located at 'bit' from 'num'
30+
*/
31+
public static int getBit(final int num, final int bit) {
32+
return ((num >> bit) & 1);
33+
}
34+
}

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

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

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class SingleBitOperationsTest {
8+
9+
@Test
10+
public void flipBitTest() {
11+
assertEquals(1, SingleBitOperations.flipBit(3, 1));
12+
assertEquals(11, SingleBitOperations.flipBit(3, 3));
13+
}
14+
15+
@Test
16+
public void setBitTest() {
17+
assertEquals(5, SingleBitOperations.setBit(4, 0));
18+
assertEquals(4, SingleBitOperations.setBit(4, 2));
19+
}
20+
21+
@Test
22+
public void clearBitTest() {
23+
assertEquals(5, SingleBitOperations.clearBit(7, 1));
24+
assertEquals(5, SingleBitOperations.clearBit(5, 1));
25+
}
26+
27+
@Test
28+
public void getBitTest() {
29+
assertEquals(0, SingleBitOperations.getBit(6, 0));
30+
assertEquals(1, SingleBitOperations.getBit(7, 1));
31+
}
32+
}

0 commit comments

Comments
 (0)