Skip to content

Commit e285e02

Browse files
Added Ones Complement
1 parent c932c99 commit e285e02

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed
Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.thealgorithms.bitmanipulation;
22

3-
import java.util.Scanner;
4-
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 and returns
7+
* the complemented binary string.
8+
* @return the complimented binary string
9+
*/
510
public class OnesComplement {
6-
711
// Function to get the 1's complement of a binary number
812
public static String onesComplement(String binary) {
913
StringBuilder complement = new StringBuilder();
@@ -19,21 +23,5 @@ public static String onesComplement(String binary) {
1923
return complement.toString();
2024
}
2125

22-
public static void main(String[] args) {
23-
Scanner scanner = new Scanner(System.in);
24-
25-
// Input a binary number
26-
System.out.print("Enter a number: ");
27-
int number = scanner.nextInt();
28-
29-
String binary = Integer.toBinaryString(number);
30-
31-
// Compute and print the 1's complement
32-
String result = onesComplement(binary);
33-
int n = Integer.parseInt(result, 2);
34-
System.out.println("1's complement of " + number +"("+binary+")"+" is: " + n + "("+result+")");
35-
36-
scanner.close();
37-
}
3826
}
3927

0 commit comments

Comments
 (0)