Marker enumeration and bitwise operations, marker enumeration operations
Notes
First, define the flag enumeration:
Create an IsEnums. cs class in the model folder of the project.
[Flags]
Public enum ABC
{
A = 1,
B = 2,
C = 4,
}
Then reference the model in the HomeController. cs class,
The advantage of using the flag enumeration is that it can be freely combined. In the flag enumeration, each of them is a n Number of 2. If it is defined as a combination of 1, 2, 3, it will conflict.
When we want to make some combinations for bitwise operations
The 8-bit binary value of 1 is 00000001
The 8-bit binary value of 2 is 00000010
The 8-bit binary value of 4 is 00000100.
/*
100000001
PM
4:00000100
8:100001000
2 + 4: 00000110
1 + 2 + 4: 00001110
* Bitwise AND 1 & 1 are 1, and the rest are 0.
Bitwise AND comparison
2 + 4: 00000110
4:00000100
**********
00000100
*
Bitwise AND comparison
1 + 2 + 4: 00001110
4:00000100
***********
00000100
* Bitwise OR 0 | 0 is 0, and the rest is 1
Bitwise OR comparison
2 + 4: 00000110
4:00000100
***********
00000110
*
Bitwise OR comparison
1 + 2 + 4: 00001110
4:00000100
***********
00001110
*/
For example, you can perform bitwise calculations in the Controller to match the corresponding data.
Public ActionResult Index ()
{
Var Type = (int) ABC. B;
Var data = _ db. [Table name]. Where (I => (I. [field name] & Type) = Type). ToList ();
Return View (data );
}