简体   繁体   English

'&'在C / C ++中做了什么以及它在perl中的等效运算符是什么?

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

I'm writing a perl implementation of a protocol whose specification is given for C/C++ but I don't know much C. 我正在编写一个perl实现的协议,其规范是针对C / C ++的,但我不太了解C.

What does the condition if ((flags & 0x400) != 0) mean? if ((flags & 0x400) != 0)是什么意思? How do I do that in perl? 我怎么在perl中这样做?

Is if ($flags == "\\x400") correct? if ($flags == "\\x400")是否正确?

& is a bitwise AND. &是一个按位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. 因此, flags被视为一系列位,然后您将其与具有您要检查的位集的东西进行对比。 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 . 特别是,在您的示例中, 0x400 = 0100 0000 0000用于检查flags第11位是否设置(为1)。

Typically, you wouldn't use 0x400 but a named constant, so it is clear what that bit represents. 通常,您不会使用0x400而是使用命名常量,因此很清楚该位代表什么。

So if ($flags == "\\x400") isn't correct. 所以if ($flags == "\\x400")不正确。 See Working with bits in Perl . 请参阅使用Perl中的位

A common example of bit-masking can be seen in Linux file permissions . Linux文件权限中可以看到位掩码的常见示例。

It's the bitwise AND operator in C\\C++ and Perl. 它是C \\ C ++和Perl中的按位AND运算符。

The & operator performs a bitwise AND. &运算符执行按位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 . 在你的情况下, if ((flags & 0x400) != 0)检查flags变量的第11位是否设置为0. 你也可以在Perl中进行按位运算

& is the bitwise and operator, basically this is a test to see if a specific bit is set in the flags . &按位和运算符,基本上这是一个测试,看看是否在flags设置了一个特定的位。 See the perl equivalents here: http://perldoc.perl.org/perlop.html#Bitwise-And 请参阅此处的perl等效项: http//perldoc.perl.org/perlop.html#Bitwise-And

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

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