/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java
28 строк
928 B
Abhinay Verma
Add Ones & Twos Complement in BitManipulation (#5604)
09 окт 2024, 22:45
Не верифицирован
09 окт 2024, 22:45
f170078
Код
Авторство
О чём код?
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() { } // Function to get the 1's complement of a binary number public static String onesComplement(String binary) { StringBuilder complement = new StringBuilder(); // Invert each bit to get the 1's complement for (int i = 0; i < binary.length(); i++) { if (binary.charAt(i) == '0') { complement.append('1'); } else { complement.append('0'); } } return complement.toString(); } }