-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryAddition.java
81 lines (71 loc) · 2 KB
/
binaryAddition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.io.InputStreamReader;
import java.io.IOException;
class BinaryAddition
{
public static void main(String[] args)
{
int[] bin1 = new int[8];
int[] bin2 = new int[8];
int[] res = new int[8];
int carry = 0;
try (InputStreamReader kb = new InputStreamReader(System.in))
{
System.out.println("Enter an eight bit binary number:");
for (int i = 0; i < bin1.length; i++)
{
bin1[i] = Character.getNumericValue(kb.read());
}
while ( kb.ready() )
{
kb.read();
}
System.out.println("Enter an other eight bit binary number:");
for (int i = 0; i < bin2.length; i++)
{
bin2[i] = Character.getNumericValue(kb.read());
}
}
catch (IOException e)
{
System.out.println("Fel fel fel");
}
System.out.println("You entered:");
for (int i = 0; i < bin1.length; i++)
{
System.out.print(bin1[i]);
}
System.out.println();
for (int i = 0; i < bin2.length; i++)
{
System.out.print(bin2[i]);
}
System.out.println();
for (int i = res.length-1; i >= 0; i--)
{
switch (bin1[i] + bin2[i] + carry)
{
case 0:
res[i] = 0;
carry = 0;
break;
case 1:
res[i] = 1;
carry = 0;
break;
case 2:
res[i] = 0;
carry = 1;
break;
case 3:
res[i] = 1;
carry = 1;
break;
}
}
System.out.println("Numbers added:");
for (int i = 0; i<res.length; i++)
{
System.out.print(res[i]);
}
}
}