File tree 1 file changed +7
-19
lines changed
src/main/java/com/thealgorithms/bitmanipulation 1 file changed +7
-19
lines changed Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .bitmanipulation ;
2
2
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
+ */
5
10
public class OnesComplement {
6
-
7
11
// Function to get the 1's complement of a binary number
8
12
public static String onesComplement (String binary ) {
9
13
StringBuilder complement = new StringBuilder ();
@@ -19,21 +23,5 @@ public static String onesComplement(String binary) {
19
23
return complement .toString ();
20
24
}
21
25
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
- }
38
26
}
39
27
You can’t perform that action at this time.
0 commit comments