交换字节中的位对

发布于 2024-09-24 10:26:04 字数 176 浏览 3 评论 0原文

我有一个任意的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一个人的夜不怕黑 2024-10-01 10:26:04

在伪代码中:

x = ((x & 0b10101010) >> 1) | ((x & 0b01010101) << 1)

它的工作原理是分别处理每个位对的低位和高位,然后组合结果:

  • 表达式 x & 0b10101010从每对中提取高位,然后>> 1 将其移至低位位置。
  • 类似地,表达式 (x & 0b01010101) << 1 从每对中提取低位并将其移至高位位置。
  • 然后使用按位或将这两部分组合起来。

由于并非所有语言都允许您直接编写二进制文字,因此您可以将它们编写为十六进制:

Binary        Hexadecimal  Decimal 
0b10101010    0xaa         170
0b01010101    0x55         85

In pseudo-code:

x = ((x & 0b10101010) >> 1) | ((x & 0b01010101) << 1)

It works by handling the low bits and high bits of each bit-pair separately and then combining the result:

  • The expression x & 0b10101010 extracts the high bit from each pair, and then >> 1 shifts it to the low bit position.
  • Similarly the expression (x & 0b01010101) << 1 extracts the low bit from each pair and shifts it to the high bit position.
  • The two parts are then combined using bitwise-OR.

Since not all languages allow you to write binary literals directly, you could write them in for example hexadecimal:

Binary        Hexadecimal  Decimal 
0b10101010    0xaa         170
0b01010101    0x55         85
著墨染雨君画夕 2024-10-01 10:26:04
  1. 创建两个位掩码,一个包含所有偶数位,另一个包含奇数位(1010101001010101)。
  2. 使用按位与将输入过滤为两个数字,一个将所有偶数位清零,另一个将所有奇数位清零。
  3. 将仅包含偶数位的数字向左移动一位,将另一位向右移动一位
  4. 使用按位或将它们重新组合在一起。

16 位示例(不是实际代码):

short swap_bit_pair(short i) {
    return ((i & 0101010110101010b) >> 1) | ((i & 0x0101010101010101b) << 1));
}
  1. Make two bit masks, one containing all the even bits and one containing the uneven bits (10101010 and 01010101).
  2. Use bitwise-and to filter the input into two numbers, one having all the even bits zeroed, the other having all the uneven bits zeroed.
  3. Shift the number that contains only even bits one bit to the left, and the other one one bit to the right
  4. Use bitwise-or to combine them back together.

Example for 16 bits (not actual code):

short swap_bit_pair(short i) {
    return ((i & 0101010110101010b) >> 1) | ((i & 0x0101010101010101b) << 1));
}
烂柯人 2024-10-01 10:26:04
b = (a & 170 >> 1) | (a & 85 << 1)
b = (a & 170 >> 1) | (a & 85 << 1)
王权女流氓 2024-10-01 10:26:04

正如其他人所说,最优雅和灵活的解决方案是,分别对偶数位和奇数位应用“梳状”掩码,然后将它们分别向左和向右移动一个位置,以使用按位或将它们组合起来。

您可能需要考虑的另一种解决方案利用了数据类型相对较小的大小。您可以创建一个包含 256 个值的查找表,该表静态初始化为您想要作为输入输出的值:

const unsigned char lookup[] = { 0x02, 0x01, 0x03, 0x08, 0x0A, 0x09, 0x0B ...

每个值都放置在数组中以表示索引的转换。因此,如果您执行以下操作:

unsigned char out = lookup[ 0xAA ];

out will contains 0x55

这比第一种方法更麻烦且不太灵活(如果您想从 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:

const unsigned char lookup[] = { 0x02, 0x01, 0x03, 0x08, 0x0A, 0x09, 0x0B ...

Each value is placed in the array to represent the transformation of the index. So if you then do this:

unsigned char out = lookup[ 0xAA ];

out will contain 0x55

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.

疾风者 2024-10-01 10:26:04

假设您的号码是 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

一身骄傲 2024-10-01 10:26:04

我会首先对其进行“常规”编码 - 也就是说,在几个明显、明确的阶段中,并使用它来验证我所拥有的单元测试是否正常运行,然后仅在我有能力的情况下才转向更深奥的位操作解决方案对性能的需求(并且通过所述改进提供了额外的性能)

首先为人编写代码,其次为计算机。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文