簡體   English   中英

如何更改下一位的位置?(僅使用位操作)

[英]How to change place of the next bits?(use only bit operations)

例如 10'00'11'01'01 -> 01'00'11'10'10

void main() {
  unsigned int num = 78;
  unsigned int num2 = change_bit(num);
  printf("%d\n", num2); //141
}

我需要這樣的功能。

從我看來,您似乎需要一個函數來交換數字中每 2 位的位置。 幾個例子:

  1. 10'00'11'01'01 -> 01'00'11'10'10
  2. 11'01'10'10'11 -> 11'10'01'01'11

對於這個操作,一個非常簡單的函數如下:

unsigned int change_bit(unsigned int num)
{
    return ((num & 0xAAAAAAAA) >> 1) | ((num & 0x55555555) << 1);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM