Convert 8-bit signed numbers to 64-bit unsigned numbers

Today, I encountered a pit in the company's big data analysis. Some data became very large inexplicably:


 The reason is finally located in this strong turn here


When an 8-bit signed number is extended to a 64-bit unsigned number, the underlying assembly actually uses the movsx sign extension instruction, which means that the extended bits are filled with sign bits, which results in a high value of 58 The bits are all 1, so the value is abnormally large .

Let's do a simple test again:



The binary value of -29 and 157 is the same 10011101. Why the value after forced conversion is different? The reason is that the negative number of computer memory is complemented, so in fact, the memory of -29 is 11100011, and the memory of 157 is 10011101. After converting to unsigned 64 bits, they are the values ​​in the above figure.

 Here's how I solve this problem, first force the type, and then take the content, which is equivalent to converting from unsigned to unsigned type. The MOVZX instruction used at the bottom layer is filled with zeros during the conversion process.


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326997932&siteId=291194637