4 ms·
If it's signed int, unless you know the value is positive you can't just shift right 1--if the result of the shift is negative, you have to add 1.
by jejones3141 6y ago
If it's signed int, unless you know the value is positive you can't just shift right 1--if the result of the shift is negative, you have to add 1.
- chrisseaton 6y agoCan't you do an arithmetic shift-right? That takes the sign into account.
- xxpor 6y agoYep, ASR is the ARM64 instruction.
- jcranmer 6y agoNo. An arithmetic shift right does a division that rounds down; an integer division operation instead truncates (rounds to zero). The easiest example is -1 / 2: -1 / 2 is 0, but -1 ashr 1 is -1. To replace a signed division with ashr, you have to know that for all negative inputs, the value of the bits shifted out are all 0.