I’m writing a perl implementation 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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
&is a bitwise AND. See Bitwise operators.So
flagsis 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 example0x400 = 0100 0000 0000is being used to check if the 11th bit is set (to 1) inflags.Typically, you wouldn’t use
0x400but 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.