开发者

What does '&' do in C/C++ and what is its equivalent operator in perl?

I'm writing a perl implementat开发者_C百科ion of a protocol whose specification is given for C/C++ but I don't know much C.

What does the condition if ((flags & 0x400) != 0) mean? How do I do that in perl?

Is if ($flags == "\x400") correct?


& is a bitwise AND. See Bitwise operators.

So flags is being treated as a series of bits, and then you AND it with something which has exactly the bit set that you want to check. If the result is non-zero, then the bit is set, otherwise the bit you were looking at isn't set. In particular, in your example 0x400 = 0100 0000 0000 is being used to check if the 11th bit is set (to 1) in flags.

Typically, you wouldn't use 0x400 but a named constant, so it is clear what that bit represents.

So if ($flags == "\x400") isn't correct. See Working with bits in Perl.

A common example of bit-masking can be seen in Linux file permissions.


It's the bitwise AND operator in C\C++ and Perl.

  • Perl
  • C\C++


The & operator performs a bitwise AND. In your case, if ((flags & 0x400) != 0) checks whether the eleventh bit of the flags variable is not set to 0. You can do bitwise operations in Perl, too.


& is the bitwise and operator, basically this is a test to see if a specific bit is set in the flags. See the perl equivalents here: http://perldoc.perl.org/perlop.html#Bitwise-And


It's the bitwise AND. In Perl, the operator is "&" as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜