Add Clear Bit (#4355)

This commit is contained in:
Lukas 2023-09-07 20:16:02 +02:00 committed by GitHub
parent 29a864b5b3
commit fbef4023d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

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

View File

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