Simple question but what does the | operator do as opposed to the || (or) operator?
Simple question but what does the | operator do as opposed to the ||
Share
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 bitwiseORoperator, where as||is a logicalORoperator. Namely, the former is used to “combine” the bits from two numeric values as a union, whereas the latter evaluates to true if either condition on the left or right side of the operator is true.Specifically, bitwise operators (not to be confused with logical operators) operate on each bit of the numbers (at the same ordinal position), and calculates a result accordingly. In the case of a bitwise
OR, the resulting bit is 1 if either bit is 1, and 0 only if both bits are 0. For example, 1|2 = 3, because:Furthermore, 2|3 = 3, because:
This can seem confusing at first, but eventually you get the hang of it. Bitwise
ORis used mostly in cases to set flags on a bit field. That is, a value holding the on/off state for a set of related conditions in a single value (usually a 32 bit number). In Win32, the window style value is a good example of a bit field, where each style is represented by a single bit (or flag), like WS_CAPTION, which indicates whether or not the window has a title bar.