C # Permissions binary operations

Source: Internet
Author: User

   /// <summary>    ///bit arithmetic, binary/// </summary>[Flags] Public enumPermissionenum {/// <summary>        ///Initialize///0000/// </summary>Init =0,        /// <summary>        ///Insert///0001/// </summary>Insert =1,        /// <summary>        ///Delete///0010/// </summary>Delete =2,        /// <summary>        ///Update///0100/// </summary>Update =4,        /// <summary>        ///Enquiry/// +/// </summary>Query =8    }
View Code
    classProgram {Static voidMain (string[] args) {            //Initialize a new permission            varDemo =insertpermission (Permissionenum.insert);            Console.WriteLine (demo); //Add one more query permissionDemo =insertpermission (Permissionenum.query, demo);            Console.WriteLine (demo); //Add one more Delete permissionDemo =insertpermission (Permissionenum.delete, demo);            Console.WriteLine (demo); //Remove a new permissionDemo =deletepermission (Permissionenum.insert, demo);            Console.WriteLine (demo); //determine if you have new permissionsConsole.WriteLine (Getpermission (Permissionenum.insert, demo)); //determine if you have query permissionsConsole.WriteLine (Getpermission (Permissionenum.query, demo));        Console.read (); }        /// <summary>        ///Increase Permissions/// </summary>        /// <param name= "Pertype" >What permissions need to be added (additional deletions)</param>        /// <param name= "Userper" >User's original permission</param>        /// <returns></returns>         Public Static intInsertpermission (Permissionenum Pertype,intUserper = (int) {permissionenum.init) {//or the operation is true or false;            returnUserper | (int) Pertype; }        /// <summary>        ///Delete Permissions/// </summary>        /// <param name= "Pertype" >What permissions need to be removed (additional deletions)</param>        /// <param name= "Userper" >User's original permission</param>        /// <returns></returns>         Public Static intDeletePermission (Permissionenum Pertype,intUserper = (int) {permissionenum.init) {//~ complement, ~000010 = 111101//& And, True True, false and true            returnUserper & ~ (int) Pertype; }        /// <summary>        ///Determine if you have permissions/// </summary>        /// <param name= "Pertype" >Determine whether a permission type is owned</param>        /// <param name= "Userper" >User's original permission</param>        /// <returns></returns>         Public Static BOOLGetpermission (Permissionenum Pertype,intUserper = (int) {permissionenum.init) {//& And, True True, false and true//The number that is judged is not equal to zero, which is to have this permission            return(Userper & (int) pertype)! =0; }
View Code

C # Permissions binary operations

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.