Remove SetKthBit
in favor of SingleBitOperations.setBit
(#4991)
This commit is contained in:
parent
6a0c0585e4
commit
092ac5795b
@ -1,22 +0,0 @@
|
||||
package com.thealgorithms.bitmanipulation;
|
||||
|
||||
/***
|
||||
* Sets the kth bit of a given integer to 1
|
||||
* e.g. setting 3rd bit in binary of 17 (binary 10001) gives 25 (binary 11001)
|
||||
* @author inishantjain
|
||||
*/
|
||||
|
||||
public class SetKthBit {
|
||||
/**
|
||||
* Sets the kth bit of a given integer.
|
||||
*
|
||||
* @param num The original integer.
|
||||
* @param k The position of the bit to set (0-based index).
|
||||
* @return The integer with the kth bit set.
|
||||
*/
|
||||
public static int setKthBit(int num, int k) {
|
||||
int mask = 1 << k;
|
||||
num = num | mask;
|
||||
return num;
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package com.thealgorithms.bitmanipulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SetKthBitTest {
|
||||
|
||||
@Test
|
||||
void testSetKthBit() {
|
||||
// Test case: Setting the 0th bit in 5 (binary 101)
|
||||
assertEquals(5, SetKthBit.setKthBit(5, 0));
|
||||
|
||||
// Test case: Setting the 2nd bit in 10 (binary 1010)
|
||||
assertEquals(14, SetKthBit.setKthBit(10, 2));
|
||||
|
||||
// Test case: Setting the 3rd bit in 15 (binary 1111)
|
||||
assertEquals(15, SetKthBit.setKthBit(15, 3));
|
||||
|
||||
// Test case: Setting the 1st bit in 0 (binary 0)
|
||||
assertEquals(2, SetKthBit.setKthBit(0, 1));
|
||||
}
|
||||
}
|
@ -1,32 +1,36 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
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));
|
||||
assertEquals(5, SingleBitOperations.setBit(5, 0));
|
||||
assertEquals(14, SingleBitOperations.setBit(10, 2));
|
||||
assertEquals(15, SingleBitOperations.setBit(15, 3));
|
||||
assertEquals(2, SingleBitOperations.setBit(0, 1));
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user