📄 codeword.cc
字号:
ExtraLongUInt::ExtraLongUInt(int val){ assert(val >= 0); memset(value, 0, sizeof(value)); value[0] = val;}// constructor:ExtraLongUInt::ExtraLongUInt(unsigned int val){ memset(value, 0, sizeof(value)); value[0] = val;}// constructor:ExtraLongUInt::ExtraLongUInt(unsigned long val){ memset(value, 0, sizeof(value)); value[0] = val;}bool ExtraLongUInt::operator == (const ExtraLongUInt& other) const{ for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) { if(value[i] != other.value[i]) { return false; } } return true;}bool ExtraLongUInt::operator != (const ExtraLongUInt& other) const{ return (*this == other) ? false : true;}bool ExtraLongUInt::operator < (const ExtraLongUInt& other) const{ unsigned int i; for(i = sizeof(value) / sizeof(unsigned long) - 1; value[i] == other.value[i] && i > 0; --i) { } return value[i] < other.value[i] ? true : false;}bool ExtraLongUInt::operator > (const ExtraLongUInt& other) const{ unsigned int i; for(i = sizeof(value) / sizeof(unsigned long) - 1; value[i] == other.value[i] && i > 0; --i) { } return value[i] > other.value[i] ? true : false;}bool ExtraLongUInt::operator <= (const ExtraLongUInt& other) const{ return (*this > other) ? false : true;}bool ExtraLongUInt::operator >= (const ExtraLongUInt& other) const{ return (*this < other) ? false : true;}ExtraLongUInt ExtraLongUInt::operator + (const ExtraLongUInt& other) const{ ExtraLongUInt res = 0; unsigned long c = 0; const int shift = 8 * sizeof(unsigned long) - 1; const unsigned long msb_mask = (unsigned long) 1 << shift; const unsigned long lsbs_mask = ~msb_mask; unsigned long x, y; for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) { x = value[i]; y = other.value[i]; res.value[i] = (x & lsbs_mask) + (y & lsbs_mask) + c; c = ((x >> shift) + (y >> shift) + (res.value[i] >> shift)) >> 1; res.value[i] ^= (x & msb_mask) ^ (y & msb_mask); } return res;}ExtraLongUInt ExtraLongUInt::operator - (const ExtraLongUInt& other) const{ return *this + ~other + 1;}ExtraLongUInt ExtraLongUInt::operator & (const ExtraLongUInt& other) const{ ExtraLongUInt res; for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) { res.value[i] = value[i] & other.value[i]; } return res;}ExtraLongUInt ExtraLongUInt::operator | (const ExtraLongUInt& other) const{ ExtraLongUInt res; for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) { res.value[i] = value[i] | other.value[i]; } return res;}ExtraLongUInt ExtraLongUInt::operator ^ (const ExtraLongUInt& other) const{ ExtraLongUInt res; for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) { res.value[i] = value[i] ^ other.value[i]; } return res;}ExtraLongUInt ExtraLongUInt::operator ~() const{ ExtraLongUInt res; for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) { res.value[i] = ~value[i]; } return res;}bool ExtraLongUInt::operator ! () const{ for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) { if(value[i]) { return false; } } return true;}ExtraLongUInt ExtraLongUInt::operator << (unsigned int bits) const{ ExtraLongUInt res = 0; const int index = bits / (8 * sizeof(unsigned long)); const int shifts = bits % (8 * sizeof(unsigned long)); unsigned int i; if(sizeof(value) > index * sizeof(unsigned long)) { if(shifts == 0) { // this is because (1 >> 32) is not 0 (gcc) memcpy(&res.value[index], value, sizeof(value) - index * sizeof(unsigned long)); } else { const unsigned long mask = (~(unsigned long) 0) >> shifts; assert(sizeof(value) >= sizeof(unsigned long)); res.value[index] = (value[0] & mask) << shifts; for(i = index + 1; i < sizeof(value) / sizeof(unsigned long); ++i) { res.value[i] = ((value[i - index ] & mask) << shifts) | (value[i - index-1] >> ((8 * sizeof(unsigned long)) - shifts)); } } } return res;}ExtraLongUInt ExtraLongUInt::operator >> (unsigned int bits) const{ ExtraLongUInt res = 0; const int index = bits / (8 * sizeof(unsigned long)); const int shifts = bits % (8 * sizeof(unsigned long)); unsigned int i; if(sizeof(value) > index * sizeof(unsigned long)) { if(shifts == 0) { // this is because (1 >> 32) is not 0 (gcc) memcpy(res.value, &value[index], sizeof(value) - index * sizeof(unsigned long)); } else { const unsigned long mask = (~(unsigned long) 0) >> (8 * sizeof(unsigned long) - shifts); assert(sizeof(value) >= sizeof(unsigned long)); for(i = 0; i < sizeof(value) / sizeof(unsigned long) - index - 1; ++i) { res.value[i] = (value[i+index ] >> shifts) | ((value[i+index+1] & mask) << ((8 * sizeof(unsigned long)) - shifts)); } res.value[i] = value[i+index] >> shifts; } } return res;}ExtraLongUInt ExtraLongUInt::operator << (const ExtraLongUInt& bits) const{ return *this << bits.value[0];}ExtraLongUInt ExtraLongUInt::operator >> (const ExtraLongUInt& bits) const{ return *this >> bits.value[0];}const ExtraLongUInt& ExtraLongUInt::operator <<= (unsigned int bits){ *this = *this << bits; return *this;}const ExtraLongUInt& ExtraLongUInt::operator >>= (unsigned int bits){ *this = *this >> bits; return *this;}const ExtraLongUInt& ExtraLongUInt::operator &= (const ExtraLongUInt& other){ *this = *this & other; return *this;}const ExtraLongUInt& ExtraLongUInt::operator |= (const ExtraLongUInt& other){ *this = *this | other; return *this;}const ExtraLongUInt& ExtraLongUInt::operator ^= (const ExtraLongUInt& other){ *this = *this ^ other; return *this;}void ExtraLongUInt::print(char* buf) const{ int i, fin; for(i = 8 * sizeof(value) - 1; !(value[i / (8 * sizeof(unsigned long))] & ((unsigned long) 1 << (i % (8 * sizeof(unsigned long))))) && i > 0; --i) ; for(fin = i; i >= 0; --i) { buf[fin-i] = (value[i / (8 * sizeof(unsigned long))] & (unsigned long) 1 << (i % (8 * sizeof(unsigned long)))) ? '1' : '0'; } buf[fin+1] = '\0';}unsigned int ExtraLongUInt::bitcount() const{ unsigned int res = 0; for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) { res += ::bitcount(value[i]); } return res;}unsigned int ExtraLongUInt::minbit() const{ for(unsigned int i = 0; i < sizeof(value) / sizeof(unsigned long); ++i) { if(value[i]) { return i * 8 * sizeof(unsigned long) + ::minbit(value[i]); } } assert(false); // value is 0 return 0; // compiler, be quiet.}// returns bit position of least significant bit in cw_patunsigned int minbit(const ExtraLongUInt& val){ return val.minbit();}// returns bit position of least significant bit in cw_patunsigned int bitcount(const ExtraLongUInt& val){ return val.bitcount();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -