reverse a given number only using bitwise operation e.g.: input: 4532 output : 2354 I'm not able to think of any bitwise operations to tackle this question.Any solution/assistance would be of great help. The division and modulo by 10 needs to be performed using bitwise operations only.
- 1Does this answer your question? Reversing a Number using bitwise shiftuser– user2020-09-23 18:07:10 +00:00Commented Sep 23, 2020 at 18:07
- 4Are you sure you need to reverse the number in base 10 (as opposed to binary) using bitwise (as opposed to basic arithmetic) operations? I'm pretty sure there is no elegant way of doing that.Mo B.– Mo B.2020-09-23 18:30:26 +00:00Commented Sep 23, 2020 at 18:30
- 1Does "only using bitwise operation" mean not using for, while, if, ... ? please explain what does it mean?AKL– AKL2020-09-23 18:46:44 +00:00Commented Sep 23, 2020 at 18:46
- 1You can do it with bitwise operations, but you need to implement division_by_10_with_remainder using shift/compare/subtract. All of those can be done in bitwise fashion, and we'll see you next month ;)user3386109– user33861092020-09-23 21:00:09 +00:00Commented Sep 23, 2020 at 21:00
- 3en.wikipedia.org/wiki/Double_dabble and cypress.com/file/42131/downloaduser3528438– user35284382020-09-24 07:49:54 +00:00Commented Sep 24, 2020 at 7:49
| Show 2 more comments
1 Answer
there's no efficient way of reversing a number using only bitwise operators (mostly shift operators). But we can use BCD conversion to convert the integer to BCD and then reverse using shift operators. So after BCD conversion we can shift every set of 4 bits (representing a digit ranging from 0-9) step wise in the opposite order, to get the reversed Number in BCD format.