C语言 C 编程 - XOR 按位运算

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16941330/
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 06:35:42  来源:igfitidea点击:

C Programming - XOR Bitwise Operation

cbit-manipulationxor

提问by D3FTY

What operation does the following ‘C' statement perform?

以下“C”语句执行什么操作?

star = star ^ 0b00100100;

星 = 星 ^ 0b00100100;

(A)Toggles bits 2 and 5 of the variable star.

(A)切换变星的第 2 位和第 5 位。

(B)Clears all bits except bits 2 and 5 of the variable star.

(B)清除变星的第 2 位和第 5 位以外的所有位。

(C)Sets all bits except bits 2 and 5 of the variable star.

(C)设置除变星的第 2 位和第 5 位之外的所有位。

(D)Multiplies value in the variable star with 0b00100100.

(D)将变星中的值与 0b00100100 相乘。

I'm still clueless about this. Can someone help me out?

我对这个还是一窍不通。有人可以帮我吗?

回答by ivan_pozdeev

XOR operator (also called "logical addition") is defined like this:

XOR 运算符(也称为“逻辑加法”)定义如下:

a   b   a^b
-----------
0   0    0
0   1    1
1   0    1
1   1    0

So a^0leaves aintact while a^1toggles it.

所以a^0a不变,同时a^1切换它。

For multiple-bit values, the operation is performed bitwise, i.e. between corresponding bits of the operands.

对于多位值,操作按位执行,即在操作数的相应位之间执行。

回答by Mike

If you know how XOR works, and you know that ^is XOR in C, then this should be pretty simple. You should know that XOR will flip bits where 1 is set, bits 2 and 5 of 0b00100100 are set, therefore it will flip those bits.

如果您知道 XOR 的工作原理,并且您知道它^是 C 中的 XOR,那么这应该非常简单。您应该知道 XOR 将翻转设置为 1 的位,设置 0b00100100 的位 2 和 5,因此它将翻转这些位。

From an "during the test" standpoint, let's say you need to prove this to yourself, you really don't need to know the initial value of starto answer the question, If you know how ^works then just throw anything in there:

从“在测试期间”的角度来看,假设您需要向自己证明这一点,您实际上不需要知道 的初始值star来回答问题,如果您知道如何^工作,那么只需将任何东西扔进去:

 00100100
^10101010  (star's made up value)
---------
 10001110  (star's new value)

 bit position: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0  
               |---|---|---|---|---|---|---|---
 star's new v: | 1 | 0 | 0 | 0 | 1 | 1 | 1 | 0
               |---|---|---|---|---|---|---|---
 star's old v: | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0

Then check your answers again, did it:

然后再次检查你的答案,做到了:

(A) Toggles bits 2 and 5 of the variable star. (Yes)

(A) 切换变星的第 2 位和第 5 位。(是的)

(B) Clears all bits except bits 2 and 5 of the variable star. (Nope)

(B) 清除变星的第 2 位和第 5 位以外的所有位。(不)

(C) Sets all bits except bits 2 and 5 of the variable star. (Nope)

(C) 设置除变星的第 2 位和第 5 位之外的所有位。(不)

(D) Multiplies value in the variable star with 0b00100100. (36x170 = 142? Nope)

(D) 将变星中的值与 0b00100100 相乘。(36x170 = 142?不)

回答by Mifeet

It is (A) toggles bits 2 and 5.

它是 (A) 切换位 2 和 5

The following is the truth table for the XOR operation:

以下是异或运算的真值表:

x  y  x^y
0  0   0
1  0   1
0  1   1
1  1   0

You can see from the table that x XOR 0 = xand x XOR 1 = !x.

从表中可以看出x XOR 0 = xx XOR 1 = !x

XOR is a bitwise operation, so it operates on individual bits. Therefore if you XOR starwith some constant, it will toggle the 1bits in the constant.

XOR 是按位运算,因此它对单个位进行运算。因此,如果您star与某个常量进行异或,它将切换1常量中的位。

You can find some explanation e.g. here.

你可以找到一些解释,例如在这里

回答by John

The exclusive ORhas this truth table:

exclusive OR有这个道理表:

A   B   A^B
-----------
1   1   0
1   0   1
0   1   1
0   0   0

We can see that if Bis true(1) then Ais flipped (toggled), and if it's false(0) Ais left alone. So the answer is (A).

我们可以看到,如果Btrue( 1) 则A翻转(切换),如果是false( 0)A则保持原样。所以答案是(A)。

回答by Abhishek Ruhela

XOR operator returns 0 if both inputs are same otherwise returns 1 if both inputs are different.For Example the Given Truth Table :-

如果两个输入相同,则 XOR 运算符返回 0,否则如果两个输入不同,则返回 1。例如,给定的真值表:-

  • a=1 b=1 => a^b=0,
  • a=0 b=0 => a^b=0,
  • a=0 b=1 => a^b=1,
  • a=1 b=0 => a^b=1.
  • a=1 b=1 => a^b=0,
  • a=0 b=0 => a^b=0,
  • a=0 b=1 => a^b=1,
  • a=1 b=0 => a^b=1。

回答by himanshu

well xor is binary operator that work on bits of 2 nos.

好吧,xor 是二元运算符,可以处理 2 个 nos 的位。

rule of xoring:for same bit ans is 0 and for different bit ans is 1 let

异或规则:对于相同的位,ans 为 0,对于不同的位 ans 为 1 let

a= 1 0 1 0 1 1
b= 0 1 1 0 1 0
--------------
c= 1 1 0 0 0 1
--------------

compare bit of a and b bit by bit if same put 0 else put 1 xor is basically used to find the unique in given set of duplicate no. just xor all nos. and u will get the unique one(if only single unique is present)

如果相同,则逐位比较 a 和 b 的位,否则放置 1 xor 基本上用于在给定的重复编号集中查找唯一值。只是异或所有没有。你会得到唯一的(如果只有一个唯一的存在)