The Application of bitwise operations in actual projects ensures that you can learn something!

Source: Internet
Author: User

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"!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.