File tree 1 file changed +6
-20
lines changed
src/main/java/com/thealgorithms/bitmanipulation 1 file changed +6
-20
lines changed Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .bitmanipulation ;
2
-
3
- import java .util .Scanner ;
4
-
2
+ /**
3
+ * @wikipedia - https://en.wikipedia.org/wiki/Two%27s_complement
4
+ * This Algorithm was first suggested by Jon Von Neumann
5
+ * @author - https://github.com/Monk-AbhinayVerma
6
+ * @return - class returns the two's complement of any binary number
7
+ */
5
8
public class TwosComplement {
6
-
7
9
// Function to get the 2's complement of a binary number
8
10
public static String twosComplement (String binary ) {
9
11
StringBuilder onesComplement = new StringBuilder ();
@@ -36,20 +38,4 @@ public static String twosComplement(String binary) {
36
38
37
39
return twosComplement .toString ();
38
40
}
39
-
40
- public static void main (String [] args ) {
41
- Scanner scanner = new Scanner (System .in );
42
-
43
- // Input a binary number
44
- System .out .print ("Enter a number: " );
45
- int n = scanner .nextInt ();
46
- String binary = Integer .toBinaryString (n );
47
-
48
- // Compute and print the 2's complement
49
- String result = twosComplement (binary );
50
- int num = Integer .parseInt (result , 2 );
51
- System .out .println ("2's complement of " + n +"(" +binary +")" +" is: " + num + "(" + result +")" );
52
-
53
- scanner .close ();
54
- }
55
41
}
You can’t perform that action at this time.
0 commit comments