C 和 C++ 中的宏定义
我是一名 C/C++ 新手程序员,用 Java 编程已经有一段时间了。我目前正在理解一些C代码。在这里我看到一些宏定义,例如:
/* Flags for ds_flags */
#define DSF_OVER (1<<0)
#define DSF_DISPLAY (1<<1)
#define DSF_CALLFLOW (1<<2)
我无法理解为什么我们必须以这种方式定义这些宏。这些比定义如下的优势是什么:
#define DSF_OVER 0
#define DSF_DISPLAY 1
#define DSF_CALLFLOW 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当看到类似的东西时,
没有多少人能够很快看出只设置了一位,并且它是什么。
然而这一点非常清楚。
When seeing something like
not many people are able to quickly see that only one bit is set, and which it is.
however makes this very clear.
唯一的潜在优点是更容易看到代码使用一个不同的位集正确定义了每个常量。
如果你只写 1, 2, 4 而不是
1<<0
、1<<1
、1<,大多数人一眼就能看出这一点;<2
,所以在这个例子中可能很难看到这个优势。一旦到达1<<15
,有些人就会错过像 32748 这样的拼写错误。The only potential advantage is that it's easier to see that the code correctly defines each constant with one distinct bit set.
Most people will see that at a glance anyway if you just write 1, 2, 4 instead of
1<<0
,1<<1
,1<<2
, so perhaps it's difficult to see that advantage in this example. Once you get to1<<15
, some people would miss a typo like 32748.这些是位值,例如 1、2、4. 8。
与使用显式值相比,这是一种更方便、更可靠的定义它们的方式,尤其是当位索引变得更大时(例如
(1<<15) 比
32768
或0x8000
更容易理解、更直观,因为它显然意味着“位 15”而不是某个可能的任意数字)。These are bit values, e.g. 1, 2, 4. 8.
It's a more convenient and robust way of defining them than using explicit values, especially as the bit indices get larger (e.g.
(1<<15)
is much easier to understand and more intuitive than32768
or0x8000
, in that it obviously means "bit 15" rather than some possibly arbitrary number).在定义中使用
(1< 可以清楚地表明该值是单个位而不是数字。
对于编译器来说,这没有什么区别,因为
(1<<2)
是在编译时计算的,而不是在运行时计算的。清楚地显示它们是单位值对于任何阅读代码的人来说都是有用的,因为例如它们可以是可以组合的多个值,或者单个变量可以用于存储多个标志:有时还需要特定位处理硬件(例如,在某个 I/O 端口上,您可能需要指定第五位,因为这就是硬件的接线方式,并且
(1<<4)
比更具可读性>16
为 那)。Using
(1<<x)
in a define makes it clear that the value is a single bit and not a number.For the compiler it makes no difference because
(1<<2)
is computed at compile time and not at runtime. Showing clearly that they are single-bit values is instead useful for whoever reads the code because for example they could be multiple values that can be combined or that a single variable can be used to store multiple flags:Also requiring specific bits is sometimes needed when dealing with hardware (e.g. on a certain I/O port may be you need to specify the fifth bit because that's how the hardware is wired, and
(1<<4)
is more readable than16
for that).实际上是为了清楚起见 - 它表明不同的定义是位掩码,通常一起使用来过滤输入中的值。我不知道你是否已经知道什么是位掩码 - 这里有一个链接: http:// www.vipan.com/htdocs/bitwisehelp.html
It's for clarity really - it shows that the different definitions are bit masks that will typically be used together to filter values from an input. I don't know if you already know what a bit mask is - here's a link: http://www.vipan.com/htdocs/bitwisehelp.html
有时,位的位置代表一些位操作,就像您的情况一样:
如果您稍后要添加一个新项目,您将这样做,因为
您不能在此处使用值 5,因为它启用了两位(二进制为 101)。为了避免此类潜在错误,使用
>>
来使用宏始终是安全的。当下一位应为二进制 1000 时,它不会错误地生成值 5 (101)。生成无错误代码的关键在于编程的便利性。
Some times the position of the bits represent some bit operations like in your case:
If you were to add a new item later, you will do it as
You cant have a value 5 here since it is two bits enabled (101 in binary). To avoid such potential errors it is always safe to use macros using
>>
. It can't produce a value of 5 (101) by error in when the next bit should be 1000 in binary.It is all about programming convenience to produce error free code.
无论您是否喜欢
(1 << n)
,每个人都应该知道十六进制数字的含义。每当您在源代码中遇到十六进制时,它总是意味着正在发生一些位或字节操作。您不得将十六进制表示法用于任何其他目的。因此,将掩码、数字位操作文字等写为十六进制是明智的。Regardless of whether you prefer
(1 << n)
or not, everyone should be aware of what hexadecimal numbers signify. Whenever you encounter hex in source code it always means that som bit- or byte manipulations are taking place. You don't use hex notation for any other purpose. Therefore it is wise to write masks, numeric bit manipulation literals etc as hex.