@@ -73,7 +73,9 @@ if tonumber(string.match(_VERSION, "%d.%d+"))*10 > 51 then
7373end
7474
7575--[[ Bitwise operations]]
76- local function bwAnd (int1 , int2 )
76+ local UINT32_MAX = 0xFFFFFFFF ;-- 32 bit word
77+ local WORD_MASK = UINT32_MAX ;
78+ local function bwAnd (int1 , int2 ) -- TODO: enforce uint32 params!
7779 if lua_version_int < 53 then
7880 return bit32.band (int1 , int2 );
7981 else
@@ -86,7 +88,7 @@ local function bwLshift(int1, int2)
8688 return int1 * math.pow (2 ,int2 );
8789 -- return bit32.lshift(int1, int2);
8890 else
89- return int1 . __shl (int2 );
91+ return int1 : __shl (int2 );
9092 end
9193end
9294
@@ -168,8 +170,6 @@ local function typeof(obj)
168170end
169171
170172--[[ Two's complement of a 64 bit value represented by two 4-byte values]]
171- local UINT32_MAX = 0xFFFFFFFF ;-- 32 bit word
172- local WORD_MASK = UINT32_MAX ;
173173local function twosComplement (low_word , high_word )
174174 local new_low_word = bwAnd (bwNot (low_word ), WORD_MASK ) + 1 ;
175175 local new_high_word = bwAnd (bwNot (high_word ), WORD_MASK );
@@ -373,7 +373,8 @@ function wirebait.UInt64.new(num, high_num)
373373 function uint_64 :__shr (shift ) --[[ bitwise right shift (>>)]]
374374 assert (type (shift ) == " number" and shift == math.floor (shift ), " The shift must be an integer!" )
375375 if shift < 32 then
376- local new_low_word = bwRshift (self .m_low_word , shift ) + bwAnd (bwLshift (self .m_high_word , (32 - shift )), WORD_MASK );
376+ -- local new_low_word = bwRshift(self.m_low_word, shift) + bwAnd(bwLshift(self.m_high_word, (32-shift)), WORD_MASK);
377+ local new_low_word = bwRshift (self .m_low_word , shift ) + bwLshift (bwAnd (self .m_high_word , tonumber (" 0" .. string.rep (" 1" , shift ), 2 )), 32 - shift ); -- TODO: super hacky, fix this!
377378 return wirebait .UInt64 .new (new_low_word , bwRshift (self .m_high_word , shift ));
378379 elseif shift < 64 then
379380 return wirebait .UInt64 .new (bwAnd (bwLshift (self .m_high_word , (shift - 32 )), WORD_MASK ), 0 );
@@ -1253,7 +1254,9 @@ function wirebait.buffer.new(data_as_hex_string)
12531254 if length <= 32 then
12541255 local uint_val = self (byte_offset , byte_size ):uint64 ();
12551256 local bit_mask = tonumber (string.rep (" 1" , length ),2 );
1256- return bwAnd (bwRshift (uint_val , right_bits_count ), bit_mask );
1257+ -- return bwAnd(bwRshift(uint_val, right_bits_count), bit_mask);
1258+ return (uint_val :rshift (right_bits_count )):band (bit_mask );
1259+
12571260 else
12581261 local high_bit_mask = tonumber (string.rep (" 1" , 32 - left_bits_count ),2 );-- << left_bits_count;
12591262 local bytes_as_uint64 = wirebait .UInt64 .fromHex (self (byte_offset , byte_size ):bytes ());
0 commit comments