merge kaps
[libfirm] / ir / tv / IeeeCC754 / src / bitstring.cc
1 /*
2 ##########################################################################
3 #                                                                        #
4 # Program: IeeeCC754                                                     #
5 #                                                                        #
6 # Description:                                                           #
7 #   IeeeCC754 or IEEE 754 Compliance Checker is a precision and range    #
8 #   independent tool to test whether an implementation of                #
9 #   floating-point arithmetic (in hardware or software) is compliant     #
10 #   with the principles of the IEEE 754-854 floating-point standards.    #
11 #   You can find out more about the testing tool IeeeCC754 at            #
12 #                                                                        #
13 #         http://win-www.uia.ac.be/u/cant/ieeecc754.html                 #
14 #                                                                        #
15 #   This tool is in parts based on and greatly benefited from the        #
16 #   the program FPTEST developed by Jerome Coonen. For a full            #
17 #   description of the extensions to FPTEST and a reference to           #
18 #   the original Coonen program, please refer to the URL given above.    #
19 #   For the options available with the program IeeeCC754 and its         #
20 #   compatibility with David Hough's hexadecimal UCB format, we          #
21 #   also refer to the file readme.usage.                                 #
22 #                                                                        #
23 #  Usage: see readme.usage                                               #
24 #                                                                        #
25 #  Responsible authors:                                                  #
26 #         Brigitte Verdonk                                               #
27 #         Annie Cuyt                                                     #
28 #                                                                        #
29 #  Contributors:                                                         #
30 #         Tarun Agarwal(05-07/2002)                                      #
31 #         Johan Bogo (1998-1999)                                         #
32 #         Tim Gevers (10-12/2000)                                        #
33 #         Debby Ooms (1996-1997)                                         #
34 #         Geert Vermuyten (1996-1997)                                    #
35 #         Dennis Verschaeren (09/1996-06/2000)                           #
36 #                                                                        #
37 #  Copyright (C) 2000  University of Antwerp                             #
38 #                                                                        #
39 #  This program can be obtained from the authors, free, but WITHOUT ANY  #
40 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or     #
41 #  FITNESS FOR A PARTICULAR PURPOSE.                                     #
42 #                                                                        #
43 #  Contact:                                                              #
44 #       Brigitte.Verdonk@uia.ua.ac.be                                    #
45 #       Department of Mathematics and Computer Science                   #
46 #       University of Antwerp (UIA)                                      #
47 #       Universiteitsplein 1                                             #
48 #       B2610 Antwerp, BELGIUM                                           #
49 #                                                                        #
50 ##########################################################################
51
52 Filename:
53        $RCSfile$
54
55 Last updated:
56        $Date$
57
58 */
59 #include <string.h>
60 #include <Bitstring.h>
61 #include <stdio.h>
62
63 /*class implementation*/
64
65 /***********************************************************************
66 * Member:  GetPattern(unsigned long res)
67 * Purpose: returns a Bitstring with the first "res" bits set.
68 * Return:  Bitstring with first "res" bits set.
69 ***********************************************************************/
70 unsigned long Bitstring::GetPattern(unsigned long rest)
71 {
72   unsigned long pattern =(unsigned long) ((unsigned long) 1 << lengthT) -1;
73   pattern = pattern >> ((lengthT) -rest);
74   return pattern;
75 }
76
77
78 /***********************************************************************
79 * Member:  Convert()
80 * Purpose: returns the first block of bitstr. Its only defined to be used
81 *          in PutByte so the ^= operator would work.
82 * Return:  first block of bitstr -> bitstr[0].
83 ***********************************************************************/
84 unsigned long Bitstring::Convert()
85 {
86   return bitstr[0];
87 }
88
89
90
91 /***********************************************************************
92 * Member:  Bitstring()  Constructor
93 * Purpose: Create empty bitstring
94 * Return:  Nothing
95 ***********************************************************************/
96 Bitstring::Bitstring()
97 {
98   length=lengthBlock=0;
99   lengthT = sizeof(unsigned long)*8;
100   bitstr = NULL;
101 }
102
103 /***********************************************************************
104 * Member:  Bitstring(unsigned long size)  Constructor
105 * Purpose: Create bitstring of size "size" and initiate it with zeros
106 * Return:  Nothing
107 ***********************************************************************/
108 Bitstring::Bitstring(unsigned long size)
109 {
110   int i;
111   length= size;
112   lengthT = sizeof(unsigned long )*8;
113   if (size % lengthT == 0)
114     lengthBlock = (size /lengthT);
115   else
116     lengthBlock= (size /lengthT) + 1;
117   bitstr = new unsigned long [lengthBlock];
118   for (i=0;i<lengthBlock;i++)
119     bitstr[i]=0;
120 }
121
122 /***********************************************************************
123 * Member:  Bitstring(char * str)  Constructor
124 * Purpose: Create bitstring with "str" as initiale value
125 * Return:  Nothing
126 ***********************************************************************/
127 Bitstring::Bitstring(char * str)
128 {
129
130   long i;
131
132   length = strlen(str);
133   lengthT =sizeof(unsigned long )*8;
134
135   if (length % lengthT == 0)
136     lengthBlock = (length /lengthT);
137   else
138     lengthBlock= (length /lengthT) + 1;
139
140   bitstr = new unsigned long [lengthBlock];
141
142   for (i=0;i<lengthBlock;i++)
143     bitstr[i]=0;
144
145   for (i= length -1; i >= 0; i--)
146     {
147       if (str[i]=='1')
148   bitstr [(length -1 -i)/lengthT] |= 1 << ((length-1 -i)%lengthT);
149     }
150 }
151
152 /***********************************************************************
153 * Member:  Bitstring(Bitstring & copy)  Copy constructor
154 * Purpose: Create a bitstring with initiale value the bitstring "copy"
155 * Return:  Nothing
156 ***********************************************************************/
157 Bitstring::Bitstring(const Bitstring & copy)
158 {
159   length = copy.length;
160   lengthT = copy.lengthT;
161   lengthBlock = copy.lengthBlock;
162   if (length > 0) {
163     bitstr = new unsigned long [lengthBlock];
164     for (int i=0 ; i< lengthBlock; i++)
165       bitstr[i]= copy.bitstr[i];
166   }
167   else
168     bitstr = NULL;
169 }
170
171
172 /***********************************************************************
173 * Member:  ~Bitstring()  Deconstructor
174 * Purpose: Remove bitstr from memory
175 * Return:  Nothing
176 ***********************************************************************/
177 Bitstring::~Bitstring()
178 {
179   delete [] bitstr;
180 }
181
182 /***********************************************************************
183 * Member:  Length()
184 * Purpose: Get the length of the bitstring
185 * Return:  Length of bitstring
186 ***********************************************************************/
187 unsigned long Bitstring::Length() const
188 {
189   return length;
190 }
191
192 /***********************************************************************
193 * Member:  Resize (unsigned long len)
194 * Purpose: Change the length of the bitstring to "len". If "len" is
195 *          larger then the length, the bitstring is appended with 0, else
196 *          the bitstring is truncated to the new length "len"
197 * Return:  Previous length
198 ***********************************************************************/
199 unsigned long Bitstring::Resize(unsigned long len)
200 {
201  unsigned long * temp;
202  long tmplenBlock,tmpMinlen,i,rest,prevLen;
203  unsigned long value;
204
205  if (len > 0) {
206  /*create bitstr with new size*/
207  if (len % lengthT == 0)
208    tmplenBlock = (len /lengthT);
209  else
210    tmplenBlock= (len /lengthT) + 1;
211
212  temp = new unsigned long [tmplenBlock];
213
214  /*get length to copy*/
215  if (lengthBlock <= tmplenBlock)
216    tmpMinlen = lengthBlock;
217  else
218    tmpMinlen = tmplenBlock;
219
220  /*copy bitstr*/
221  for (i=0; i < tmpMinlen; i++)
222    temp[i] = bitstr[i] ;
223
224
225  /*delete old bitstr*/
226  if (bitstr != NULL)
227      delete [] bitstr;
228
229  if (len % lengthT != 0) {
230    rest = len % lengthT;
231    value = GetPattern (rest);
232    temp[len/lengthT] &= value;
233  }
234
235  /*append with 0*/
236  if(lengthBlock < tmplenBlock)
237    {
238      for (i = lengthBlock; i < tmplenBlock; i++)
239        temp[i]=0;
240    }
241
242  /*change size */
243  bitstr = temp;
244  prevLen = length;
245  length= len ;
246  lengthBlock=tmplenBlock;
247 }
248 else {
249   length=lengthBlock=0;
250   lengthT = sizeof(unsigned long)*8;
251   delete [] bitstr;
252   bitstr = NULL;
253 }
254  return prevLen;
255 }
256
257 /***********************************************************************
258 * Member:  GetBit(unsigned long bit)
259 * Purpose: Get the bit value at position "bit"
260 * Return:  0 <= "bit" < length -> The bit value
261 *          else           -> 0
262 ***********************************************************************/
263 int Bitstring::GetBit(long bit) const
264 { unsigned long  tmp;
265
266   if ((0 <= bit) && (bit < length)) {
267     tmp = bitstr[bit/lengthT];
268     if (tmp & (1L << lengthT - 1 - (bit% lengthT)))
269        return 1;
270   }
271   return 0;
272 }
273
274 /***********************************************************************
275 * Member:  PutBit (unsigned long bit, unsigned int bitvalue)
276 * Purpose: Replace the bit value at position "bit" with the new "bitvalue"
277 * Return:  Previous bit value
278 ***********************************************************************/
279 int Bitstring::PutBit(unsigned long bit,unsigned int bitvalue)
280 {
281   int prevBit;
282
283   /*get current bit value*/
284   prevBit = GetBit(bit);
285   // cout << "bit = " << bit << " " << bitvalue << endl;
286   // cout << "bitstr = " << bitstr[bit/lengthT] << endl;
287   if (bitvalue == 0) {
288       if (bitstr[bit/lengthT] & (1L << (lengthT - 1 - (bit%lengthT))))
289           bitstr[bit/lengthT] -= (1L << (lengthT - 1 - (bit%lengthT)));
290   }
291   else
292       bitstr[bit/lengthT] |= (1L << (lengthT - 1 - (bit%lengthT)));
293   // cout << "bitstr = " << bitstr[bit/lengthT] << endl;
294   return prevBit;
295 }
296
297 /***********************************************************************
298 * Member:  Set (unsigned long bit)
299 * Purpose: Replace the bit value at position "bit" with the 1
300 * Return:  Previous bit value
301 ***********************************************************************/
302 int Bitstring::Set(long bit)
303 {
304   return PutBit(bit,1);
305 }
306 /***********************************************************************
307 * Member:  Clear (unsigned long bit)
308 * Purpose: Replace the bit value at position "bit" with the 0
309 * Return:  Previous bit value
310 ***********************************************************************/
311 int Bitstring::Clear(long bit)
312 {
313   return PutBit(bit,0);
314 }
315
316 /***********************************************************************
317 * Member:  ClearBitString ()
318 * Purpose: Clears the bitstring
319 * Return:  Cleared bitstring
320 ***********************************************************************/
321 Bitstring Bitstring::ClearBitString()
322 {
323   int i ;
324   for (i =0; i< lengthBlock; i++)
325     bitstr[i] = 0;
326
327   return *this;
328 }
329
330 /***********************************************************************
331 * Member:  GetByte (unsigned long byte)
332 * Purpose: Get the byte value at position "byte"
333 * Return:  The byte value
334 ***********************************************************************/
335 void Bitstring::GetByte(unsigned long byte, Bitstring sub) const
336 {
337   SubBitstring(byte*8,sub);
338 }
339
340 /***********************************************************************
341 * Member:  PutByte (unsigned long byte, Bitstring bytevalue)
342 * Purpose: Replace the byte value at position "byte" with the new "bytevalue"
343 * Return:  Previous byte value
344 ***********************************************************************/
345 void Bitstring::PutByte(unsigned long byte,Bitstring bytevalue)
346 {
347   long sizeT;
348
349   sizeT=sizeof(unsigned long);
350
351   // GetByte(byte,prevByte);
352   // prevByte.Resize(lengthT);
353   // bytevalue.Resize(8);
354   // bytevalue.Resize(lengthT);
355
356   /* replace value by "bytevalue" using xor*/
357   // cout << bitstr << endl;
358   // cout << bytevalue << endl << flush;
359   bitstr[byte/sizeT] ^= (bytevalue.Convert() << ((byte%sizeT)*8));
360   // bitstr[byte/sizeT] ^= (prevByte.Convert() << ((byte%sizeT)*8));
361   // cout << bitstr << endl << flush;
362 }
363
364 /***********************************************************************
365 * Member:  Inc ()
366 * Purpose: Increase the bitstring value by 1
367 * Return:  Carry after most significant bit
368 ***********************************************************************/
369 int Bitstring::Inc()
370 {
371   int i = 0;
372
373   while ((i <length) && (GetBit(i)== 1))
374     Clear(i++);
375
376   if (i < length)
377     Set(i);
378   else
379     return 1;
380
381   return 0;
382
383 }
384
385 /***********************************************************************
386 * Member:  Dec ()
387 * Purpose: Decrease the bitstring value by 1
388 * Return:  1 -> wrap around (negative)
389 *          0 -> OK
390 ***********************************************************************/
391 int Bitstring:: Dec()
392 {
393   int i = 0;
394
395   while ((i<length) && (GetBit(i) == 0))
396     Set(i++);
397
398   if (i< length)
399     Clear(i);
400   else
401     return 1;
402
403   return 0;
404 }
405
406 /***********************************************************************
407 * Member:  BitstrToString()
408 * Purpose: Converts a bitstring to a C-string
409 * Return:  length >0   ->  The C-string
410 *          else        ->  "Bitstring is empty"
411 ***********************************************************************/
412 void Bitstring::BitstrToString(char str[256]) const
413 {
414   long place, i;
415   unsigned long block;
416
417
418   if (length > 0)
419     {
420       /*convert bitstring*/
421       for (i=0; i <length;i++)
422   {
423     place = i%lengthT;
424     block = 1 << place;
425     if ((bitstr[i/lengthT] & block) == 0)
426       str[length-1 -i]='0';
427     else
428       str[length-1 -i]='1';
429   }
430       str[length]='\0';
431     }
432   else
433     {
434       /*empty bitstring*/
435       //      str = new char [25];
436       // strcpy(str,"Bitstring is empty!\n");
437       str[0]='\0';
438     }
439 }
440
441
442 /***********************************************************************
443 * Member:  StringToBitstr(char *str)
444 * Purpose: Converts a C-string to a bitstring
445 * Return:  converted bitstring
446 ***********************************************************************/
447 void Bitstring::StringToBitstr(char *str)
448 {
449
450   long i;
451
452   length = strlen(str);
453   lengthT =sizeof(unsigned long)*8;
454   lengthBlock = (length/lengthT)+1;
455   if(bitstr)
456     delete [] bitstr;
457   bitstr = new unsigned long [lengthBlock];
458
459   for (i=0;i<lengthBlock;i++)
460     bitstr[i]=0;
461
462   for (i= length -1; i >= 0; i--)
463     {
464       if (str[i]=='1')
465   bitstr [(length -1 -i)/lengthT] |= 1 << ((length-1 -i)%lengthT);
466     }
467 }
468
469
470
471 /***********************************************************************
472 * Member:  Concat(const Bitstring &b2)
473 * Purpose: Puts bitstring *this in front of b2 -> so the first bit of b2
474 *          becomes the first bit of *thisb2
475 * Return:  *this -> *thisb2
476 ***********************************************************************/
477 void Bitstring::Concat(const Bitstring &b2)
478 {
479   Resize(length+b2.length);
480   for (int i = length;i < length + b2.length;i++) {
481      bitstr[i] = b2[i-length];
482   }
483 }
484
485
486 /***********************************************************************
487 * Member:  SubBitstring(unsigned long begin, unsigned long count)
488 * Purpose: Returns the substring defined by position "begin" to "count"
489 * Return:  substring from bitstring
490 ***********************************************************************/
491 void Bitstring::SubBitstring(unsigned long begin, Bitstring &sub) const
492 {
493   Bitstring temp(*this);
494   int i;
495   // cout << "temp = " << temp << endl << flush;
496   temp << begin;
497   // cout << "temp = " << temp << endl << flush;
498   for (i = 0;i < sub.lengthBlock-1;i++)
499     sub.bitstr[i] = temp[i];
500   sub.bitstr[i] = ((temp[i] >> (32 - (sub.length % lengthT))) << (32 - (sub.length % lengthT)));
501   // cout << sub << endl << flush;
502 }
503
504 /***********************************************************************
505 * Member:  operator = (const Bitstring &copy)
506 * Purpose: Overloads the assign operator. Makes the bistring equal to "copy"
507 * Return:  The changed bitstring
508 ***********************************************************************/
509 Bitstring& Bitstring::operator = (const Bitstring &copy)
510 {
511   int i=0;
512   //  unsigned long * tmp;
513
514   /*change lengths*/
515   length = copy.length;
516   lengthT = copy.lengthT;
517   lengthBlock = copy.lengthBlock;
518
519   if(bitstr)
520     // tmp = bitstr;
521     delete [] bitstr;
522
523
524   //check length of copy
525   if (length >0)
526     {
527       bitstr = new unsigned long [lengthBlock];
528
529       /*copy the bitstring*/
530       for (i=0 ; i< lengthBlock; i++)
531   bitstr[i]= copy.bitstr[i];
532     }
533   else
534     bitstr= NULL;
535
536   //  if (tmp)
537   //  delete [] tmp;
538
539   return *this;
540 }
541
542 /***********************************************************************
543 * Member:  operator == (const Bitstring & b) const
544 * Purpose: Overloads the equal operator.
545 * Return:  equal     -> 1
546 *          not equal -> 0
547 ***********************************************************************/
548 int Bitstring::operator == (const Bitstring &b) const
549 {
550   int i=0, value = 1;
551
552   /*if lengths not equal than the bitstrings not equal */
553   if (length != b.length)
554     value = 0;
555
556
557   while ((i<lengthBlock ) && (value))
558     {
559       if (bitstr[i] != b[i])
560   value =0;
561       i++;
562     }
563   return value;
564 }
565
566 /***********************************************************************
567 * Member:  operator != (const Bitstring &b) const
568 * Purpose: Overloads the not equal operator.
569 * Return:  not equal -> 1
570 *          equal     -> 0
571 ***********************************************************************/
572 int Bitstring::operator != (const Bitstring &b) const
573 {
574
575   int i=0, value = 0;
576
577   if (length != b.length) {
578     value = 1;
579   }
580   while ((i<lengthBlock ) && (!value))
581     {
582       if (bitstr[i] != b[i]) {
583   value = 1;
584       }
585       i++;
586     }
587
588   // cout << *this << endl;
589   // cout << b << endl;
590   return value;
591 }
592
593 /***********************************************************************
594 * Member:  operator > (const Bitstring &b) const
595 * Purpose: Overloads the greater than operator.
596 * Return:  greater       -> 1
597 *          not greater   -> 0
598 ***********************************************************************/
599 int Bitstring::operator > (const Bitstring &b) const
600 {
601   Bitstring temp;
602   int i;
603
604   if (*this == b)
605     return 0;
606
607   if (Length() > b.Length())
608     {
609       temp = b;
610       temp.Resize(Length());
611     }
612
613   if (Length() < b.Length())
614     {
615       temp = *this;
616       temp.Resize(b.Length());
617     }
618
619   i = Length()-1;
620
621   while (i>=0)
622     {
623       if (GetBit(i)!= b.GetBit(i))
624   {
625     if (GetBit(i) == 1)
626       return 1;
627     else
628       return 0;
629   }
630       i--;
631     }
632   return 0;
633 }
634
635 /***********************************************************************
636 * Member:  operator < (const Bitstring &b) const
637 * Purpose: Overloads the smaller than operator.
638 * Return:  smaller      -> 1
639 *          not smaller  -> 0
640 ***********************************************************************/
641 int Bitstring::operator < (const Bitstring &b) const
642 {
643   Bitstring temp;
644   int i;
645
646   if (*this == b)
647     return 0;
648
649   if (Length() > b.Length())
650     {
651       temp = b;
652       temp.Resize(Length());
653     }
654
655   if (Length() < b.Length())
656     {
657       temp = *this;
658       temp.Resize(b.Length());
659     }
660
661   i = Length()-1;
662
663   while (i>=0)
664     {
665       if (GetBit(i)!= b.GetBit(i))
666   {
667     if (GetBit(i) == 1)
668       return 0;
669     else
670       return 1;
671   }
672       i--;
673     }
674   return 0;
675 }
676
677
678
679 /***********************************************************************
680 * Member:  operator << (unsigned long count)
681 * Purpose: Overloads the shift left operator. Shifts the bitstring "count"
682 *          bits to the left
683 * Return:  shifted bitstring
684 ***********************************************************************/
685 void Bitstring::operator <<(long count)
686 {
687   int i,j, rest;
688
689   /*if count > lengthT -> copy blocks*/
690   if ((count/lengthT) > 0)
691     {
692       for (j=0; j  <lengthBlock ; j++)
693         {
694           if (j+(count/lengthT) < (unsigned long)(lengthBlock))
695             bitstr[j]=bitstr[j+(count/lengthT)];
696           else
697             bitstr[j]= 0;
698         }
699
700       //make lastblocks = 0
701       for (j= lengthBlock - (count/lengthT); j < lengthBlock ; j++)
702         bitstr[j]= 0;
703     }
704   /* if count < lengthT -> shift bitwise*/
705   rest = count % lengthT;
706
707   unsigned long leading = (1L << lengthT-1);
708   for (i=0; i < rest ; i++)
709     {
710       for (j=0; j < lengthBlock-1; j++)
711   {
712           bitstr[j] <<= 1;
713     //check for carry
714     if (bitstr[j+1] & leading)
715         bitstr[j] += 1L;
716   }
717         bitstr[lengthBlock-1] <<=1;
718     }
719 }
720
721 /***********************************************************************
722 * Member:  operator >> (unsigned long count)
723 * Purpose: Overloads the shift right operator. Shifts the bitstring "count"
724 *          bits to the right
725 * Return:  shifted bitstring
726 ***********************************************************************/
727 void Bitstring::operator >>(unsigned long count)
728 {
729   int i,j, rest,carry = 0;
730
731   /*if count > lengthT -> copy blocks*/
732   if ((count/lengthT) > 0)
733     {
734       // cout << "copy blocks" << endl << flush;
735       for (j=0; j  <lengthBlock ; j++)
736   {
737     if (j+(count/lengthT) < (unsigned long)(lengthBlock))
738       bitstr[j]=bitstr[j+(count/lengthT)];
739     else
740       bitstr[j]= 0;
741   }
742
743       //make lastblocks = 0
744       for (j= lengthBlock - (count/lengthT); j < lengthBlock ; j++)
745   bitstr[j]= 0;
746
747     }
748
749
750   /* if count < lengthT -> shift bitwise*/
751   rest = count % lengthT;
752   // cout << "rest = " << rest << endl << flush;
753   for (i=0; i < rest; i++)
754     {
755       carry = 0;
756       for (j=lengthBlock-1;j > 0; j--)
757   {
758           bitstr[j] >>= 1;
759           if ((bitstr[j-1] & 1L) == 1L)
760             bitstr[j] |= (1 <<( lengthT-1));
761   }
762         bitstr[0] >>= 0;
763     }
764 }
765
766
767 /***********************************************************************
768 * Member:  operator [] (unsigned long n)
769 * Purpose: Overloads the array operator. Returns/change block "n"
770 * Return:  block "n"
771 ***********************************************************************/
772 unsigned long& Bitstring::operator [](unsigned long n) const
773 {
774   //if(( n < 0) || (n > lengthBlock))
775     return bitstr[n];
776 }
777
778 /***********************************************************************
779 * Member:  operator &(const Bitstring &bitst)
780 * Purpose: Overloads the bitwise and operator. Does a bitwise and with
781 *          "bitstr" and "bitst". Makes the size of bitst equal to the
782 *          bitstring.
783 * Return:  bitwised and -> *this & bitst
784 ***********************************************************************/
785 Bitstring Bitstring::operator &(const Bitstring &bitst)
786 {
787
788   long i;
789   Bitstring temp1(*this),temp2(bitst);
790
791   //make size bitst equal to *this
792   if (lengthBlock > bitst.lengthBlock)
793     temp2.Resize(length);
794
795   //bitwise and
796   for (i = 0; i<lengthBlock ; i++ )
797     temp1[i] &= temp2[i];
798
799   return temp1;
800
801 }
802
803 /***********************************************************************
804 * Member:  operator |(const Bitstring &bitst)
805 * Purpose: Overloads the bitwise or operator. Does a bitwise or with
806 *          "bitstr" and "bitst". Makes the size of bitst equal to the
807 *          bitstring.
808 * Return:  bitwised or -> *this | bitst
809 ***********************************************************************/
810 Bitstring Bitstring::operator |(const Bitstring &bitst)
811 {
812   long i;
813   Bitstring temp1(*this),temp2(bitst);
814
815  //make size bitst equal to *this
816   if (lengthBlock > bitst.lengthBlock)
817     temp2.Resize(length);
818
819   //bitwise or
820   for (i = 0; i<lengthBlock ; i++ )
821     temp1[i] |= temp2[i];
822
823   return temp1;
824 }
825
826
827 /***********************************************************************
828 * Member:  operator ^ (const Bitstring &bitst)
829 * Purpose: Overloads the bitwise xor operator. Does a bitwise xor with
830 *          "bitstr" and "bitst". Makes the size of bitst equal to the
831 *          bitstring.
832 * Return:  bitwised xor -> *this ^ bitst
833 ***********************************************************************/
834 Bitstring Bitstring::operator ^( const Bitstring &bitst)
835 {
836   long i;
837   Bitstring temp1(*this),temp2(bitst);
838
839  //make size bitst equal to *this
840   if (lengthBlock > bitst.lengthBlock)
841     temp2.Resize(length);
842
843   //bitwise xor
844   for (i = 0; i<lengthBlock ; i++ )
845     temp1[i] ^= temp2[i];
846
847   return temp1;
848 }
849
850 /***********************************************************************
851 * Member:  operator ~ ()
852 * Purpose: Overloads the bitwise not operator. Does a bitwise not with
853 *          "bitstr".
854 * Return:  bitwised not -> ~(*this)
855 ***********************************************************************/
856 Bitstring Bitstring::operator ~()
857 {
858   long i;
859   Bitstring temp1(*this);
860
861
862   //bitwise not
863   for (i = 0; i<lengthBlock ; i++ )
864     temp1[i] = ~temp1[i];
865
866   temp1.Resize(this->Length());
867   return temp1;
868
869 }
870
871
872
873 /***********************************************************************
874 * Member:  operator << (ostream& outs, Bitstring &strout)
875 * Purpose: Overloads the stream output operator. Converts the bitstring to a
876 *          C-string and returns it to "ostream".
877 * Return:  outs -> converted bitstring
878 ***********************************************************************/
879 ostream& operator << (ostream& outs, const Bitstring &strout)
880 {
881   // convert bitstring
882
883 /*
884   char tmp[256];
885   strout.BitstrToString(tmp);
886   outs << tmp;
887 */
888   char tmp[9];
889   for (int i = 0; i < strout.lengthBlock;i++) {
890     sprintf(tmp,"%08x",strout[i]);
891     outs << tmp;
892   }
893   // outs << hex << setw(8) << setfill('0') << strout[i];
894   return outs;
895 }
896
897
898 /***********************************************************************
899 * Member:  operator >> (istream& ins, Bitstring &instr)
900 * Purpose: Overloads the stream input operator. Converts the C-string to a
901 *          bitstring.
902 * Return:  ins
903 ***********************************************************************/
904 istream& operator >> (istream& ins, Bitstring &instr)
905 {
906   char str[255];
907
908   cin>> str;
909   instr.StringToBitstr(str);
910
911   return ins;
912 }