交换字节中的位对
我有一个任意的8位二进制数字,例如11101101
我必须交换所有的位对,例如:
交换之前:11-10-11-01
交换后:11-01-11-10
我在面试中被问到了这个问题!
I have an arbitrary 8-bit binary number e.g., 11101101
I have to swap all the pair of bits like:
Before swapping: 11-10-11-01
After swapping: 11-01-11-10
I was asked this in an interview !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在伪代码中:
它的工作原理是分别处理每个位对的低位和高位,然后组合结果:
x & 0b10101010
从每对中提取高位,然后>> 1
将其移至低位位置。(x & 0b01010101) << 1
从每对中提取低位并将其移至高位位置。由于并非所有语言都允许您直接编写二进制文字,因此您可以将它们编写为十六进制:
In pseudo-code:
It works by handling the low bits and high bits of each bit-pair separately and then combining the result:
x & 0b10101010
extracts the high bit from each pair, and then>> 1
shifts it to the low bit position.(x & 0b01010101) << 1
extracts the low bit from each pair and shifts it to the high bit position.Since not all languages allow you to write binary literals directly, you could write them in for example hexadecimal:
10101010
和01010101
)。16 位示例(不是实际代码):
10101010
and01010101
).Example for 16 bits (not actual code):
正如其他人所说,最优雅和灵活的解决方案是,分别对偶数位和奇数位应用“梳状”掩码,然后将它们分别向左和向右移动一个位置,以使用按位或将它们组合起来。
您可能需要考虑的另一种解决方案利用了数据类型相对较小的大小。您可以创建一个包含 256 个值的查找表,该表静态初始化为您想要作为输入输出的值:
每个值都放置在数组中以表示索引的转换。因此,如果您执行以下操作:
out
will contains0x55
这比第一种方法更麻烦且不太灵活(如果您想从 8 位移动到 16 位怎么办?)但确实有一种方法,如果执行大量这些操作,速度会明显加快。
The most elegant and flexible solution is, as others have said, to apply an 'comb' mask to both the even and odd bits seperately and then, having shifted them left and right respectively one place to combine them using bitwise or.
One other solution you may want to think about takes advantage of the relatively small size of your datatype. You can create a look up table of 256 values which is statically initialised to the values you want as output to your input:
Each value is placed in the array to represent the transformation of the index. So if you then do this:
out
will contain0x55
This is more cumbersome and less flexible than the first approach (what if you want to move from 8 bits to 16?) but does have the approach that it will be measurably faster if performing a large number of these operations.
假设您的号码是
num
。首先找到偶数位置位:
num & oxAAAAAAAA
第二步找到奇数位置位:
num & ox55555555
第三步将奇数位更改为偶数位,偶数位更改为奇数位:
偶数 = (num & oxAAAAAAAA)>>1
奇数 = (num & 0x55555555)<<1
最后一步 ...
结果 = 偶数 |奇数
打印结果
Suppose your number is
num
.First find the even position bit:
num & oxAAAAAAAA
Second step find the odd position bit:
num & ox55555555
3rd step change position odd position to even position bit and even position bit to odd position bit:
Even = (num & oxAAAAAAAA)>>1
Odd = (num & 0x55555555)<<1
Last step ...
result = Even | Odd
Print result
我会首先对其进行“常规”编码 - 也就是说,在几个明显、明确的阶段中,并使用它来验证我所拥有的单元测试是否正常运行,然后仅在我有能力的情况下才转向更深奥的位操作解决方案对性能的需求(并且通过所述改进提供了额外的性能)
首先为人编写代码,其次为计算机。
I would first code it 'longhand' - that is to say in several obvious, explicit stages, and use that to validate that the unit tests I had in place were functioning correctly, and then only move to more esoteric bit manipulation solutions if I had a need for performance (and that extra performance was delivered by said improvments)
Code for people first, computers second.