Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

ExtraLongUInt Class Reference

#include <codeword.h>

Collaboration diagram for ExtraLongUInt:

Collaboration graph
[legend]
List of all members.

Public Types

enum  { size = 2 }

Public Member Functions

 ExtraLongUInt ()
 ExtraLongUInt (const char *val)
 ExtraLongUInt (int val)
 ExtraLongUInt (unsigned int val)
 ExtraLongUInt (unsigned long val)
bool operator== (const ExtraLongUInt &other) const
bool operator!= (const ExtraLongUInt &other) const
bool operator< (const ExtraLongUInt &other) const
bool operator> (const ExtraLongUInt &other) const
bool operator<= (const ExtraLongUInt &other) const
bool operator>= (const ExtraLongUInt &other) const
ExtraLongUInt operator+ (const ExtraLongUInt &other) const
ExtraLongUInt operator- (const ExtraLongUInt &other) const
ExtraLongUInt operator & (const ExtraLongUInt &other) const
ExtraLongUInt operator| (const ExtraLongUInt &other) const
ExtraLongUInt operator^ (const ExtraLongUInt &other) const
ExtraLongUInt operator~ () const
bool operator! () const
ExtraLongUInt operator<< (unsigned int bits) const
ExtraLongUInt operator<< (const ExtraLongUInt &bits) const
ExtraLongUInt operator>> (unsigned int bits) const
ExtraLongUInt operator>> (const ExtraLongUInt &bits) const
const ExtraLongUIntoperator<<= (unsigned int bits)
const ExtraLongUIntoperator>>= (unsigned int bits)
const ExtraLongUIntoperator &= (const ExtraLongUInt &other)
const ExtraLongUIntoperator|= (const ExtraLongUInt &other)
const ExtraLongUIntoperator^= (const ExtraLongUInt &other)
void print (char *buf) const
unsigned int bitcount () const
unsigned int minbit () const

Protected Attributes

unsigned long value [size]

Member Enumeration Documentation

anonymous enum
 

Enumeration values:
size 

Definition at line 62 of file codeword.h.

00062 { size = 2 };            // determines size of value (in units of unsigned long).


Constructor & Destructor Documentation

ExtraLongUInt::ExtraLongUInt  ) 
 

Definition at line 257 of file codeword.cc.

References value.

00258 {
00259     memset(value, 0, sizeof(value));
00260 }

ExtraLongUInt::ExtraLongUInt const char *  val  ) 
 

Definition at line 263 of file codeword.cc.

References len, and value.

00264 {
00265     const unsigned int len = strlen(val);
00266     char digit;
00267 
00268     assert(len <= 8 * sizeof(value));  // or else overflow
00269     memset(value, 0, sizeof(value));
00270     for(unsigned int i = 0; i < len; ++i) {
00271         digit = val[len - 1 - i];
00272         assert(digit == '0' || digit == '1'); // or else val is not a binary number
00273         if(digit == '1') {
00274             value[i / (8 * sizeof(unsigned long))] |=
00275                 (unsigned long) 1 << (i % (8 * sizeof(unsigned long)));
00276         }
00277     }
00278 }

ExtraLongUInt::ExtraLongUInt int  val  ) 
 

Definition at line 281 of file codeword.cc.

References value.

00282 {
00283     assert(val >= 0);
00284     memset(value, 0, sizeof(value));
00285     value[0] = val;
00286 }

ExtraLongUInt::ExtraLongUInt unsigned int  val  ) 
 

Definition at line 289 of file codeword.cc.

References value.

00290 {
00291     memset(value, 0, sizeof(value));
00292     value[0] = val;
00293 }

ExtraLongUInt::ExtraLongUInt unsigned long  val  ) 
 

Definition at line 296 of file codeword.cc.

References value.

00297 {
00298     memset(value, 0, sizeof(value));
00299     value[0] = val;
00300 }


Member Function Documentation

unsigned int ExtraLongUInt::bitcount  )  const
 

Definition at line 526 of file codeword.cc.

References value.

Referenced by bitcount().

00527 {
00528     unsigned int res = 0;
00529     for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) {
00530         res += ::bitcount(value[i]);
00531     }
00532     return res;
00533 }

unsigned int ExtraLongUInt::minbit  )  const
 

Definition at line 535 of file codeword.cc.

References value.

Referenced by minbit().

00536 {
00537     for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) {
00538         if(value[i]) {
00539             return i * 8 * sizeof(unsigned long) + ::minbit(value[i]);
00540         }
00541     }
00542     assert(false); // value is 0
00543     return 0;      // compiler, be quiet.
00544 }

ExtraLongUInt ExtraLongUInt::operator & const ExtraLongUInt other  )  const
 

Definition at line 371 of file codeword.cc.

References value.

00372 {
00373     ExtraLongUInt res;
00374 
00375     for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) {
00376         res.value[i] = value[i] & other.value[i];
00377     }
00378     return res;
00379 }

const ExtraLongUInt & ExtraLongUInt::operator &= const ExtraLongUInt other  ) 
 

Definition at line 493 of file codeword.cc.

00494 {
00495     *this = *this & other;
00496     return *this;
00497 }

bool ExtraLongUInt::operator!  )  const
 

Definition at line 411 of file codeword.cc.

References value.

00412 {
00413     for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) {
00414         if(value[i]) {
00415             return false;
00416         }
00417     }
00418     return true;
00419 }

bool ExtraLongUInt::operator!= const ExtraLongUInt other  )  const
 

Definition at line 312 of file codeword.cc.

00313 {
00314     return (*this == other) ? false : true;
00315 }

ExtraLongUInt ExtraLongUInt::operator+ const ExtraLongUInt other  )  const
 

Definition at line 347 of file codeword.cc.

References value.

00348 {
00349     ExtraLongUInt res = 0;
00350     unsigned long c = 0;
00351     const int shift = 8 * sizeof(unsigned long) - 1;
00352     const unsigned long msb_mask = (unsigned long) 1 << shift;
00353     const unsigned long lsbs_mask = ~msb_mask;
00354     unsigned long x, y;
00355 
00356     for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) {
00357         x = value[i];
00358         y = other.value[i];
00359         res.value[i] = (x & lsbs_mask) + (y & lsbs_mask) + c;
00360         c = ((x >> shift) + (y >> shift) + (res.value[i] >> shift)) >> 1;
00361         res.value[i] ^= (x & msb_mask) ^ (y & msb_mask);
00362     }
00363     return res;
00364 }

ExtraLongUInt ExtraLongUInt::operator- const ExtraLongUInt other  )  const
 

Definition at line 366 of file codeword.cc.

00367 {
00368     return *this + ~other + 1;
00369 }

bool ExtraLongUInt::operator< const ExtraLongUInt other  )  const
 

Definition at line 317 of file codeword.cc.

References value.

00318 {
00319     unsigned int i;
00320 
00321     for(i = sizeof(value) / sizeof(unsigned long) - 1;
00322         value[i] == other.value[i] && i > 0; --i) {
00323     }
00324     return value[i] < other.value[i] ? true : false;
00325 }

ExtraLongUInt ExtraLongUInt::operator<< const ExtraLongUInt bits  )  const
 

Definition at line 471 of file codeword.cc.

References value.

00472 {
00473     return *this << bits.value[0];
00474 }

ExtraLongUInt ExtraLongUInt::operator<< unsigned int  bits  )  const
 

Definition at line 421 of file codeword.cc.

References value.

00422 {
00423     ExtraLongUInt res = 0;
00424     const int index =  bits / (8 * sizeof(unsigned long));
00425     const int shifts = bits % (8 * sizeof(unsigned long));
00426     unsigned int i;
00427 
00428     if(sizeof(value) > index * sizeof(unsigned long)) {
00429         if(shifts == 0) { // this is because (1 >> 32) is not 0 (gcc)
00430             memcpy(&res.value[index],
00431                    value,
00432                    sizeof(value) - index * sizeof(unsigned long));
00433         } else {
00434             const unsigned long mask = (~(unsigned long) 0) >> shifts;
00435             assert(sizeof(value) >= sizeof(unsigned long));
00436             res.value[index] = (value[0] & mask) << shifts;
00437             for(i = index + 1; i < sizeof(value) / sizeof(unsigned long); ++i) {
00438                 res.value[i] = ((value[i - index  ] & mask) << shifts) |
00439                     (value[i - index-1]          >> ((8 * sizeof(unsigned long)) - shifts));
00440             }
00441         }
00442     }
00443     return res;
00444 }

const ExtraLongUInt & ExtraLongUInt::operator<<= unsigned int  bits  ) 
 

Definition at line 481 of file codeword.cc.

00482 {
00483     *this = *this << bits;
00484     return *this;
00485 }

bool ExtraLongUInt::operator<= const ExtraLongUInt other  )  const
 

Definition at line 337 of file codeword.cc.

00338 {
00339     return (*this > other) ? false : true;
00340 }

bool ExtraLongUInt::operator== const ExtraLongUInt other  )  const
 

Definition at line 302 of file codeword.cc.

References value.

00303 {
00304     for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) {
00305         if(value[i] != other.value[i]) {
00306             return false;
00307         }
00308     }
00309     return true;
00310 }

bool ExtraLongUInt::operator> const ExtraLongUInt other  )  const
 

Definition at line 327 of file codeword.cc.

References value.

00328 {
00329     unsigned int i;
00330 
00331     for(i = sizeof(value) / sizeof(unsigned long) - 1;
00332         value[i] == other.value[i] && i > 0; --i) {
00333     }
00334     return value[i] > other.value[i] ? true : false;
00335 }

bool ExtraLongUInt::operator>= const ExtraLongUInt other  )  const
 

Definition at line 342 of file codeword.cc.

00343 {
00344     return (*this < other) ? false : true;
00345 }

ExtraLongUInt ExtraLongUInt::operator>> const ExtraLongUInt bits  )  const
 

Definition at line 476 of file codeword.cc.

References value.

00477 {
00478     return *this >> bits.value[0];
00479 }

ExtraLongUInt ExtraLongUInt::operator>> unsigned int  bits  )  const
 

Definition at line 446 of file codeword.cc.

References value.

00447 {
00448     ExtraLongUInt res = 0;
00449     const int index =  bits / (8 * sizeof(unsigned long));
00450     const int shifts = bits % (8 * sizeof(unsigned long));
00451     unsigned int i;
00452 
00453     if(sizeof(value) > index * sizeof(unsigned long)) {
00454         if(shifts == 0) { // this is because (1 >> 32) is not 0 (gcc)
00455             memcpy(res.value,
00456                    &value[index],
00457                    sizeof(value) - index * sizeof(unsigned long));
00458         } else {
00459             const unsigned long mask = (~(unsigned long) 0) >> (8 * sizeof(unsigned long) - shifts);
00460             assert(sizeof(value) >= sizeof(unsigned long));
00461             for(i = 0; i < sizeof(value) / sizeof(unsigned long) - index - 1; ++i) {
00462                 res.value[i] = (value[i+index  ] >> shifts) |
00463                     ((value[i+index+1] & mask) << ((8 * sizeof(unsigned long)) - shifts));
00464             }
00465             res.value[i] = value[i+index] >> shifts;
00466         }
00467     }
00468     return res;
00469 }

const ExtraLongUInt & ExtraLongUInt::operator>>= unsigned int  bits  ) 
 

Definition at line 487 of file codeword.cc.

00488 {
00489     *this = *this >> bits;
00490     return *this;
00491 }

ExtraLongUInt ExtraLongUInt::operator^ const ExtraLongUInt other  )  const
 

Definition at line 391 of file codeword.cc.

References value.

00392 {
00393     ExtraLongUInt res;
00394 
00395     for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) {
00396         res.value[i] = value[i] ^ other.value[i];
00397     }
00398     return res;
00399 }

const ExtraLongUInt & ExtraLongUInt::operator^= const ExtraLongUInt other  ) 
 

Definition at line 505 of file codeword.cc.

00506 {
00507     *this = *this ^ other;
00508     return *this;
00509 }

ExtraLongUInt ExtraLongUInt::operator| const ExtraLongUInt other  )  const
 

Definition at line 381 of file codeword.cc.

References value.

00382 {
00383     ExtraLongUInt res;
00384 
00385     for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) {
00386         res.value[i] = value[i] | other.value[i];
00387     }
00388     return res;
00389 }

const ExtraLongUInt & ExtraLongUInt::operator|= const ExtraLongUInt other  ) 
 

Definition at line 499 of file codeword.cc.

00500 {
00501     *this = *this | other;
00502     return *this;
00503 }

ExtraLongUInt ExtraLongUInt::operator~  )  const
 

Definition at line 401 of file codeword.cc.

References value.

00402 {
00403     ExtraLongUInt res;
00404 
00405     for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) {
00406         res.value[i] = ~value[i];
00407     }
00408     return res;
00409 }

void ExtraLongUInt::print char *  buf  )  const
 

Definition at line 511 of file codeword.cc.

References value.

Referenced by MFTPRcvAgent::recv_data(), and MFTPSndAgent::send_data().

00512 {
00513     int i, fin;
00514 
00515     for(i = 8 * sizeof(value) - 1; !(value[i / (8 * sizeof(unsigned long))] &
00516                           ((unsigned long) 1 << (i % (8 * sizeof(unsigned long))))) && i > 0; --i)
00517         ;
00518     for(fin = i; i >= 0; --i) {
00519         buf[fin-i] = (value[i / (8 * sizeof(unsigned long))] &
00520                      (unsigned long) 1 << (i % (8 * sizeof(unsigned long)))) ?
00521                      '1' : '0';
00522     }
00523     buf[fin+1] = '\0';
00524 }


Member Data Documentation

unsigned long ExtraLongUInt::value[size] [protected]
 

Definition at line 65 of file codeword.h.

Referenced by bitcount(), ExtraLongUInt(), minbit(), operator &(), operator!(), operator+(), operator<(), operator<<(), operator==(), operator>(), operator>>(), operator^(), operator|(), operator~(), and print().


The documentation for this class was generated from the following files:
Generated on Tue Apr 20 12:46:21 2004 for NS2.26SourcesOriginal by doxygen 1.3.3