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.
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.
Let’s break it down, piece by piece:
1ULis anunsigned long intwith a value of 1 represented at the bit level as:the
<<is a “bit shift” operator which will move all the bits in that value above to the leftbitnonumber of times. If it’s1UL<<5, you’ll end up with:Once you have this value, the
|=(which is a bitwise OR operation with an assignment) will essentially force the bit ofvarthat’s in line with that1to be a1and wont touch any other bits because (X | 0 = X)Lets say
varis37andbitnois7. Then everything at the bit level will look like this:Finally, in case it isn’t clear, the
#definemarksBitsetas a function-like macro.