mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
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 <65706193+vil02@users.noreply.github.com>
This commit is contained in:
@ -1,11 +0,0 @@
|
|||||||
package com.thealgorithms.bitmanipulation;
|
|
||||||
/**
|
|
||||||
* Clears the bit located at clear from num
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class ClearBit {
|
|
||||||
public static int clearBit(int num, int clear) {
|
|
||||||
int mask = ~(1 << clear);
|
|
||||||
return num & mask;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package com.thealgorithms.bitmanipulation;
|
|
||||||
/**
|
|
||||||
* Sets a specific bit to 1
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class SetBit {
|
|
||||||
public static int setBit(int num, int bit) {
|
|
||||||
return num | (1 << bit);
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.thealgorithms.bitmanipulation;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Author: lukasb1b (https://github.com/lukasb1b)
|
||||||
|
*/
|
||||||
|
|
||||||
|
public final class SingleBitOperations {
|
||||||
|
private SingleBitOperations() {
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Flip the bit at position 'bit' in 'num'
|
||||||
|
*/
|
||||||
|
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(final int num, final int bit) {
|
||||||
|
return num | (1 << bit);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Clears the bit located at 'bit' from 'num'
|
||||||
|
*/
|
||||||
|
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(final int num, final int bit) {
|
||||||
|
return ((num >> bit) & 1);
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
package com.thealgorithms.bitmanipulation;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
public class ClearBitTest {
|
|
||||||
@Test
|
|
||||||
public void clearBitTest() {
|
|
||||||
assertEquals(5, ClearBit.clearBit(7, 1));
|
|
||||||
assertEquals(5, ClearBit.clearBit(5, 1));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package com.thealgorithms.bitmanipulation;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
class SetBitTest {
|
|
||||||
@Test
|
|
||||||
void testSetBit() {
|
|
||||||
assertEquals(5, SetBit.setBit(4, 0));
|
|
||||||
assertEquals(3, SetBit.setBit(3, 1));
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.thealgorithms.bitmanipulation;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class SingleBitOperationsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void flipBitTest() {
|
||||||
|
assertEquals(1, SingleBitOperations.flipBit(3, 1));
|
||||||
|
assertEquals(11, SingleBitOperations.flipBit(3, 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setBitTest() {
|
||||||
|
assertEquals(5, SingleBitOperations.setBit(4, 0));
|
||||||
|
assertEquals(4, SingleBitOperations.setBit(4, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void clearBitTest() {
|
||||||
|
assertEquals(5, SingleBitOperations.clearBit(7, 1));
|
||||||
|
assertEquals(5, SingleBitOperations.clearBit(5, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getBitTest() {
|
||||||
|
assertEquals(0, SingleBitOperations.getBit(6, 0));
|
||||||
|
assertEquals(1, SingleBitOperations.getBit(7, 1));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user