I have the following items
Apple= 00000001 (1)
Mango= 00000010 (2)
Banana= 00000100 (4)
Grapes= 00001000 (8)
Now I am storing the fruits a user can have like this by doing or'd
UserA- 00000001(Apple) or'd 00000010(Mango) = 00000011 (ie 3)
Now I want to check whether that user contains Apple or Banana ? Till now I am thinking this : (UserA's-Values) & (Apple|Banana ) =(Apple|Banana )
ie (00000011)&(00000001|00000100 ) =(00000001|00000100 )
but it is not working ? May I know what I am missing ?
You're checking for Apple AND Banana. The expression should be (UserA's-Values) & (Apple|Banana) (!= 0)
Bits can be stored and manipulated in both Enum and Struct as well.
For Enum: http://msdn.microsoft.com/en-us/library/cc138362.aspx
For Struct: Bit fields in C#
Enums are nice as .ToString()
will print a coma-separated list of the elements, but you still use standard AND/OR operators to manipulate and test against it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.