简体   繁体   English

仅使用按位运算反转数字

[英]Reverse a number using bitwise operations only

reverse a given number only using bitwise operation eg: 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.仅使用按位运算反转给定数字,例如:输入: 4532输出: 2354 我想不出任何按位运算来解决这个问题。任何解决方案/帮助都会有很大帮助。 The division and modulo by 10 needs to be performed using bitwise operations only.除法和除以 10 只需要使用按位运算来执行。

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.但是我们可以使用 BCD 转换将整数转换为 BCD,然后使用移位运算符反转。 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.因此,在 BCD 转换后,我们可以以相反的顺序逐步移动每组 4 位(代表 0-9 之间的数字),以获得 BCD 格式的反转数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM