Skip to content

Commit 636ee0c

Browse files
feat: Add ToggleNthBit algo with JUnit tests
1 parent 4a03f42 commit 636ee0c

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java)
4646
* [SingleElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleElement.java)
4747
* [SwapAdjacentBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java)
48+
* [ToggleNthBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ToggleNthBit.java)
4849
* [TwosComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/TwosComplement.java)
4950
* ciphers
5051
* a5
@@ -676,6 +677,7 @@
676677
* [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java)
677678
* [SingleElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleElementTest.java)
678679
* [SwapAdjacentBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java)
680+
* [ToggleNthBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ToggleNthBitTest.java)
679681
* [TwosComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java)
680682
* ciphers
681683
* a5
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* <pre>
5+
* This class provides a method to toggle the nth bit of an integer.
6+
*
7+
* Toggling a bit means flipping it: changing a 0 to a 1 or a 1 to a 0.
8+
*
9+
* The algorithm works as follows:
10+
*
11+
* Let's say we have an integer x and we want to toggle its nth bit.
12+
*
13+
* Step 1: Create a bitmask with a 1 at the nth position.
14+
* - Example: If n = 2, the bitmask will be 1 << 2 = 4 (0100 in binary).
15+
*
16+
* Step 2: XOR the integer x with the bitmask.
17+
* - Example: If x = 5 (0101 in binary) and the bitmask is 4 (0100 in binary),
18+
* then x ^ bitmask = 5 ^ 4 = 1 (0001 in binary).
19+
*
20+
* After these two steps, the nth bit of x is toggled.
21+
*
22+
* For more information, refer to the
23+
* <a href="https://en.wikipedia.org/wiki/Bit_manipulation">Bit manipulation</a>.
24+
*
25+
* <b>Example usage:</b>
26+
* <code>
27+
* int result = ToggleNthBit.toggleNthBit(5, 2);<br>
28+
* System.out.println("Result after toggling 2nd bit: " + result);
29+
* </code>
30+
* </pre>
31+
*/
32+
public final class ToggleNthBit {
33+
private ToggleNthBit() {
34+
}
35+
36+
/**
37+
* Toggles the nth bit of an integer.
38+
*
39+
* @param x the integer whose nth bit is to be toggled
40+
* @param n the position of the bit to be toggled (0-based index)
41+
* @return the integer with the nth bit toggled
42+
*/
43+
public static int toggleNthBit(int x, int n) {
44+
return x ^ (1 << n);
45+
}
46+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
/**
8+
* Unit tests for the ToggleNthBit class.
9+
*/
10+
public class ToggleNthBitTest {
11+
12+
/**
13+
* Test the toggleNthBit method with a positive number.
14+
*/
15+
@Test
16+
public void testToggleNthBitPositive() {
17+
int result = ToggleNthBit.toggleNthBit(5, 2);
18+
assertEquals(1, result); // 5 (0101) with 2nd bit toggled becomes 1 (0001)
19+
}
20+
21+
/**
22+
* Test the toggleNthBit method with a negative number.
23+
*/
24+
@Test
25+
public void testToggleNthBitNegative() {
26+
int result = ToggleNthBit.toggleNthBit(-5, 2);
27+
assertEquals(-1, result); // -5 (1111...1011) with 2nd bit toggled becomes -1 (1111...1111)
28+
}
29+
30+
/**
31+
* Test the toggleNthBit method with zero.
32+
*/
33+
@Test
34+
public void testToggleNthBitZero() {
35+
int result = ToggleNthBit.toggleNthBit(0, 2);
36+
assertEquals(4, result); // 0 (0000) with 2nd bit toggled becomes 4 (0100)
37+
}
38+
39+
/**
40+
* Test the toggleNthBit method with the same bit toggled twice.
41+
*/
42+
@Test
43+
public void testToggleNthBitTwice() {
44+
int result = ToggleNthBit.toggleNthBit(5, 2);
45+
result = ToggleNthBit.toggleNthBit(result, 2);
46+
assertEquals(5, result); // Toggling the same bit twice should return the original number
47+
}
48+
}

0 commit comments

Comments
 (0)