forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnesComplement.java
More file actions
37 lines (34 loc) · 1.37 KB
/
OnesComplement.java
File metadata and controls
37 lines (34 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.thealgorithms.bitmanipulation;
/**
* @author - https://github.com/Monk-AbhinayVerma
* @Wikipedia - https://en.wikipedia.org/wiki/Ones%27_complement
* The class OnesComplement computes the complement of binary number
* and returns
* the complemented binary string.
* @return the complimented binary string
*/
public final class OnesComplement {
private OnesComplement() {
}
/**
* Returns the 1's complement of a binary string.
*
* @param binary A string representing a binary number (e.g., "1010").
* @return A string representing the 1's complement.
* @throws IllegalArgumentException if the input is null or contains characters other than '0' or '1'.
*/
public static String onesComplement(String binary) {
if (binary == null || binary.isEmpty()) {
throw new IllegalArgumentException("Input must be a non-empty binary string.");
}
StringBuilder complement = new StringBuilder(binary.length());
for (char bit : binary.toCharArray()) {
switch (bit) {
case '0' -> complement.append('1');
case '1' -> complement.append('0');
default -> throw new IllegalArgumentException("Input must contain only '0' and '1'. Found: " + bit);
}
}
return complement.toString();
}
}