简体   繁体   English

以下关于 C 编程中的按位运算是什么意思?

[英]What does the following mean with respect to, bitwise operations in C programming?

A book on C programming states,一本关于 C 编程状态的书,

enum corvid { magpie , raven , jay , chough , corvid_num , };
# define FLOCK_MAGPIE 1 U
# define FLOCK_RAVEN 2 U
# define FLOCK_JAY 4 U
# define FLOCK_CHOUGH 8 U
# define FLOCK_EMPTY 0 U
# define FLOCK_FULL 15 U
int main ( void ) {
unsigned flock = FLOCK_EMPTY ;
if ( something ) flock |= FLOCK_JAY ;
...
if ( flock & FLOCK_CHOUGH )
do_something_chough_specific ( flock ) ;

Here the constants for each type of corvid are a power of two, and so they have exactly one bit set in their binary representation.在这里,每种类型的 corvid 的常数都是 2 的幂,因此它们的二进制表示中只有一个位。 Membership in a flock can then be handled through the operators: |= adds a corvid to flock, and & with one of the constants tests whether a particular corvid is present然后可以通过运算符处理群中的成员资格: |= 向群中添加一个 corvid,并且 & 使用其中一个常量测试是否存在特定的 corvid

Question 1. What is the code doing?问题 1. 代码在做什么? What's the purpose of declaring enum corvid?声明 enum corvid 的目的是什么?

Question 2. What does "Here the constants for each type of corvid are a power of two, and so they have exactly one bit set in their binary representation."问题 2. 什么是“这里每种类型的 corvid 的常数都是 2 的幂,因此它们的二进制表示中只有一个位。” mean?意思是?

It becomes more obvious further down in the same text.在同一个文本中,它变得更加明显。 The defines from your example are "hard-coded" (absolute values) constants used for bit masking.您示例中的定义是用于位掩码的“硬编码”(绝对值)常量。 something & 1U gives the first bit in a chunk of data, & 2U gives the 2nd bit, & 4U gives the 3rd bit and so on. something & 1U给出数据块中的第一位, & 2U给出第二位, & 4U给出第三位,依此类推。

The book writes them with decimal notation for some reason, but it is custom to write bitmasks with hex notation, because that's what they really are and it's the form easiest to understand.出于某种原因,这本书用十进制表示法编写它们,但习惯用十六进制表示法编写位掩码,因为那是它们的真正含义,而且它是最容易理解的形式。 something & 0xFU masks out the 4 lowest bits, for example.例如, something & 0xFU屏蔽掉 4 个最低位。

Now further down in the same chapter, the book swaps the hard coded constants for computed ones based on the enum.现在在同一章的更下方,这本书将硬编码常量交换为基于枚举的计算常量。 #define FLOCK_MAGPIE (1U << magpie ) and so on. #define FLOCK_MAGPIE (1U << magpie )等等。 The advantage of this is that the enum can be modified and then the bit masks will get updated accordingly.这样做的好处是可以修改枚举,然后相应地更新位掩码。 Defines using shift like this is perhaps the most common form to define bit masks, with 1U << n giving bit number n.像这样使用移位定义可能是定义位掩码的最常见形式, 1U << n给出位编号 n。 Note that the first bit in any binary is called bit 0, so 1U << 0 gives the first bit.请注意,任何二进制中的第一位称为位 0,因此1U << 0给出第一位。

Also note that all these expressions are integer constant expressions , meaning they are calculated at compile-time and gets replaced by a constant in your executable, so that your program doesn't need to calculate them in run-time.另请注意,所有这些表达式都是integer 常量表达式,这意味着它们是在编译时计算的,并被可执行文件中的常量替换,因此您的程序不需要在运行时计算它们。

For question 2:对于问题 2:

Since each number in the enum is a power of 2, there is only one bit set in its binary representation.由于枚举中的每个数字都是 2 的幂,因此在其二进制表示中只设置了一个位。 For example 2 = 00000010 and 16 = 00010000 .例如2 = 0000001016 = 00010000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM