security/nss/lib/freebl/mpi/tests/mptest-9.c

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:73d46244bf60
1 /*
2 * mptest-9.c
3 *
4 * Test logical functions
5 *
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
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <limits.h>
15 #include <time.h>
16
17 #include "mpi.h"
18 #include "mplogic.h"
19
20 int main(int argc, char *argv[])
21 {
22 mp_int a, b, c;
23 int pco;
24 mp_err res;
25
26 printf("Test 9: Logical functions\n\n");
27
28 if(argc < 3) {
29 fprintf(stderr, "Usage: %s <a> <b>\n", argv[0]);
30 return 1;
31 }
32
33 mp_init(&a); mp_init(&b); mp_init(&c);
34 mp_read_radix(&a, argv[1], 16);
35 mp_read_radix(&b, argv[2], 16);
36
37 printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
38 printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
39
40 mpl_not(&a, &c);
41 printf("~a = "); mp_print(&c, stdout); fputc('\n', stdout);
42
43 mpl_and(&a, &b, &c);
44 printf("a & b = "); mp_print(&c, stdout); fputc('\n', stdout);
45
46 mpl_or(&a, &b, &c);
47 printf("a | b = "); mp_print(&c, stdout); fputc('\n', stdout);
48
49 mpl_xor(&a, &b, &c);
50 printf("a ^ b = "); mp_print(&c, stdout); fputc('\n', stdout);
51
52 mpl_rsh(&a, &c, 1);
53 printf("a >> 1 = "); mp_print(&c, stdout); fputc('\n', stdout);
54 mpl_rsh(&a, &c, 5);
55 printf("a >> 5 = "); mp_print(&c, stdout); fputc('\n', stdout);
56 mpl_rsh(&a, &c, 16);
57 printf("a >> 16 = "); mp_print(&c, stdout); fputc('\n', stdout);
58
59 mpl_lsh(&a, &c, 1);
60 printf("a << 1 = "); mp_print(&c, stdout); fputc('\n', stdout);
61 mpl_lsh(&a, &c, 5);
62 printf("a << 5 = "); mp_print(&c, stdout); fputc('\n', stdout);
63 mpl_lsh(&a, &c, 16);
64 printf("a << 16 = "); mp_print(&c, stdout); fputc('\n', stdout);
65
66 mpl_num_set(&a, &pco);
67 printf("population(a) = %d\n", pco);
68 mpl_num_set(&b, &pco);
69 printf("population(b) = %d\n", pco);
70
71 res = mpl_parity(&a);
72 if(res == MP_EVEN)
73 printf("a has even parity\n");
74 else
75 printf("a has odd parity\n");
76
77 mp_clear(&c);
78 mp_clear(&b);
79 mp_clear(&a);
80
81 return 0;
82 }
83

mercurial