I need to do a circular right shift to get 4552 from 5524 or 22266 from 22662 or 82719 from 27198. How it is possible to obtain such shift? I am puzzled on how to approach this problem. Thank you¡
On the assumption that these are single integers, you could turn them into a string, go through the string copying everything to the right (be sure not to overwrite a value before you've moved it along), a single edge case to copy the one from the end back over the one at the start, and then turn that string back into an int.
Two points:
1. With int rightShift( int x ) we would have to take care of x being negative
2. The result of the circular shift of an int may be outside the range of int (undefined behaviour) http://coliru.stacked-crooked.com/a/112b9172e0e19aaa
Good points. Those are flaws.
The other flaw, low initial value for tenPow (1) caused trouble when ltDigits = 1.
before: rightShift( 14 ) --> 5
after: rightShift( 14 ) --> 41
OK. I can at least fix the negative value problem.I don't see what I can do about the out of range issue, so I won't address it. Avoid 10 1 + std::numeric_limits<int>::digits10 digit numbers?