什么是位运算符? [英] What are bitwise operators?

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

问题描述

我是谁的人只是为了好玩写code和还没有真正钻研它要么是学术或专业设置,所以像这样的位运算符的东西真的逃脱我。

I'm someone who writes code just for fun and haven't really delved into it in either an academic or professional setting, so stuff like these bitwise operators really escapes me.

我在阅​​读关于JavaScript,这显然支持位运算的文章。我一直看到的地方提到这个操作,我已经试过阅读弄清楚它到底是什么,但我只是似乎没有得到它。因此,他们是什么?明显的例子将是巨大的! :D

I was reading an article about JavaScript, which apparently supports bitwise operations. I keep seeing this operation mentioned in places, and I've tried reading about to figure out what exactly it is, but I just don't seem to get it at all. So what are they? Clear examples would be great! :D

短短几年更多的问题 - 什么是位运算的一些实际应用?什么时候使用它们?

Just a few more questions - what are some practical applications of bitwise operations? When might you use them?

推荐答案

由于没有人开始讨论为什么这些都是有用的主题:

Since nobody has broached the subject of why these are useful:

我用旗时使用位运算了很多。例如,如果你要通过一系列标志来操作(例如,File.Open,具有读取模式和写入模式同时启用),你可以将它们作为单个值。这是由一个bitset(字节,短,int或long)分配每个可能的标志它自己的位来实现的。例如:

I use bitwise operations a lot when working with flags. For example, if you want to pass a series of flags to an operation (say, File.Open, with Read mode and Write mode both enabled), you could pass them as a single value. This is accomplished by assigning each possible flag it's own bit in a bitset (byte, short, int, or long). For example:

 Read: 00000001
Write: 00000010

所以,如果你想通过读,写,你会通过(READ | WRITE),然后结合了两成

So if you want to pass read AND write, you would pass (READ | WRITE) which then combines the two into

00000011

,然后可以在另一端等来解密

Which then can be decrypted on the other end like:

if ((flag & Read) != 0) { //...

它检查

00000011 &
00000001

返回

00000001

这是不为0,所以标记并指定读

which is not 0, so the flag does specify READ.

您可以使用XOR来切换不同的位。我已经使用标志指定方向输入(上,下,左,右)时使用此。例如,如果一个精灵的水平移动,我想它转身:

You can use XOR to toggle various bits. I've used this when using a flag to specify directional inputs (Up, Down, Left, Right). For example, if a sprite is moving horizontally, and I want it to turn around:

     Up: 00000001
   Down: 00000010
   Left: 00000100
  Right: 00001000
Current: 00000100

我简单地异或电流值(LEFT | RIGHT),这将左转断,对上,在这种情况下

I simply XOR the current value with (LEFT | RIGHT) which will turn LEFT off and RIGHT on, in this case.

位移位是有用的几起案件。

Bit Shifting is useful in several cases.

x << y

是相同的

X * 2

如果您需要快速乘以二的幂,但要小心移1位到最高位 - 这使得消极的,除非它是无符号数。不同大小的数据打交道时也非常有用。例如,从四个字节读取的整数:

if you need to quickly multiply by a power of two, but watch out for shifting a 1-bit into the top bit - this makes the number negative unless it's unsigned. It's also useful when dealing with different sizes of data. For example, reading an integer from four bytes:

int val = (A << 24) | (B << 16) | (C << 8) | D;

假设A是最显著字节和D最少。这将最终为:

Assuming that A is the most-significant byte and D the least. It would end up as:

A = 01000000
B = 00000101
C = 00101011
D = 11100011
val = 01000000 00000101 00101011 11100011

颜色通常存储这种方式(具有最显著字节忽略或用作阿尔法):

Colors are often stored this way (with the most significant byte either ignored or used as Alpha):

A = 255 = 11111111
R = 21 = 00010101
G = 255 = 11111111
B = 0 = 00000000
Color = 11111111 00010101 11111111 00000000

要再次找到的值,就在比特,直到它的底部移动到右边,则屏蔽掉剩余的高位比特

To find the values again, just shift the bits to the right until it's at the bottom, then mask off the remaining higher-order bits:

Int Alpha = Color >> 24
Int Red = Color >> 16 & 0xFF
Int Green = Color >> 8 & 0xFF
Int Blue = Color & 0xFF

0xFF的是相同的11​​111111所以基本上,对于红,你会做这样的:

0xFF is the same as 11111111. So essentially, for Red, you would be doing this:

Color >> 16 = (filled in 00000000 00000000)11111111 00010101  (removed 11111111 00000000)
00000000 00000000 11111111 00010101 &
00000000 00000000 00000000 11111111 =
00000000 00000000 00000000 00010101 (The original value)

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

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