I’ve been trying to port some JavaScript code to Lua. The code encodes and decodes data to and from a binary stream. I am having an issue when trying to port some JavaScript code that can contain signed integers. The JavaScript code looks as such: This code is based on code found in this library: https://github.com/hathora/bin-serde-ts/blob/develop/index.ts I’ve added simplified versions
Tag: bit-manipulation
Javascript’s Shift right with zero-fill operator (>>>) yielding unexpected result
First, (-1 >>> 0) === (2**32 – 1) which I expect is due to adding a new zero to the left, thus converting the number into 33-bit number? But, Why is (-1 >>> 32) === (2**32 – 1) as well, while I expect it (after shifting the 32-bit number 32 times and replacing the Most Significant Bits with zeros) to
How to remove the nth hexadecimal digit of a integer number without using Strings?
Consider a hexadecimal integer value such as n = 0x12345, how to get 0x1235 as result by doing remove(n, 3) (big endian)? For the inputs above I think this can be achieved by performing some bitwising steps: partA = extract the part from index 0 to targetIndex – 1 (should return 0x123); partB = extract the part from targetIndex +
Unsigned Right Shift / Zero-fill Right Shift / >>> in PHP (Java/JavaScript equivalent)
Before flagging this as a duplicate, please read below, and check my code * my updated code! So my problem is that, I have to implement Java/JavaScript ‘>>>’ (Unsigned Right Shift / Zero-fill Right Shift), but I can’t get it work exactly the same way. I’ve selected the 11 most promising implementations I’ve found on SO and on the web