Back to index

lightning-sunbird  0.9+nobinonly
mptest-9.c
Go to the documentation of this file.
00001 /*
00002  *    mptest-9.c
00003  *
00004  *   Test logical functions
00005  *
00006  * ***** BEGIN LICENSE BLOCK *****
00007  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00008  *
00009  * The contents of this file are subject to the Mozilla Public License Version
00010  * 1.1 (the "License"); you may not use this file except in compliance with
00011  * the License. You may obtain a copy of the License at
00012  * http://www.mozilla.org/MPL/
00013  *
00014  * Software distributed under the License is distributed on an "AS IS" basis,
00015  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00016  * for the specific language governing rights and limitations under the
00017  * License.
00018  *
00019  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
00020  *
00021  * The Initial Developer of the Original Code is
00022  * Michael J. Fromberger.
00023  * Portions created by the Initial Developer are Copyright (C) 1998
00024  * the Initial Developer. All Rights Reserved.
00025  *
00026  * Contributor(s):
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 /* $Id: mptest-9.c,v 1.4 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
00042 
00043 #include <stdio.h>
00044 #include <stdlib.h>
00045 #include <string.h>
00046 #include <ctype.h>
00047 #include <limits.h>
00048 #include <time.h>
00049 
00050 #include "mpi.h"
00051 #include "mplogic.h"
00052 
00053 int main(int argc, char *argv[])
00054 {
00055   mp_int    a, b, c;
00056   int       pco;
00057   mp_err    res;
00058 
00059   printf("Test 9: Logical functions\n\n");
00060 
00061   if(argc < 3) {
00062     fprintf(stderr, "Usage: %s <a> <b>\n", argv[0]);
00063     return 1;
00064   }
00065 
00066   mp_init(&a); mp_init(&b); mp_init(&c);
00067   mp_read_radix(&a, argv[1], 16);
00068   mp_read_radix(&b, argv[2], 16);
00069 
00070   printf("a       = "); mp_print(&a, stdout); fputc('\n', stdout);
00071   printf("b       = "); mp_print(&b, stdout); fputc('\n', stdout);
00072 
00073   mpl_not(&a, &c);
00074   printf("~a      = "); mp_print(&c, stdout); fputc('\n', stdout);
00075 
00076   mpl_and(&a, &b, &c);
00077   printf("a & b   = "); mp_print(&c, stdout); fputc('\n', stdout);
00078 
00079   mpl_or(&a, &b, &c);
00080   printf("a | b   = "); mp_print(&c, stdout); fputc('\n', stdout);
00081 
00082   mpl_xor(&a, &b, &c);
00083   printf("a ^ b   = "); mp_print(&c, stdout); fputc('\n', stdout);
00084 
00085   mpl_rsh(&a, &c, 1);
00086   printf("a >>  1 = "); mp_print(&c, stdout); fputc('\n', stdout);
00087   mpl_rsh(&a, &c, 5);
00088   printf("a >>  5 = "); mp_print(&c, stdout); fputc('\n', stdout);
00089   mpl_rsh(&a, &c, 16);
00090   printf("a >> 16 = "); mp_print(&c, stdout); fputc('\n', stdout);
00091 
00092   mpl_lsh(&a, &c, 1);
00093   printf("a <<  1 = "); mp_print(&c, stdout); fputc('\n', stdout);
00094   mpl_lsh(&a, &c, 5);
00095   printf("a <<  5 = "); mp_print(&c, stdout); fputc('\n', stdout);
00096   mpl_lsh(&a, &c, 16);
00097   printf("a << 16 = "); mp_print(&c, stdout); fputc('\n', stdout);
00098 
00099   mpl_num_set(&a, &pco);
00100   printf("population(a) = %d\n", pco);
00101   mpl_num_set(&b, &pco);
00102   printf("population(b) = %d\n", pco);
00103 
00104   res = mpl_parity(&a);
00105   if(res == MP_EVEN)
00106     printf("a has even parity\n");
00107   else
00108     printf("a has odd parity\n");
00109   
00110   mp_clear(&c);
00111   mp_clear(&b);
00112   mp_clear(&a);
00113 
00114   return 0;
00115 }
00116