Normally when comparing flag enums I use the following format:
(value & flag) == flag;
But sometimes I come across this:
(value & flag) != 0;
Just wondering which is the better to use, or does it come down to personal preference?
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.
So long as
flagis a one-bit flag, they are equivalent. Ifflaghas multiple bits,is a logical AND (ALL bits must match) while
is a logical OR (ANY of the bits must match).