I was reading about flag enums and bitwise operators, and came across this code:
enum file{ read = 1, write = 2, readandwrite = read | write }
I read somewhere about why there is a inclusive or statement and how there can’t be an &, but can’t find the article. Can someone please refresh my memory and explain the reasoning?
Also, how can I say and/or? Eg. if dropdown1=’hello’ and/or dropdown2=’hello’….
Thanks
First Question:
A
|does a bitwise or; a bit will be set in the result if it is set in the first value or the second value. (You use it onenumsto create values that are combinations of other values) If you were to use a bitwise and, it wouldn’t make very much sense.It gets used as follows:
Essentially, you can test if certain bits in an
enumare set; in this case we are testing to see if the bits corresponding to aReadare set. ValuesReadandReadWritewould both pass this test (the both have bit zero set);Writewould not (it does not have bit zero set).Second Question:
I think when you say ‘and/or’, you mean ‘one, the other or both’. This is exactly what the
||(or operator) does. To say ‘one or the other, but not both’, you’d use^( the exclusive or opertor).Truth tables (true==1, false==0):