-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.java
220 lines (170 loc) · 5.6 KB
/
code.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package co_project;
import java.util.Scanner;
public class _2019235_AnoushkaNarang_Project2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn = new Scanner(System.in);
System.out.println("Enter the first number");
int m1 = scn.nextInt(); // multiplicand
System.out.println("Enter the second number");
int m2 = scn.nextInt(); // multiplier
String m = getbinary(m1);
System.out.println(m1 + ", the multiplicand in signed 2s complement notation is " + m);
String r = getbinary(m2);
System.out.println(m2 + ", the multiplier in signed 2s complement notation is " + r);
int x = m.length();
int y = r.length();
System.out.println("-------------------------------------------------------------------------");
// determining initial values of A,S and P
if (m1 * m2 > 32767 || m1 * m2 < -32768) { // -2^15 < product < 2^15-1 for a 16 bit register
System.out.println("Product is out of range for a 16-bit register!!");
return;
}
String A = m;
for (int i = 0; i <= y; i++) {
A = A + "0";
}
System.out.println("The initial value of A is " + A);
String S = get2scomplement(m.substring(1));
if (m1 <= 0) {
S = "0" + S;
} else {
S = "1" + S;
}
for (int i = 0; i <= y; i++) {
S = S + "0";
}
System.out.println("The initial value of S is " + S);
String P = "";
for (int i = 0; i < x; i++) {
P += "0";
}
P = P + r + "0";
System.out.println("The initial value of P is " + P);
System.out.println("-------------------------------------------------------------------------");
for (int i = 1; i <= y; i++) {
if (P.substring(P.length() - 2).equals("01")) {
P = binaryaddn(P, A);
P = shiftright(P);
System.out.println("The value of P after P + A operation and right shift is " + P + " when i is " + i);
}
else if (P.substring(P.length() - 2).equals("10")) {
P = binaryaddn(P, S);
P = shiftright(P);
System.out.println("The value of P after P + S operation and right shift is " + P + " when i is " + i);
}
else {
P = shiftright(P);
System.out.println("The value of P after right shift is " + P + " when i is " + i);
}
}
P = P.substring(0, P.length() - 1);
System.out.println("-------------------------------------------------------------------------");
System.out.println("The product of the numbers entered in binary is " + P);
if (P.charAt(0) == '0') {
System.out.print("It's Decimal Equivalent is ");
int i;
for (i = 0; i < P.length(); i++) {
if (P.charAt(i) == '0') { // leftmost zeroes are removed to get decimal eqv to avoid redundancy
continue;
}
break;
}
if (i == P.length()) { // this means that the string consists of zeroes only.
// checking for 0
System.out.print("0");
return;
}
binarytodec(P.substring(i)); // after removing leading zeroes
}
if (P.charAt(0) == '1') {
System.out.print("MSB = 1 indicates that the given number is negative."
+ " It's magnitude is given by the magnitude of it's 2's complement, that is ");
binarytodec(get2scomplement(P));
}
}
public static String get2scomplement(String s1) {
String s2 = "";
for (int i = 0; i < s1.length(); i++) { // flipping the bits
if (s1.charAt(i) == '0') {
s2 += '1';
continue;
}
s2 += '0';
}
String s3 = "";
String carry = "1";
for (int i = s2.length() - 1; i >= 0; i--) {
if (s2.charAt(s2.length() - 1) == '0') {
s2 = s2.substring(0, s2.length() - 1) + "1";
return s2;
}
if (s2.charAt(i) == '1' && carry.equals("1")) {
s3 = '0' + s3;
carry = "1";
} else if (s2.charAt(i) == '0' && carry.equals("1")) {
s3 = '1' + s3;
carry = "0";
} else if (s2.charAt(i) == '0' && carry.equals("0")) {
s3 = '0' + s3;
carry = "0";
} else {
s3 = '1' + s3;
carry = "0";
}
}
return s3;
}
public static String getbinary(int n) {
String str1 = "";
if (n >= 0) {
str1 = Integer.toBinaryString(n);
str1 = "0" + str1;
} else {
str1 = Integer.toBinaryString(-1 * n);
str1 = get2scomplement(str1);
str1 = "1" + str1;
}
return str1;
}
public static String shiftright(String str) {
return str.charAt(0) + str.substring(0, str.length() - 1);
}
public static String binaryaddn(String s1, String s2) {
String res = "";
String carry = "0";
for (int i = s1.length() - 1; i >= 0; i--) {
if (s1.charAt(i) == '1' && s2.charAt(i) == '1' && carry.equals("0")
|| (s1.charAt(i) == '0' && s2.charAt(i) == '1' && carry.equals("1"))
|| s1.charAt(i) == '1' && s2.charAt(i) == '0' && carry.equals("1")) {
res = '0' + res;
carry = "1";
} else if (s1.charAt(i) == '1' && s2.charAt(i) == '1' && carry.equals("1")) {
res = '1' + res;
carry = "1";
} else if (s1.charAt(i) == '0' && s2.charAt(i) == '1' && carry.equals("0")
|| s1.charAt(i) == '1' && s2.charAt(i) == '0' && carry.equals("0")
|| s1.charAt(i) == '0' && s2.charAt(i) == '0' && carry.equals("1")) {
res = '1' + res;
carry = "0";
} else if (s1.charAt(i) == '0' && s2.charAt(i) == '0' && carry.equals("0")) {
res = '0' + res;
carry = "0";
}
}
return res;
}
public static void binarytodec(String s) {
Long n = Long.parseLong(s);
Long rem = (long) 0;
Long ans = (long) 0;
Long val = (long) 1;
while (n != 0) {
rem = n % 10;
ans = ans + rem * val;
n = n / 10;
val = val * 2;
}
System.out.println(ans);
}
}