Skip to content

Commit 795c134

Browse files
Monk-AbhinayVermaChiefpatwal
authored andcommitted
Add Ones & Twos Complement in BitManipulation (TheAlgorithms#5604)
1 parent ab447d9 commit 795c134

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* @author - https://github.com/Monk-AbhinayVerma
5+
* @Wikipedia - https://en.wikipedia.org/wiki/Ones%27_complement
6+
* The class OnesComplement computes the complement of binary number
7+
* and returns
8+
* the complemented binary string.
9+
* @return the complimented binary string
10+
*/
11+
public final class OnesComplement {
12+
private OnesComplement() {
13+
}
14+
15+
// Function to get the 1's complement of a binary number
16+
public static String onesComplement(String binary) {
17+
StringBuilder complement = new StringBuilder();
18+
// Invert each bit to get the 1's complement
19+
for (int i = 0; i < binary.length(); i++) {
20+
if (binary.charAt(i) == '0') {
21+
complement.append('1');
22+
} else {
23+
complement.append('0');
24+
}
25+
}
26+
return complement.toString();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* @wikipedia - https://en.wikipedia.org/wiki/Two%27s_complement
5+
* This Algorithm was first suggested by Jon Von Neumann
6+
* @author - https://github.com/Monk-AbhinayVerma
7+
* @return the two's complement of any binary number
8+
*/
9+
public final class TwosComplement {
10+
private TwosComplement() {
11+
}
12+
13+
// Function to get the 2's complement of a binary number
14+
public static String twosComplement(String binary) {
15+
StringBuilder onesComplement = new StringBuilder();
16+
// Step 1: Find the 1's complement (invert the bits)
17+
for (int i = 0; i < binary.length(); i++) {
18+
if (binary.charAt(i) == '0') {
19+
onesComplement.append('1');
20+
} else {
21+
onesComplement.append('0');
22+
}
23+
}
24+
// Step 2: Add 1 to the 1's complement
25+
StringBuilder twosComplement = new StringBuilder(onesComplement);
26+
boolean carry = true;
27+
for (int i = onesComplement.length() - 1; i >= 0; i--) {
28+
if (onesComplement.charAt(i) == '1' && carry) {
29+
twosComplement.setCharAt(i, '0');
30+
} else if (onesComplement.charAt(i) == '0' && carry) {
31+
twosComplement.setCharAt(i, '1');
32+
carry = false;
33+
}
34+
}
35+
// If there is still a carry, append '1' at the beginning
36+
if (carry) {
37+
twosComplement.insert(0, '1');
38+
}
39+
return twosComplement.toString();
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
* Test case for Highest Set Bit
9+
* @author Abhinay Verma(https://github.com/Monk-AbhinayVerma)
10+
*/
11+
public class OnesComplementTest {
12+
13+
@Test
14+
public void testOnesComplementAllZeroes() {
15+
16+
// Test cases with all-zero binary strings
17+
assertEquals("1111", OnesComplement.onesComplement("0000"));
18+
assertEquals("111", OnesComplement.onesComplement("000"));
19+
assertEquals("11", OnesComplement.onesComplement("00"));
20+
assertEquals("1", OnesComplement.onesComplement("0"));
21+
}
22+
23+
@Test
24+
public void testOnesComplementAllOnes() {
25+
// Test cases with all-one binary strings
26+
assertEquals("0000", OnesComplement.onesComplement("1111"));
27+
assertEquals("000", OnesComplement.onesComplement("111"));
28+
assertEquals("00", OnesComplement.onesComplement("11"));
29+
assertEquals("0", OnesComplement.onesComplement("1"));
30+
}
31+
32+
@Test
33+
public void testOnesComplementMixedBits() {
34+
// Test more mixed binary patterns
35+
assertEquals("1010", OnesComplement.onesComplement("0101"));
36+
assertEquals("0101", OnesComplement.onesComplement("1010"));
37+
assertEquals("1100", OnesComplement.onesComplement("0011"));
38+
assertEquals("0011", OnesComplement.onesComplement("1100"));
39+
assertEquals("1001", OnesComplement.onesComplement("0110"));
40+
}
41+
42+
@Test
43+
public void testOnesComplementEmptyString() {
44+
// Test empty string scenario
45+
assertEquals("", OnesComplement.onesComplement(""));
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
* Test case for Highest Set Bit
9+
* @author Abhinay Verma(https://github.com/Monk-AbhinayVerma)
10+
*/
11+
public class TwosComplementTest {
12+
13+
@Test
14+
public void testTwosComplementAllZeroes() {
15+
// Test with a binary number consisting entirely of zeroes
16+
assertEquals("10000", TwosComplement.twosComplement("0000"));
17+
assertEquals("1000", TwosComplement.twosComplement("000"));
18+
assertEquals("100", TwosComplement.twosComplement("00"));
19+
assertEquals("10", TwosComplement.twosComplement("0"));
20+
}
21+
22+
@Test
23+
public void testTwosComplementAllOnes() {
24+
// Test with a binary number consisting entirely of ones
25+
assertEquals("00001", TwosComplement.twosComplement("11111"));
26+
assertEquals("0001", TwosComplement.twosComplement("1111"));
27+
assertEquals("001", TwosComplement.twosComplement("111"));
28+
assertEquals("01", TwosComplement.twosComplement("11"));
29+
}
30+
31+
@Test
32+
public void testTwosComplementMixedBits() {
33+
// Test with binary numbers with mixed bits
34+
assertEquals("1111", TwosComplement.twosComplement("0001")); // 1's complement: 1110, then add 1: 1111
35+
assertEquals("1001", TwosComplement.twosComplement("0111")); // 1's complement: 1000
36+
assertEquals("11001", TwosComplement.twosComplement("00111")); // 1's complement: 11000, add 1: 11001
37+
assertEquals("011", TwosComplement.twosComplement("101")); // 1's complement: 010, add 1: 011
38+
}
39+
40+
@Test
41+
public void testTwosComplementSingleBit() {
42+
// Test with single bit
43+
assertEquals("10", TwosComplement.twosComplement("0"));
44+
assertEquals("1", TwosComplement.twosComplement("1"));
45+
}
46+
47+
@Test
48+
public void testTwosComplementWithLeadingZeroes() {
49+
// Test with leading zeroes in the input
50+
assertEquals("1111", TwosComplement.twosComplement("0001"));
51+
assertEquals("101", TwosComplement.twosComplement("011"));
52+
assertEquals("110", TwosComplement.twosComplement("010"));
53+
}
54+
}

0 commit comments

Comments
 (0)