[英]Bit Setting and Bit Shifting in Ansi C
Can anyone explain this bitwise operation syntax? 任何人都可以解释这种按位操作语法吗?
#define Bitset(var,bitno) ((var) |=1UL<<(bitno))
I know it sets the bits of var
, but I can't understand the syntax. 我知道它设置
var
的位,但我无法理解语法。
Let's break it down, piece by piece: 让我们一块一块地分解它:
1UL
is an unsigned long int
with a value of 1 represented at the bit level as: 1UL
是一个unsigned long int
,其值为1,表示为位:
00000000000000000000000000000001
the <<
is a "bit shift" operator which will move all the bits in that value above to the left bitno
number of times. <<
是一个“位移”运算符,它将该值中的所有位移到左侧bitno
的次数。 If it's 1UL<<5
, you'll end up with: 如果它是
1UL<<5
,你将最终得到:
00000000000000000000000000100000
Once you have this value, the |=
(which is a bitwise OR operation with an assignment) will essentially force the bit of var
that's in line with that 1
to be a 1
and wont touch any other bits because ( X | 0 = X
) 一旦你有了这个值,
|=
(这是一个带有赋值的按位OR运算 )将基本上强制与该1
一致的var
位为1
并且不会触及任何其他位,因为( X | 0 = X
)
Lets say var
is 37
and bitno
is 7
. 让我们说
var
是37
而bitno
是7
。 Then everything at the bit level will look like this: 然后位级别的所有内容将如下所示:
00000000000000000000000000100101 // var
00000000000000000000000010000000 // 1UL<<7
00000000000000000000000010100101 // var | (1UL<<7)
Finally, in case it isn't clear, the #define
marks Bitset
as a function-like macro . 最后,如果是不明确的,
#define
标记Bitset
为函数宏 。
This is a macro. 这是一个宏。 Whenever the preprocessor hits a statement like
Bitset(var,bitno)
it faithfully replaces it with 每当预处理器遇到类似
Bitset(var,bitno)
的语句时Bitset(var,bitno)
它就会忠实地替换它
var = var | 1UL << (bitno)
Further to explain this. 进一步解释这个。
UL here means Unsigned Long . UL在这里表示无符号长 。
Operator |
运营商
|
is used for bitwise OR
operation. 用于
bitwise OR
运算。 so the variable var
is OR
ed with 1UL << bitno
and assigned back to var
所以变量
var
与1UL << bitno
OR
1UL << bitno
并分配回var
Then during runtime or compile time depending on the nature of the program, 然后在运行时或编译时,取决于程序的性质,
Say var
is 01000110
and bitno
is 5
Say
var
是01000110
, bitno
是5
then 1UL << 5 = 32
or 00100000
然后
1UL << 5 = 32
或00100000
then 然后
var = 01000110 | 00100000
ie var = 01100110
即
var = 01100110
Say var=8, that is 0000 1000
in binary. 假设var = 8,即二进制的
0000 1000
。
If you do 如果你这样做
8 | 16
8 | 16
you will have 0000 1000 | 0001 0000
8 | 16
你将有0000 1000 | 0001 0000
0000 1000 | 0001 0000
which will give you 0001 1000
, because the |
0000 1000 | 0001 0000
这将给你0001 1000
,因为|
operator sets the bit if either bit is 1. 如果任一位为1,则运算符设置该位。
So you are applying the |
所以你正在应用
|
operator to your value and 1<<n
, that is to 0000 0001
shifted of n bits to the left. 运算符为您的值和
1<<n
,即0000 0001
向左移位n位。
For instance 1 << 3
is 0000 0001 << 2 = 0000 0100
. 例如,
1 << 3
是0000 0001 << 2 = 0000 0100
。
In essence: doing Bitset(8,3)
will generate a mask with only the third bit set by doing 1 << 3
, getting 0000 0100
. 实质上:做
Bitset(8,3)
将生成一个掩码,只有第三位设置为1 << 3
,得到0000 0100
。 It will then "or" this mask to 8, giving: 0000 1000 | 0000 0100
然后将“或”这个掩码设为8,给出:
0000 1000 | 0000 0100
0000 1000 | 0000 0100
, resulting in 0000 1100
, that is, you set the 3rd bit of 8. 0000 1000 | 0000 0100
,结果为0000 1100
,即设置第3位为8。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.