41template<
size_t PLACE = 8>
63 m_num[i] = other.m_num[i];
104 bool isMinus =
false;
107 if ( i == 0 && str[i] ==
'-' )
111 else if ( str[i] <
'0' || str[i] >
'9' )
121 str = str.
Mid(1, len);
176 if ( j >= DWORD_LEN )
181 DWORD k = (c & 0xFF) << ((i & 7) * 4);
199 if ( size > BYTE_LEN )
203 const BYTE* P1 =
static_cast<const BYTE*
>(lpRawData);
204 BYTE* P2 =
reinterpret_cast<BYTE*
>(m_num);
219 _rawData.
Resize(BYTE_LEN);
220 const BYTE* P1 =
reinterpret_cast<const BYTE*
>(m_num);
221 BYTE* P2 = _rawData.
Ref();
236 return reinterpret_cast<const BYTE*
>(m_num);
247 size_t l = bitNo / 32;
250 return (m_num[l] &
_BIT(bitNo % 32)) != 0;
262 size_t l = bitNo / 32;
267 m_num[l] |=
_BIT(bitNo % 32);
271 m_num[l] &= ~_BIT(bitNo % 32);
283 loop ( i, DWORD_LEN )
285 m_num[i] = other.m_num[i];
297 LONGLONG* P =
reinterpret_cast<LONGLONG*
>(m_num);
299 DWORD u = (val < 0) ? DWORD_MAX : 0;
300 for (
size_t i = 2 ; i < DWORD_LEN; i++ )
325 loop ( i, DWORD_LEN )
327 x.m_num[i] = ~m_num[i];
329 if ( x.m_num[0] == DWORD_MAX )
333 for (
size_t i = 1; i < DWORD_LEN; i++ )
335 if ( x.m_num[i] == DWORD_MAX )
360 loop ( i, DWORD_LEN )
362 x.m_num[i] = ~m_num[i];
388 loop ( i, DWORD_LEN )
390 bool isBorrow =
true;
391 if ( m_num[i] >= val.m_num[i] )
395 m_num[i] -= val.m_num[i];
397 while ( isBorrow && j < DWORD_LEN )
459 loop ( i, DWORD_LEN )
461 m_num[i] &= val.m_num[i];
474 loop ( i, DWORD_LEN )
476 m_num[i] |= val.m_num[i];
489 loop ( i, DWORD_LEN )
491 m_num[i] ^= val.m_num[i];
508 for (
int i = DWORD_LEN - 1; i > 0; i-- )
510 m_num[i] = m_num[i - 1];
517 for (
int i = DWORD_LEN - 1; i > 0; i-- )
519 m_num[i] = (m_num[i] << 1) | ((m_num[i - 1] & 0x80000000) ? 1 : 0);
540 loop ( i, DWORD_LEN - 1 )
542 m_num[i] = m_num[i + 1];
544 m_num[DWORD_LEN - 1] = 0;
549 loop ( i, DWORD_LEN - 1 )
551 m_num[i] = (m_num[i] >> 1) | ((m_num[i + 1] & 1) ? 0x80000000 : 0);
553 m_num[DWORD_LEN - 1] >>= 1;
594 if ( m_num[0] == DWORD_MAX )
596 for (
size_t i = 1; i < DWORD_LEN; i++ )
599 if ( m_num[i] != DWORD_MAX )
630 for (
size_t i = 1; i < DWORD_LEN; i++ )
690 const BYTE* pV1 =
reinterpret_cast<const BYTE*
>(a.m_num);
691 const BYTE* pV2 =
reinterpret_cast<const BYTE*
>(b.m_num);
692 BYTE* pWork =
reinterpret_cast<BYTE*
>(w.
Ref());
702 BYTE* P = &pWork[ix];
703 DWORD kk = pV1[j] * pV2[i] + *P;
704 *P++ =
static_cast<BYTE
>(kk & 0xFF);
707 while ( (kk != 0) && (ix < (BYTE_LEN * 2)) )
710 *P++ =
static_cast<BYTE
>(kk & 0xFF);
719 loop ( i, DWORD_LEN )
736 m_Divide(a, b, quotient, remainder);
750 m_Divide(a, b, quotient, remainder);
818 return m_Divide(*
this, val, _quotient, _remainder);
830 INT_PTR sign1 =
IsMinus() ? -1 : 1;
831 INT_PTR sign2 = t.
IsMinus() ? -1 : 1;
832 if ( sign1 == sign2 )
834 for (
int i = DWORD_LEN - 1; i >= 0; i-- )
836 if ( m_num[i] > t.m_num[i] )
840 else if ( m_num[i] < t.m_num[i] )
861 return (m_num[DWORD_LEN - 1] & 0x80000000) != 0;
872 loop ( i, DWORD_LEN )
890 return m_CanExpress(1);
913 return m_CanExpress(2);
925 LONGLONG r = m_num[1];
943 if ( (m_num[i] &
_BIT(j)) != 0 )
945 return i * 32 + j + 1;
962 LPCTSTR lpszFmt = isUpperCase ? _T(
"%08X") : _T(
"%08x");
963 loop ( i, DWORD_LEN )
965 s +=
CStr::Fmt(lpszFmt, m_num[DWORD_LEN - 1 - i]);
1008 size_t l = DWORD_LEN;
1026 if ( l1 != l2 || l1 != DWORD_LEN )
1072 loop ( i, DWORD_LEN )
1074 x.m_num[i] = DWORD_MAX;
1076 x.m_num[DWORD_LEN - 1] = 0x7FFFFFFF;
1089 x.m_num[DWORD_LEN - 1] = 0x80000000;
1154 for ( INT_PTR i = 0; i <= cnt; i++ )
1160 _quotient.m_num[0] |= 1;
1166 _quotient = -_quotient;
1171 _remainder = -_remainder;
1176 bool m_CanExpress(INDEX idx)
const
1178 DWORD n = m_num[idx];
1179 if ( n == 0 || n != DWORD_MAX )
1181 for (
size_t i = idx + 1; i < DWORD_LEN; i++ )
1183 if ( n != m_num[i] )
1202 BYTE_LEN = PLACE * 4,
1203 BIT_LEN = PLACE * 32,
1204 INT_VALUE_MAX = 1000000000,
1206 DWORD m_num[DWORD_LEN];
1207 friend class CLongIntegerTest;
#define loop_dn(VAR, CNT)
loop構文.
#define loop(VAR, CNT)
loop構文.
size_t SetHexString(LPCTSTR lpsz)
[代入] 値代入.
void SetBit(INDEX bitNo, bool v=true)
[設定] ビット設定.
CLongIntegerT & operator^=(const CLongIntegerT &val)
[代入] XOR 算代入.
bool GetBit(INDEX bitNo) const
[取得] ビット取得.
CLongIntegerT & operator<<=(size_t val)
[代入] 左シフト代入.
bool CanExpressLonglong(void) const
[確認] 保持値確認.
virtual void Deserialize(const IDeserializer &ds)
[処理] デシリアライズ
CLongIntegerT operator>>(size_t val) const
[計算] 右シフト.
int ToInt(void) const
[取得] int 取得.
CLongIntegerT operator-(void) const
[計算] マイナス符号.
bool Divide(CLongIntegerT &_quotient, CLongIntegerT &_remainder, const CLongIntegerT &val) const
[計算] 除算.
CLongIntegerT & operator--(void)
[代入] デクリメント.
bool IsZero(void) const
[確認] ゼロ確認.
size_t SetString(LPCTSTR lpsz)
[代入] 値代入.
static CLongIntegerT FromString(LPCTSTR lpsz)
[取得] 変換値取得.
CLongIntegerT & operator|=(const CLongIntegerT &val)
[代入] OR 算代入.
LONGLONG ToLonglong(void) const
[取得] LONGLONG 取得.
CLongIntegerT & operator&=(const CLongIntegerT &val)
[代入] AND 算代入.
CLongIntegerT operator+(void) const
[計算] プラス符号.
CLongIntegerT operator~(void) const
[計算] ビット反転.
friend CLongIntegerT operator|(const CLongIntegerT &a, const CLongIntegerT &b)
[計算] OR 算.
bool CanExpressInt(void) const
[確認] 保持値確認.
static CLongIntegerT FromHexString(LPCTSTR lpsz)
[取得] 変換値取得.
CLongIntegerT(void)
コンストラクタ.
friend CLongIntegerT abs(const CLongIntegerT &val)
[計算] 絶対値.
CLongIntegerT & operator/=(const CLongIntegerT &val)
[代入] 除算代入.
CLongIntegerT & operator++(void)
[代入] インクリメント.
CStr ToHexString(bool isUpperCase=true) const
[取得] HEX文字列化.
friend CLongIntegerT operator-(const CLongIntegerT &a, const CLongIntegerT &b)
[計算] 減算.
const BYTE * RefRawData(void) const
[参照] 生データ参照.
friend CLongIntegerT operator*(const CLongIntegerT &a, const CLongIntegerT &b)
[計算] 乗算.
friend CLongIntegerT operator%(const CLongIntegerT &a, const CLongIntegerT &b)
[計算] 余り算.
void Zero(void)
[代入] ゼロ代入.
void GetRawData(CWorkMem &_rawData) const
[取得] 生データ取得.
static CLongIntegerT GetMin(void)
[取得] 最小値取得.
CLongIntegerT & operator=(const CLongIntegerT &other)
コピーオペレータ.
size_t GetValidBitsCount(void) const
[取得] 有効 bit 数取得.
virtual INT_PTR Compare(const CLongIntegerT< PLACE > &t) const
[確認] 比較
CLongIntegerT & operator+=(const CLongIntegerT &val)
[代入] 加算代入.
CLongIntegerT operator--(int)
[代入] デクリメント.
friend CLongIntegerT operator^(const CLongIntegerT &a, const CLongIntegerT &b)
[計算] XOR 算.
CLongIntegerT & operator*=(const CLongIntegerT &val)
[代入] 乗算代入.
CLongIntegerT & operator>>=(size_t val)
[代入] 右シフト代入.
virtual void Serialize(ISerializer &_sr) const
[処理] シリアライズ
static size_t GetBytesCount(void)
[取得] byte 数取得.
bool IsMinus(void) const
[確認] マイナス確認.
CStr ToString(void) const
[取得] 文字列化.
CLongIntegerT operator++(int)
[代入] インクリメント.
CLongIntegerT(const CLongIntegerT &other)
コピーコンストラクタ.
static CLongIntegerT GetMax(void)
[取得] 最大値取得.
CLongIntegerT & operator%=(const CLongIntegerT &val)
[代入] 余り算代入.
static size_t GetBitsCount(void)
[取得] bit 数取得.
friend CLongIntegerT operator+(const CLongIntegerT &a, const CLongIntegerT &b)
[計算] 加算.
CLongIntegerT(LONGLONG val)
代入コンストラクタ.
CLongIntegerT & operator-=(const CLongIntegerT &val)
[代入] 減算代入.
friend CLongIntegerT operator/(const CLongIntegerT &a, const CLongIntegerT &b)
[計算] 除算.
void SetRawData(size_t size, LPCVOID lpRawData)
[設定] 生データ設定.
CLongIntegerT & operator=(LONGLONG val)
代入オペレータ.
CLongIntegerT operator<<(size_t val) const
[計算] 左シフト.
friend CLongIntegerT operator&(const CLongIntegerT &a, const CLongIntegerT &b)
[計算] AND 算.
static size_t GetMaxPlace(void)
[取得] 最大桁数取得.
CStrT & TrimLeft(TYP t=' ')
[処理] 先頭から文字をトリム.
bool IsEmpty(void) const
[確認] 空チェック
CStrT Left(size_t iSize) const
[作成] 範囲取得.
size_t GetLength(void) const
[取得] 文字列長
CStrT Right(INT_PTR iSize) const
[作成] 範囲取得.
static CStrT Fmt(const TCHAR *lpszFormat,...)
[作成] 書式付き文字列作成
CStrT Mid(INDEX iOffset, size_t iSize=INVALID_SIZE) const
[作成] 範囲取得.
int ToInt(INDEX iOffset=0, int iBase=10) const
[取得] 数値(int)へ変換
void Fill(const TYP &other)
[設定] 全情報代入.
void Resize(size_t l)
[設定] サイズ再設定
const TYP * Ref(void) const
[取得] ポインタ取得
size_t GetLen(LPCSTR lpsz)
[計算] 文字列長計算(ASCII/SJIS用)
int HexCharToInt(int c)
[変換] HEX文字数値変換
CLongIntegerT< 8 > INT256
256bit 整数管理