diff --git a/KaitaiStream.cs b/KaitaiStream.cs index 5a1bf06..936dd81 100644 --- a/KaitaiStream.cs +++ b/KaitaiStream.cs @@ -307,11 +307,9 @@ namespace Kaitai // raw mask with required number of 1s, starting from lowest bit ulong mask = GetMaskOnes(n); - // shift mask to align with highest bits available in "bits" + // shift "bits" to align the highest bits with the mask & derive reading result int shiftBits = BitsLeft - n; - mask = mask << shiftBits; - // derive reading result - ulong res = (Bits & mask) >> shiftBits; + ulong res = (Bits >> shiftBits) & mask; // clear top bits that we've just read => AND with 1s BitsLeft -= n; mask = GetMaskOnes(BitsLeft);