2 /*****************************************************************************/
4 /* Copyright (c) 2002 - 2004 by Steffen Beyer. */
5 /* All rights reserved. */
7 /* This program is free software; you can redistribute it */
8 /* and/or modify it under the same terms as Perl itself. */
10 /*****************************************************************************/
12 /******************************************************/
14 /* Example for using the BitVector.c library from C */
16 /* (Just for playing around; also shows how one can */
17 /* deal with error handling) */
19 /* Copy ToolBox.h and BitVector.h to this directory */
20 /* then compile this file and link with BitVector.o */
22 /******************************************************/
26 #include "BitVector.h"
28 void ListErrCode(ErrCode code)
30 if (code) fprintf(stdout, "BitVector ErrCode %2d: %s\n", code, BitVector_Error(code));
33 void PrintErrorMessage(ErrCode code, char *name)
35 if (code) fprintf(stdout, "Bit::Vector::%s(): %s\n", name, BitVector_Error(code));
38 #define FATAL_ERROR(name,code) \
39 { fprintf(stderr, "Bit::Vector::" name "(): %s\n", BitVector_Error(code)); exit(code); }
44 // N_char x[] = "001010"; /* 10 */
45 N_char x[] = "001x10"; /* 10 */
46 // N_char y[] = "000100"; /* 4 */
47 // N_char y[] = "000111"; /* 7 */
48 N_char y[] = "001111"; /* 63 */
54 boolean carry = false;
78 err = BitVector_Boot();
79 if (err) FATAL_ERROR("Boot", err);
81 printf("Number of bits in a WORD: %d\n", BitVector_Word_Bits());
82 printf("Number of bits in a LONG: %d\n", BitVector_Long_Bits());
84 X = BitVector_Create(bits, 1);
85 if (X == NULL) FATAL_ERROR("Create", ErrCode_Null);
87 err = BitVector_from_Bin(X, x);
88 PrintErrorMessage(err,"from_Bin");
90 Y = BitVector_Create(bits, 1);
91 if (Y == NULL) FATAL_ERROR("Create", ErrCode_Null);
93 err = BitVector_from_Bin(Y, y);
94 PrintErrorMessage(err,"from_Bin");
96 Z = BitVector_Create(bits, 1);
97 if (Z == NULL) FATAL_ERROR("Create", ErrCode_Null);
99 ovrflw = BitVector_add(Z, X, Y, &carry);
101 printf("result of %s + %s is %s (carry = %d, overflow = %d)\n",
103 BitVector_to_Dec(Y), /* Beware the memory leaks here! */
104 BitVector_to_Dec(Z), /* (Call "BitVector_Dispose()"!) */
107 err = BitVector_Multiply(Z, X, Y);
109 printf("result of %s * %s is %s\n",
111 BitVector_to_Dec(Y), /* Beware the memory leaks here! */
112 BitVector_to_Dec(Z)); /* (Call "BitVector_Dispose()"!) */
114 PrintErrorMessage(err,"Multiply");