C语言 如何翻转C中字节中的特定位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19617248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:50:40  来源:igfitidea点击:

How to flip a specific bit in a byte in C?

cbit-manipulationbit

提问by Niels Robben

I'm trying to use masks and manipulating specific bits in a byte. For example:

我正在尝试使用掩码并操作字节中的特定位。例如:

I want to write a program in C that flips two bits at particular positions e.g. the bit at position 0 and the one at the third position. So, 11100011, would become 01110011.

我想用 C 编写一个程序,在特定位置翻转两位,例如位置 0 的位和第三个位置的位。所以,11100011,会变成01110011

How can I swap these bits?

我怎样才能交换这些位?

回答by dasblinkenlight

Flipping a bit is done by XOR-ing with a mask: set bits at the positions that you want to flip, and then execute a XOR, like this:

翻转位是通过带掩码的异或运算完成的:在要翻转的位置设置位,然后执行 XOR,如下所示:

int mask = 0x90; // 10010000
int num  = 0xE3; // 11100011
num ^= mask;     // 01110011

Here are a few notes:

这里有一些注意事项:

  1. bits are commonly counted from the least significant position, so your example flips bits in positions 4 and 7, not at positions 0 and 4
  2. To construct a bit mask for a single position, use expression 1 << n, where nis the position number counting from the least significant bit.
  3. To combine multiple bits in a single mask, use |operator. For example, (1 << 4) | (1 << 7)constructs the mask for flipping bits 4 and 7.
  1. 位通常从最低有效位置开始计数,因此您的示例翻转位置 4 和 7 中的位,而不是位置 0 和 4
  2. 要为单个位置构造位掩码,请使用表达式1 << n,其中n是从最低有效位开始计数的位置编号。
  3. 要在单个掩码中组合多个位,请使用|运算符。例如,(1 << 4) | (1 << 7)构造用于翻转第 4 位和第 7 位的掩码。

回答by Eutherpy

If your byte is x, and you want to switch the bits at the i-th and j-th position:

如果您的字节是 x,并且您想切换第 i 个和第 j 个位置的位:

x = x ^ ((1<<i) | (1<<j));

So, in your case, it would just be (1<<4) | (1<<7). :)

所以,在你的情况下,它只是 (1<<4) | (1<<7)。:)

回答by AssafR

First of all, good luck!

首先祝你好运!

One remark - it is more useful to count the bits from the right and not left, since there are various byte/word sizes (8-bit,16-bit,etc.) and that count preserves compatibility better. So in your case you are referring to bits #7 and #4 (zero-count).

一句话 - 从右侧而不是左侧计算位更有用,因为有各种字节/字大小(8 位、16 位等),并且该计数可以更好地保持兼容性。因此,在您的情况下,您指的是位 #7 和 #4(零计数)。

Did you mean 'flip' (change 0<->1 bits) or 'switch' them between one and the other?

您的意思是“翻转”(更改 0<->1 位)还是“切换”它们一个和另一个?

For the first option, the answer above (XOR with "int mask = 0x90; // 10010000") is very good. For the second one, it's a bit more tricky (but not much).

对于第一个选项,上面的答案(XOR with "int mask = 0x90; // 10010000")非常好。对于第二个,它有点棘手(但不是很多)。

回答by Jonathan Sacramento

To flip bits, you can use the exclusive OR bitwise operator. This takes two operands (typically, the value you want to operate on and the mask defining what bits will be flipped). The eXclusive OR (XOR) operator will only flip a bit if, and only if, one of the two is set to 1, but NOT both. See the (simple) example below:

要翻转位,您可以使用异或按位运算符。这需要两个操作数(通常,您要操作的值和定义将翻转哪些位的掩码)。eXclusive OR (XOR) 运算符仅当且仅当两者之一设置为 1 时才会翻转一点,但不是两者都设置为 1。请参阅下面的(简单)示例:

#include <stdio.h>

int main(int argc, char** argv)
{
   int num = 7;   //00000111
   int mask = 3;  //00000011

   int result = num ^ mask; //00000100
   printf("result = %d\n", result); //should be 4

   return 0;
}