OLD | NEW |
1 /* | 1 /* |
2 * mptest-9.c | 2 * mptest-9.c |
3 * | 3 * |
4 * Test logical functions | 4 * Test logical functions |
5 * | 5 * |
6 * This Source Code Form is subject to the terms of the Mozilla Public | 6 * This Source Code Form is subject to the terms of the Mozilla Public |
7 * License, v. 2.0. If a copy of the MPL was not distributed with this | 7 * License, v. 2.0. If a copy of the MPL was not distributed with this |
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
9 | 9 |
10 #include <stdio.h> | 10 #include <stdio.h> |
11 #include <stdlib.h> | 11 #include <stdlib.h> |
12 #include <string.h> | 12 #include <string.h> |
13 #include <ctype.h> | 13 #include <ctype.h> |
14 #include <limits.h> | 14 #include <limits.h> |
15 #include <time.h> | 15 #include <time.h> |
16 | 16 |
17 #include "mpi.h" | 17 #include "mpi.h" |
18 #include "mplogic.h" | 18 #include "mplogic.h" |
19 | 19 |
20 int main(int argc, char *argv[]) | 20 int main(int argc, char *argv[]) { |
21 { | 21 mp_int a, b, c; |
22 mp_int a, b, c; | 22 int pco; |
23 int pco; | 23 mp_err res; |
24 mp_err res; | |
25 | 24 |
26 printf("Test 9: Logical functions\n\n"); | 25 printf("Test 9: Logical functions\n\n"); |
27 | 26 |
28 if(argc < 3) { | 27 if (argc < 3) { |
29 fprintf(stderr, "Usage: %s <a> <b>\n", argv[0]); | 28 fprintf(stderr, "Usage: %s <a> <b>\n", argv[0]); |
30 return 1; | 29 return 1; |
31 } | 30 } |
32 | 31 |
33 mp_init(&a); mp_init(&b); mp_init(&c); | 32 mp_init(&a); |
| 33 mp_init(&b); |
| 34 mp_init(&c); |
34 mp_read_radix(&a, argv[1], 16); | 35 mp_read_radix(&a, argv[1], 16); |
35 mp_read_radix(&b, argv[2], 16); | 36 mp_read_radix(&b, argv[2], 16); |
36 | 37 |
37 printf("a = "); mp_print(&a, stdout); fputc('\n', stdout); | 38 printf("a = "); |
38 printf("b = "); mp_print(&b, stdout); fputc('\n', stdout); | 39 mp_print(&a, stdout); |
| 40 fputc('\n', stdout); |
| 41 printf("b = "); |
| 42 mp_print(&b, stdout); |
| 43 fputc('\n', stdout); |
39 | 44 |
40 mpl_not(&a, &c); | 45 mpl_not(&a, &c); |
41 printf("~a = "); mp_print(&c, stdout); fputc('\n', stdout); | 46 printf("~a = "); |
| 47 mp_print(&c, stdout); |
| 48 fputc('\n', stdout); |
42 | 49 |
43 mpl_and(&a, &b, &c); | 50 mpl_and(&a, &b, &c); |
44 printf("a & b = "); mp_print(&c, stdout); fputc('\n', stdout); | 51 printf("a & b = "); |
| 52 mp_print(&c, stdout); |
| 53 fputc('\n', stdout); |
45 | 54 |
46 mpl_or(&a, &b, &c); | 55 mpl_or(&a, &b, &c); |
47 printf("a | b = "); mp_print(&c, stdout); fputc('\n', stdout); | 56 printf("a | b = "); |
| 57 mp_print(&c, stdout); |
| 58 fputc('\n', stdout); |
48 | 59 |
49 mpl_xor(&a, &b, &c); | 60 mpl_xor(&a, &b, &c); |
50 printf("a ^ b = "); mp_print(&c, stdout); fputc('\n', stdout); | 61 printf("a ^ b = "); |
| 62 mp_print(&c, stdout); |
| 63 fputc('\n', stdout); |
51 | 64 |
52 mpl_rsh(&a, &c, 1); | 65 mpl_rsh(&a, &c, 1); |
53 printf("a >> 1 = "); mp_print(&c, stdout); fputc('\n', stdout); | 66 printf("a >> 1 = "); |
| 67 mp_print(&c, stdout); |
| 68 fputc('\n', stdout); |
54 mpl_rsh(&a, &c, 5); | 69 mpl_rsh(&a, &c, 5); |
55 printf("a >> 5 = "); mp_print(&c, stdout); fputc('\n', stdout); | 70 printf("a >> 5 = "); |
| 71 mp_print(&c, stdout); |
| 72 fputc('\n', stdout); |
56 mpl_rsh(&a, &c, 16); | 73 mpl_rsh(&a, &c, 16); |
57 printf("a >> 16 = "); mp_print(&c, stdout); fputc('\n', stdout); | 74 printf("a >> 16 = "); |
| 75 mp_print(&c, stdout); |
| 76 fputc('\n', stdout); |
58 | 77 |
59 mpl_lsh(&a, &c, 1); | 78 mpl_lsh(&a, &c, 1); |
60 printf("a << 1 = "); mp_print(&c, stdout); fputc('\n', stdout); | 79 printf("a << 1 = "); |
| 80 mp_print(&c, stdout); |
| 81 fputc('\n', stdout); |
61 mpl_lsh(&a, &c, 5); | 82 mpl_lsh(&a, &c, 5); |
62 printf("a << 5 = "); mp_print(&c, stdout); fputc('\n', stdout); | 83 printf("a << 5 = "); |
| 84 mp_print(&c, stdout); |
| 85 fputc('\n', stdout); |
63 mpl_lsh(&a, &c, 16); | 86 mpl_lsh(&a, &c, 16); |
64 printf("a << 16 = "); mp_print(&c, stdout); fputc('\n', stdout); | 87 printf("a << 16 = "); |
| 88 mp_print(&c, stdout); |
| 89 fputc('\n', stdout); |
65 | 90 |
66 mpl_num_set(&a, &pco); | 91 mpl_num_set(&a, &pco); |
67 printf("population(a) = %d\n", pco); | 92 printf("population(a) = %d\n", pco); |
68 mpl_num_set(&b, &pco); | 93 mpl_num_set(&b, &pco); |
69 printf("population(b) = %d\n", pco); | 94 printf("population(b) = %d\n", pco); |
70 | 95 |
71 res = mpl_parity(&a); | 96 res = mpl_parity(&a); |
72 if(res == MP_EVEN) | 97 if (res == MP_EVEN) |
73 printf("a has even parity\n"); | 98 printf("a has even parity\n"); |
74 else | 99 else |
75 printf("a has odd parity\n"); | 100 printf("a has odd parity\n"); |
76 | 101 |
77 mp_clear(&c); | 102 mp_clear(&c); |
78 mp_clear(&b); | 103 mp_clear(&b); |
79 mp_clear(&a); | 104 mp_clear(&a); |
80 | 105 |
81 return 0; | 106 return 0; |
82 } | 107 } |
83 | |
OLD | NEW |