mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
Add Clear Bit (#4355)
This commit is contained in:
@ -0,0 +1,11 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user