Talk:RightShift - Math
Multiplication/Division Based Shifting[edit source]
Is it worth mentioning that you can also perform left and right shifts of non-negative values through simple multiplication and division by powers of two? This may be faster unless SKSE's function calls somehow operate without context switching.
For example, to right-shift non-negative values you can simply do:
int iA = 2146 / 4 ;iA == 536
int iC = 0x00802000 / 512) ;iC == 16400
Likewise you can left-shift non-negative values like so:
int iA = 52 * 32 ;iA == 1664
int iC = 0x00070 * 2 ;iC == 224
Basically what I'm doing here is taking the shift in number of bits, and getting the equivalent power of 2, so 1 is 1 ^ 2 = 2, 2 is 2 ^ 2 = 4, 3 is 3 ^ 2 = 8 and so-on.
NOTE: This only works properly for non-negative values, as multiplication and division preserve the negative bit leading to unexpected results. But if your aim is to pack data into fewer than 32-bits for example, then this avoids the need for a function call, and potentially any calls at all. Haravikk (talk) 2020-01-16T10:13:17 (EST)