Implemented same workaround for mask length == 64 as in most other languages

This commit is contained in:
Mikhail Yakshin 2017-03-01 01:54:56 +03:00
parent edb7b09c96
commit f925128b6b
1 changed files with 14 additions and 2 deletions

View File

@ -296,7 +296,7 @@ namespace Kaitai
} }
// raw mask with required number of 1s, starting from lowest bit // raw mask with required number of 1s, starting from lowest bit
ulong mask = (1UL << n) - 1; ulong mask = GetMaskOnes(n);
// shift mask to align with highest bits available in "bits" // shift mask to align with highest bits available in "bits"
int shiftBits = BitsLeft - n; int shiftBits = BitsLeft - n;
mask = mask << shiftBits; mask = mask << shiftBits;
@ -304,12 +304,24 @@ namespace Kaitai
ulong res = (Bits & mask) >> shiftBits; ulong res = (Bits & mask) >> shiftBits;
// clear top bits that we've just read => AND with 1s // clear top bits that we've just read => AND with 1s
BitsLeft -= n; BitsLeft -= n;
mask = (1UL << BitsLeft) - 1; mask = GetMaskOnes(BitsLeft);
Bits &= mask; Bits &= mask;
return res; return res;
} }
private static ulong GetMaskOnes(int n)
{
if (n == 64)
{
return 0xffffffffffffffffUL;
}
else
{
return (1UL << n) - 1;
}
}
#endregion #endregion
#region Byte arrays #region Byte arrays