Users
I am trying to do a right or left shift of the Int32 digits (not a bit !!).
So, if the constant shift:
123456789
3
I have to get
789123456
Thus, the numbers are not lost, because we are talking about a circular shift. After a little testing, I came up with this method that works:
static uint[] Pow10 = new uint[] { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, uint.MaxValue }; static uint RotateShift10(uint value, int shift) { int r = (int)Math.Floor(Math.Log10(value) + 1); while (r < shift) shift = shift - r; if (shift < 0) shift = 9 + shift; uint x = value / Pow10[shift]; uint i = 0; while (true) { if (x < Pow10[i]) return x + (value % Pow10[shift]) * Pow10[i]; i += 1; } }
What I'm looking for should be an arithmetic solution, not a string conversion, and then a rotation. I also assume that:
- Int32 does not contain 0 digits to prevent the loss of digits.
- Int32 - non-negative number
- A positive integer rotation should shift to the right, and a negative integer should shift to the left.
My algorithm already does all this, and I like to know if there is a way to fine-tune it a bit if there is a better arithmetic solution to the problem?
source share