Files
Java/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java
Oleksandr Klymenko 25aaa6e064 refactor: OnesComplement Tests Using Parameterized Inputs (#6375)
refactor: OnesComplement Tests Using Parameterized Inputs

Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
2025-07-14 05:51:17 +00:00

38 lines
1.4 KiB
Java

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