C# enum判断是否包含某项及按位运算

来源:互联网 发布:phpdisk无法连接数据库 编辑:程序博客网 时间:2024/06/18 12:23

地址:http://www.cnblogs.com/yank/archive/2009/02/27/1399423.html

地址:http://hi.baidu.com/wllllll/blog/item/1a5f1e95a34209137bf480ca.html


“|”符号,实际上是把所有二进制数据进行合并,有一个或两个1都返回1,

比如“9|64”等于二进制数据“1001”,“1000000”,他们执行“|”操作后返回“1001001”,得“73”

Directions aDirections = Directions.Top | Directions.Bottom; 等于1|2,获得3。
aDirection | Directions.Top; 等于3|1,获得3。
aDirection | Directions.Bottom; 等于3|2,获得3。
aDirection | Directions.Left; 等于3|4,获得7。
aDirection | Directions.Left; 等于3|8,获得11。


using System;
using System.Windows.Forms;

public partial class Form1 : Form {
    public enum Directions {
        None = 0,
        Top = 1,
        Bottom = 2,
        Left = 4,
        Right = 8
    }

    public Form1() {
        InitializeComponent();

        Directions aDirection = Directions.Top | Directions.Bottom;

        if (aDirection == Directions.None) MessageBox.Show(
"None");
        if ((aDirection | Directions.Top) == aDirection) MessageBox.Show(
"Top");
        if ((aDirection | Directions.Bottom) == aDirection) MessageBox.Show(
"Bottom");
        if ((aDirection | Directions.Left) == aDirection) MessageBox.Show(
"Left");
        if ((aDirection | Directions.Right) == aDirection) MessageBox.Show(
"Right");
    }
}

http://apps.hi.baidu.com/share/detail/31803965


位操作符是对数据按二进制位进行运算的操作符。位操作是其他很多语言都支持的操作,如C、C++和Java等,C#也不例外支持位操作。注意位操作支持的数据类型是基本数据类型,如byte、short、char、int、long等,C#支持的位操作有如下几种:
· 按位与 &
· 按位或 |
· 按位取反 ~
· 左移 <<
· 右移 >>
· 异或^

在C#中位操作同C的位操作没有什么区别,位操作的速度相对较快,而且如果熟练的话,处理起来也相对方便,特别是在一些权限等相关的设置中,比如:用1、2、4、8、16、32、64分别代表查看、添加、编辑、修改、删除、审批等权限值的时候,如果某个用户的最终权限是多种权限值的叠加,用位操作来判断是否具有某种权限是相当方便的了。

using System;
/**//*
* 作者:周公
* 说明:本程序用以说明在C#中如何进行位操作。
* 日期:2007-09-17
* */
public class BitAction
{
public static void Main(string[] args)
{
int[] power = new int[] { 1, 2, 4, 8, 16, 32, 64 };
int value = 126;
/**//*
* 1的二进制形式: 00000001
* 2的二进制形式: 00000010
* 4的二进制形式: 00000100
* 8的二进制形式: 00001000
* 16的二进制形式: 00010000
* 32的二进制形式: 00100000
* 64的二进制形式: 01000000
* 126的二进制形式:01111110
*/
for (int i = 0; i < power.Length; i++)
{
if ((value & power[i]) != 0)
{
Console.WriteLine("有power[{0}]={1}所代表的权限", i, power[i]);
}
}
Console.WriteLine("按位与:126&4={0}", value & 4);
Console.WriteLine("按位或:126|4={0}", value | 4);
Console.WriteLine("左移:126<<4={0}", value << 4);
Console.WriteLine("右移:126>>4={0}", value & 4);
Console.WriteLine("异或:126^4={0}", value ^ 4);
Console.WriteLine("按位取反:~126={0}", ~value);
Console.ReadLine();
}
}

运算结果

//举例说明
using System;
class MikeCat
{
public static void Main()
{
int a=6&3;
Console.WriteLine("a={0}",a);
//6的二进制是00000110,3的二进制是00000011,按位与后等于00000010,  即2。
    
int b=6|3;
Console.WriteLine("b={0}",b);
//6的二进制是00000110,3的二进制是00000011,按位或后等于00000111,即7

int c=~6;
Console.WriteLine("c={0}",c);
//6的二进制是00000110,按位取反后是11111001即-7

int d=6^3;
Console.WriteLine("d={0}",d);
//6的二进制是00000110,3的二进制是00000011,按位异或后等于00000101,即5

int e=6<<3;
Console.WriteLine("e={0}",e);
//6的二进制是00000110,左移三位后等于00101000,即48

int f=6>>2;
Console.WriteLine("f={0}",f);
//6的二进制是00000110,右移二位等于00000001,即1
  }
}

转载自http://www.cnblogs.com/yiki/archive/2008/03/05/1091378.html



原创粉丝点击