-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbig_integer.c
More file actions
358 lines (300 loc) · 11 KB
/
big_integer.c
File metadata and controls
358 lines (300 loc) · 11 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* CSE202: Big Integer Manipulation Program
* Full name: Jack Keane
* Full Lehigh Email Address: jwk324@lehigh.edu
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// big_integer structure
struct two_quadwords{
unsigned long lsq;
long msq;
};
// big_integer typedef
typedef struct two_quadwords big_integer;
// Union to store big integers as 16 characters or two quadwords
union value{
big_integer binary;
char hex[16];
};
// Prototypes of the methods to manipulate big integers
// reads a big integer from string input and stores it in the union v
// returns 0 if the hexadecimal number is invalid, 1 otherwise
int read_big_integer(union value *v, char *input);
// writes the value of b to standard output as 32 hex characters
void write_big_integer(union value b);
// performs b1 & b2 and stores the result in b1_and_b2
void and_big_integers(big_integer b1, big_integer b2, big_integer *b1_and_b2);
// performs b1 | b2 and stores the result in b1_or_b2
void or_big_integers(big_integer b1, big_integer b2, big_integer *b1_or_b2);
// performs b1 ^ b2 and stores the result in b1_xor_b2
void xor_big_integers(big_integer b1, big_integer b2, big_integer *b1_xor_b2);
// performs ~b and stores the result in b
void not_big_integer(big_integer *b);
// performs b << k and stores the result in b
void shift_big_integer_left(big_integer *b, unsigned k);
// performs b >> k and stores the result in b
void shift_big_integer_right(big_integer *b, unsigned k);
// performs b1+b2 and stores the result in sum
// returns 1 if overflow occurs, 0 otherwise
int add_big_integers(big_integer b1, big_integer b2, big_integer *sum);
// prints the message and value followed by the program usage message
void print_usage(const char *message, const char* value);
// Char to int
int charToInt(char c);
// Check overflow
int check_overflow(big_integer b1, big_integer b2);
// main method
int main(int argc, char* argv[]){
// Checking to see if their are an invalid number of arguments
if(argc < 3 || argc > 4){
print_usage("Too many or too few arguments", "");
return 0;
}
// 3-arg command
if(argc == 3){ // NOT operation (3 arguments)
if(!(strcmp(argv[1], "not"))){ // check if operation is 'not'
union value v;
// Passing hex string and union to function.
if (read_big_integer(&v, argv[2]) == 0) {
print_usage("Invalid input:", argv[2]);
return 0;
}
// Formatting for printing
fprintf(stdout, "~ ");
write_big_integer(v);
fprintf(stdout, " = ");
not_big_integer(&v.binary);
write_big_integer(v);
fprintf(stdout, "\n\n");
} else {
print_usage("Invalid operation", argv[1]);
}
return 0;
}
if(argc == 4){ //and, or, xor, sl, sr, add operations (4 arguments)
// Checking to see if the cmd involves shifting.
if(!(strcmp(argv[1], "sl")) || !(strcmp(argv[1], "sr"))) {
union value v;
unsigned int k = strtol(argv[2], NULL, 10);
// Validating hex & reading big int.
if (read_big_integer(&v, argv[3]) == 0) {
print_usage("Invalid input:", argv[3]);
return 0;
}
// Beginning of printing.
write_big_integer(v);
// Checking to see if its a left or right shift.
if(!(strcmp(argv[1], "sl"))) {
fprintf(stdout, " << %s = ", argv[2]);
shift_big_integer_left(&v.binary, k);
} else if(!(strcmp(argv[1], "sr"))) {
fprintf(stdout, " >> %s = ", argv[2]);
shift_big_integer_right(&v.binary, k);
}
// Finish printing.
write_big_integer(v);
fprintf(stdout, "\n\n");
} else if(!(strcmp(argv[1], "xor")) || !(strcmp(argv[1], "or")) || !(strcmp(argv[1], "and")) || !(strcmp(argv[1], "add"))) {
// ^ Checks to see if argument is valid: xor, or, and, add
union value b1; // Union for first input.
// Validating hex & reading big int.
if (read_big_integer(&b1, argv[2]) == 0) {
print_usage("Invalid input:", argv[2]);
return 0;
}
union value b2; // Union for second input.
// Validating hex & reading big int.
if (read_big_integer(&b2, argv[3]) == 0) {
print_usage("Invalid input:", argv[3]);
return 0;
}
// Declaring variables.
big_integer b1_b2; // Resulting big int.
union value b1_b2_union; // Resulting union.
int add_overflow = 0; // int var holding return of the add_big_integer function.
// Begin printing.
write_big_integer(b1);
// Checks to see which command and performs corresponding actions.
if(!(strcmp(argv[1], "xor"))) {
fprintf(stdout, " ^\n");
xor_big_integers(b1.binary, b2.binary, &b1_b2);
b1_b2_union.binary = b1_b2;
} else if(!(strcmp(argv[1], "or"))) {
fprintf(stdout, " |\n");
or_big_integers(b1.binary, b2.binary, &b1_b2);
} else if(!(strcmp(argv[1], "add"))) {
// Stores return value to see if " - Overflow" needs to be added at the end.
fprintf(stdout, " +\n");
add_overflow = add_big_integers(b1.binary, b2.binary, &b1_b2);
} else {
// No if-statement for 'and' needed because the outer if-statement ensures
// that once the code makes it to this point, it will always be using the 'and' command.
fprintf(stdout, " &\n");
and_big_integers(b1.binary, b2.binary, &b1_b2);
}
// Finishes printing format.
write_big_integer(b2);
fprintf(stdout, " =\n");
b1_b2_union.binary = b1_b2;
write_big_integer(b1_b2_union);
// Checking to see if there is overflow.
if (add_overflow) {
fprintf(stdout, " - Overflow\n\n");
} else fprintf(stdout, "\n\n");
} else {
print_usage("Invalid operation:", argv[1]);
}
}
return 0;
}
// DONE – reads 32 hex characters // returns 0 if the hexadecimal number is invalid, 1 otherwise
int read_big_integer(union value *v, char *input){
// Checking to see if hexidecimal number is invalid
if (!(input[strspn(input, "0123456789abcdefABCDEF")] == 0) || strlen(input) != 32) {
return 0;
}
// For loop to iterate over hex array
int j = 31;
for (size_t i = 0; i < sizeof(v->hex); i++) {
char res;
int i1 = charToInt(input[j-1]);
int i2 = charToInt(input[j]);
j-=2;
res = i1 << 4; //bit,bit,bit,bit, 0,0,0,0.
res |= i2; // Adds the c1 and c2 together.
v->hex[i] = res;
}
// return 1 for success
return 1;
}
// DONE – writes the 32 hex characters b to standard output
void write_big_integer(union value b){
int j = 0;
// Working backwards since we are working in little-endian.
for (size_t i = sizeof(b.hex) -1; i > 0; i--) {
if (j != 0 && j % 2 == 0) {
fprintf(stdout, " ");
}
j++;
fprintf(stdout, "%02x", (unsigned char)b.hex[i]);
}
fprintf(stdout, "%02x", (unsigned char)b.hex[0]);
}
// DONE – performs b1 & b2 and stores the result in b1_and_b2
void and_big_integers(big_integer b1, big_integer b2, big_integer *b1_and_b2){
unsigned long x1 = b1.lsq;
long x2 = b1.msq;
unsigned long y1 = b2.lsq;
long y2 = b2.msq;
b1_and_b2->lsq = x1 & y1;
b1_and_b2->msq = x2 & y2;
}
// DONE – performs b1 | b2 and stores the result in b1_or_b2
void or_big_integers(big_integer b1, big_integer b2, big_integer *b1_or_b2){
unsigned long x1 = b1.lsq;
long x2 = b1.msq;
unsigned long y1 = b2.lsq;
long y2 = b2.msq;
b1_or_b2->lsq = x1 | y1;
b1_or_b2->msq = x2 | y2;
}
// DONE – performs b1 ^ b2 and stores the result in b1_xor_b2
void xor_big_integers(big_integer b1, big_integer b2, big_integer *b1_xor_b2){
unsigned long x1 = b1.lsq;
long x2 = b1.msq;
unsigned long y1 = b2.lsq;
long y2 = b2.msq;
b1_xor_b2->lsq = x1 ^ y1;
b1_xor_b2->msq = x2 ^ y2;
}
// DONE – performs ~b and stores the result in b
void not_big_integer(big_integer *b){
b->lsq = ~(b->lsq);
b->msq = ~(b->msq);
}
// DONE – performs b << k and stores the result in b – 100%
void shift_big_integer_left(big_integer *b, unsigned k){
// Shifts msq if greater than 64 and then makes lsq all 0s.
if (k >= 64) {
b->msq = b->msq << (k-64);
b->lsq = b->msq & 0;
} else {
b->lsq = b->lsq << k;
}
}
// DONE – performs b >> k and stores the result in b
void shift_big_integer_right(big_integer *b, unsigned k){
int bits = sizeof(b->msq) * 8; // Get warning unless from next line unless I do this.
int operand = 1 << (bits - 1); // Operand to be used for seeing the msq of each big int.
long signedness = operand & b->msq; // Checks to see if first bit is '1'.
if (k < 64) {
// Simple shifting op.
b->msq = b->msq >> k;
} else if (signedness != -2147483648) {
//shifts by '0'
long temp_long = b->msq >> (k - 64);
b->msq = b->lsq & 0;
b->lsq = temp_long;
} else if (signedness == -2147483648) {
// Casting to signed so that the long shifts by 'f'
b->lsq = (signed) b->lsq >> (k - 64);
b->msq = b->msq & -1;
}
}
// performs b1+b2 and stores the result in sum
// returns 1 if overflow occurs, 0 otherwise
int add_big_integers(big_integer b1, big_integer b2, big_integer *sum){
// Calculating sum.
sum->lsq = (b1.lsq + b2.lsq);
sum->msq = (b1.msq + b2.msq);
// Calculating overflow.
if ((check_overflow(b1, b2) == 1 )|| (check_overflow(b1, b2) == 0)) {
return 1;
} else return 0;
}
// prints the message and value followed by the program usage message
void print_usage(const char *message, const char* value){
// Formatting string
if (strcmp(value, "")) {
printf("%s '%s'\n", message, value);
} else printf("%s %s\n", message, value);
// Standard usage message
fprintf(stdout, "Usage: \n");
fprintf(stdout, "\t./prog0 op [k] number1 [number2]\n");
fprintf(stdout, "\t\top: operation from {not, sl, sr, and, or, xor, add}\n");
fprintf(stdout, "\t\tk: shift positions for operations 'sl' and 'sr'\n");
fprintf(stdout, "\t\tnumber1: operand in hex for unary operations 'not', 'sl', 'sr'\n");
fprintf(stdout, "\t\tor first operand in hex for binary operations 'and', 'or', 'xor', 'add'\n");
fprintf(stdout, "\t\tnumber2: second operand in hex for binary operations\n");
// Exits code
exit(0);
}
// Char to int helper method
int charToInt(char c) {
// Standard char to int conversion procedure.
switch(c) {
case '0'...'9':
c -= 48;
break;
case 'A'...'F':
c -=55;
break;
case 'a'...'f':
c -= 87;
break;
}
return c;
}
// Check overflow helper method
int check_overflow(big_integer b1, big_integer b2) {
unsigned long operand = 1UL << (sizeof(b1.msq) * 8 - 1);
// Checking conditons required for overflow.
if ((b1.msq & operand) && (b2.msq & operand)) { // b1.msq & operand will either be 9223372036854775808 or 0.
return 1;
} else if ((b1.msq & operand) == 0 && (b2.msq & operand) == 0){
return 0;
}
return -1;
}