If you still do not understand bit operations, please view myArticle: Bit operations that we forget together in those years!
The following is a bit operation application I learned in this project! It mainly implements three notifications:
1. Stick
2. Set as Homepage
3. Set "TOP" + "as Homepage" at the same time"
Effect
To facilitate bitwise operations, we can directly perform the following enumeration definitions, which are defined to the power of 2 and should be their values very special:
Binary Value
1 1
2 10
4 100
8 1000
16 10000
32 100000
64 1000000
128 10000000
/// <Summary> /// Notification Type /// </Summary> Public Enum Noticetype { /// <Summary> /// Normal /// </Summary> [Description ( " Normal " )] Normal = 1 , /// <Summary> /// PIN to top /// </Summary> [Description ( " PIN to top " )] Top = 2 , /// <Summary> /// Set Homepage /// </Summary> [Description ( " Set Homepage " )] Home = 4 ,}
We can easily accept parameters for bitwise operations,CodeAs follows:
/// <Summary> /// Top and set as Homepage /// </Summary> /// <Param name = "ID"> </param> /// <Returns> </returns> [Role] Public Actionresult workshop_noticeset ( Int ID, Int Typeflag ){ Bool Res = False ; Workshop_notice entity = Workshopmanager. getworkshop_notice (I => I. workshopnoticeid = ID ); If (Entity! = Null ){ Switch (Noticetype) typeflag ){ Case Noticetype. Home: If (Entity. typeflag &( Int ) Noticetype. Home)> 0 ) Entity. typeflag = Entity. typeflag ^ ( Int ) Noticetype. Home; // unpin Else Entity. typeflag = Entity. typeflag | ( Int ) Noticetype. Home; // top Break ; Case Noticetype. Top: If (Entity. typeflag &(Int ) Noticetype. Top)> 0 ) Entity. typeflag = Entity. typeflag ^ ( Int ) Noticetype. Top; // unsets it as the homepage. Else Entity. typeflag = Entity. typeflag | ( Int ) Noticetype. Top; // set it to the homepage Break ;} If (Workshopmanager. editworkshop_notice (I => New Workshop_notice {typeflag = Entity. typeflag, workshopnoticeid = Entity. workshopnoticeid,}). iscomplete) res = True ;} Return Content (res. tostring ());}
Your support is my greatest motivation. If you think it is okay, please give it"Recommendation"!