定义了一种(1 <<;&小于0) [英] define SOMETHING (1 &lt;&lt; 0)

查看:124
本文介绍了定义了一种(1 <<;&小于0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来accros此行code的:

I came accros this line of code:

#define CPARSER_FLAGS_DEBUG        (1 << 0)

这是什么呢?其相同

What does it do? Its the same as:

#define CPARSER_FLAGS_DEBUG        (1)

右键?

推荐答案

在C-启发的语言,&LT;&LT; &GT;&GT ; 经营者左,右按位移运营商(虽然在C ++中,他们可以被重载 - 最有名的超载可能是 I / O流运营商)。例如,

In C-inspired languages, << and >> operators are left and right bitwise shift operators (though in C++ they can be overloaded — the most famous overload is probably I/O stream operators). For example,

x = y << 2;

分配x除以两比特移位y以左的结果。

assigns x the result of shifting y to the left by two bits.

通常你会看到很多低级别的code字节移位,这里是为什么...任何硬件提供了一种方法来配置和控制其行为,但没有人愿意使用整个整数重新present,比方说,ON / OFF状态。因此,硬件开发人员通常提供一个整数(又名寄存器,最常见的32位无符号)和状态,例如,位#0允许或禁止数据传输,位#1允许或禁止数据过滤,位#3做其他一些魔术和这么一等等力。通常,一个或多个设置可读取或同时改变。现在想象如何方便的是一个软件开发 - 而不是简单的整数(或布尔)时,程序员必须处理那些通常不是由CPU寻址位。为了简化自己的生活,开发者定义口罩。与上面的例子中坚持,配置口罩看起来是这样的:

Usually you will see a lot of byte shifting in low-level code and here is why... Any hardware provides a way to configure and control its behavior but nobody wants to use the whole integer to represent, say, ON/OFF state. Therefore, hardware developers generally provide a single integer (aka a register, most often unsigned 32-bit) and state that, for example, bit #0 enables or disables data transmission, bit #1 enables or disables data filtering, bit #3 does some other magic and so one and so force. Generally, one or more settings can be read or changed at the same time. Now imagine how convenient that is for software developers — instead of working with simple integers (or booleans), programmers have to deal with bits that are generally not addressable by the CPU. To simplify their lives, developers define masks. Sticking with the above example, the configuration masks could look like this:

#define MYDEV_ENABLE_DATA_FLOW (1u<<0)
#define MYDEV_ENABLE_FILTERING (1u<<1)
#define MYDEV_ENABLE_MAGIC     (1u<<2)

由于移位前pression的右手侧是已知的,编译器会生成用于分别各值以下的整数:

Since the right hand side of the shift expression is known, compiler will generate the following integers for each value respectively:


  • 1

  • 2

  • 4

起初可能没有很大的意义,但是如果你看看二进制重新presentation这些值,则是这样的:

Which at first might not make a lot of sense, but if you look at those values in binary representation, the look like this:


  • 项目0B001

  • 0b010

  • 0b100时

在换言之,每个值仅具有一个比特设置在不同的位置。然后,说,我们要实现数据传输和一个神奇的功能,但不启用我们想象的设备筛选。对于这一点,我们就必须有唯一的位#0和#2集( 1 ),和位#1没有( 0 )。这是当位或运营商,我们的pre自定义模板一起去方便。我们做的是这样的:

In other words, each value has only one bit set at different positions. Then, say we want to enable data transmission and a magic functionality but not enable filtering for our imaginary device. For that, we would have to have only bits #0 and #2 set (1), and bit #1 unset (0). This is when Bitwise OR operator along with our pre-defined masks come handy. What we do is this:

uint32_t value_for_device = MYDEV_ENABLE_DATA_FLOW | MYDEV_ENABLE_MAGIC;

这或S 项目0B001 0b100时,使 0b101时值。我们发送给设备,它会检查每一个位和启用或禁用相应的功能。

Which "OR"s 0b001 and 0b100, giving 0b101 value. We send this to a device, it checks every bit and enables or disables corresponding functionality.

其他位操作通常使用为好。比方说,我们不知道什么是目前启用或禁用了,我们不想改变什么,但只是确保数据过滤是关闭的。这可以通过阅读来完成当前的配置,未设置位,并写回。例如:

Other bits operations are often used as well. Say, we don't know what is currently enabled or disabled, and we don't want to change anything but just make sure that data filtering is off. This can be done by reading the current configuration, un-setting the bit, and writing it back. For example:

uint32_t value;
value = read_from_device();
value &= (~MYDEV_ENABLE_FILTERING);
write_to_device(value);

这是当然,不是唯一的用法。对于很多有用的示例,请参阅位操作黑客的肖恩·安德森玉龙

This is, of course, not the only usage. For a lot of useful examples, see "Bit Twiddling Hacks" by Sean Eron Anderson.

OK,又回到了原来的问题 - 为什么写(1 LT;℃下),而不是简单地(1)?有许多原因:

OK, getting back to your original question — why write (1<<0) and not simply (1)? There are a number of reasons:

这是比较一致的,以有这样的事情:

It is more consistent to have something like this:

#define A (1<<0)
#define B (1<<1)
#define C (1<<2)

而不是这样的:

#define A (1)
#define B (1<<1)
#define C (1<<2)

甚至这样的:

#define A 1
#define B 2
#define C 4

可维护性

易于改变周围的

的东西

这使得它更容易改变周围的事物。这是比较容易通过只改变移动宽度改变周围的事物,而不是继续添加/删除'&LT;意图

防爆pression

它明确规定了作者的那些谁读code的意图。刚 1 没有太大意义。但是当你看到 1&LT;&LT; 0 您最有可能的假设位数据移位参与和code可与位掩码

Maintainability

Easy to change things around

It makes it easier to change things around. It is easier to change things around by only changing the shift width and not keep adding/removing '<

Expression of intent

It clearly states the intent of the author to those who read the code. Just 1 does not mean much. But when you see 1<<0 you most likely assume that bit shifting is involved and the code works with bit-masks.

想象一下由应执行的移位数被定义为宏。例如:

Imagine that the number by which the shift should be performed is defined as a macro. For example:

#define MY_BIT (1u<<MAGIC_BIT_OFFSET)

那么,你真的不知道羯羊结果是 1 与否。你可以保持位偏移定义分开。

Then, you don't really know wether the result is 1 or not. And you can keep bit offset definitions separately.

有可能是一些更多的理由这样做不来我的脑海里来,立竿见影。

There is probably some more reasons to do this that don't come to my mind straight away.

希望它澄清了事情的的。祝你好运!

Hope it clears up things a bit. Good Luck!

这篇关于定义了一种(1 <<;&小于0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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