When negative numbers in 2-complement get converted from 16-bit (Arduino) to 32-bit (JavaScript), the sign bit has to be at bit 0, not at bit 17, as it was with the old code, which implicitly padded 16 zeroes to the left. As a result, the negative number -1 returns 65535.
By prefixing a 16-bit negative number with 16 ones, the correct 32-bit 2-complement number is constructed.
When negative numbers in 2-complement get converted from 16-bit (Arduino) to 32-bit (JavaScript), the sign bit has to be at bit 0, not at bit 17, as it was with the old code, which implicitly padded 16 zeroes to the left. As a result, the negative number -1 returns 65535.
By prefixing a 16-bit negative number with 16 ones, the correct 32-bit 2-complement number is constructed.