I have a number $v = f * 2^e$ where $f$ has up to nine digits, and $0 \le e \le 3$. I'm interested in splitting, for example, $v=123456789 * 2^2$ into three parts: $f * 2^2 \to 493, 827, 156$.
The intuitive (to me) operations are:
- $2^2 * v / 10^6$ % $10^3 = 493$
- $2^2 * v / 10^3$ % $10^3 = 827$
- $2^2 * v / 10^0$ % $10^3 = 156$
But I know (from an algorithm paper I'm reading) that you can do:
- $v / (10^3 / 2^2) / 10^3 = 493$
- $v / (10^3 / 2^2)$ % $10^3 = 827$
- $v$ % $(10^3 / 2^2) * 2^2 = 156$
The equivalence of the operations are not apparent to me. Can someone provide some clarity about how the operations are equivalent?
If down voting, please indicate to me in a comment how to improve my question or a more appropriate place to ask my question.
Which is $v$? Is it $123456789$ or $493827156$? If you are using integer divide, splitting it into three pieces is as easy as:
$v/10^6$ to get the top three,
$v\%10^3$ to get the bottom three and
$(v/10^3)\%10^3$ to get the middle three.
The paper is using $v=123456789$ because it divides the modulus by $2^2$. They will work because $2^2$ divides evenly into $10^3$. If it didn't, it would be better to multiply $v$ by $2^2$ before doing the divides.
Your operations will be close for $v=123456789$ but not correct. $v/10^6=123$, so when you multiply it by $2^2$ you get $492$, which carries through the rest unchanged. You lose the carry because you divided before multiplying by $4$.