C中的XOR运算符 [英] XOR operator in C

查看:125
本文介绍了C中的XOR运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行按位操作时,我很难确定何时使用XOR运算符.按位与和或非常简单.当您要屏蔽位时,请使用按位与(常见的用例是IP寻址和子网掩码).当您想打开位时,请使用包含"或.".但是,XOR总是能吸引我,我觉得如果在面试中被问到需要使用XOR的问题,我将永远无法得到它.有人可以说明何时使用它以及一些常见的用例.

I am having some trouble identifying when to use the XOR operator when doing bitwise manipulations. Bitwise And and Or are pretty straight forward. When you want to mask bits, use a bitwise AND (common use case is IP Addressing and subnet masks). When you want to turn bits on use the inclusive or. However, the XOR always gets me and I feel like if asked a question in an interview that requires using XOR I will never get it. Can someone shed some light on when to use it and some common use cases.

推荐答案

您使用互斥或​​翻转位-开启的位被关闭,反之亦然.例如,这对于交换两个数字而没有第三个数字的空间很方便.

You use exclusive or to flip bits - the ones that are ON are turned OFF, and vice versa. This can be handy for swapping two numbers without having space for a third number, for example.

0x0A ^ 0xFF = 0x03 ( 00001010 ^ 11111111 = 11110101 )

交换号码:

operation   example 
             A      B
initial:   0011    1010
a = a^b;   1001    1010
b = a^b;   1001    0011
a = a^b;   1010    0011

如您所见,数字(在这种情况下为半字节)A和B在不占用额外空间的情况下进行了交换.这适用于相同类型的任何两个数字(尽管在C中,按位运算符期望无符号整数)

As you can see, the numbers (nibbles in this case) A and B were swapped without using additional space. This works for any two numbers of the same type (although in C, bitwise operators expect unsigned integers)

XOR操作也用于弱加密".您可以将字符串与(重复的)代码字"进行XOR,结果将是没有意义的字节字符串.再次应用相同的操作,将出现原稿.这是一个很弱的算法,在现实生活中绝对不应该使用.

XOR operations are also used for "weak encryption". You can take the XOR of a string with a (repeated) "code word", and the result will be a string of bytes that make no sense. Apply the same operation again, and the original appears. This is quite a weak algorithm, and should never be used in real life.

关于切换位-在往日"中,如果要反转图像,则可以执行 pix = pix&每个像素为1 -或者如果您一次可以做到一个字节,则 byte = byte&0xFF .这会将白色背景上的黑色文本转换为黑色背景上的白色文本.我认为ATARI拥有通过对位图进行XOR来创建屏幕上任何地方"闪烁光标的专利.

Regarding toggling bits - in the "olden days", if you wanted to invert an image, you would do pix = pix & 1 for each pixel - or if you could do it a byte at a time, byte = byte & 0xFF. This would turn black text on a white background into white text on a black background. I think ATARI had a patent for creating a blinking cursor "anywhere on the screen" by doing XOR with the bit map.

类似地,如果您有一个微控制器想要创建一个闪烁的指示灯,那么反复执行 state = state XOR 1 会导致该状态切换.

Similarly if you have a microcontroller that wants to create a blinking light, repeatedly doing state = state XOR 1 will cause the state to toggle.

最后,有许多依赖XOR操作的乱序黑客".例如,可以使用以下方法来计算单词的奇偶性(即,设置的位数是奇数还是偶数)(来源:

Finallly, there are many "bit twiddling hacks" that rely on the XOR operation. For example, computing the parity of a word (i.e. whether the number of set bits is odd or even) can be done with the following (source: http://graphics.stanford.edu/~seander/bithacks.html)

unsigned int v;  // word value to compute the parity of
v ^= v >> 16;
v ^= v >> 8;
v ^= v >> 4;
v &= 0xf;
return (0x6996 >> v) & 1;

还有许多其他的聪明把戏"-当您试图找到最快的方法来进行涉及位操作的方法时,它们通常会出现.大多数人都可以过上完美的生活,而无需真正得到"它,这没关系.我,我喜欢摆弄东西.

There are many other "clever tricks" - they usually show up when you are trying to find the fastest way to do something that involves bit manipulation. Most people can go through life perfectly fine without ever really "getting" it, and that's OK. Me, I like bit fiddling.

这篇关于C中的XOR运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆