按位操作 [英] Bitwise Operations

查看:69
本文介绍了按位操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道8和4的按位AND将返回0或false而

按位AND 8和9将返回1或true但我不知道如何写

C#中的synax。我有一个从0到15的值,我需要

将它与15比较,以​​便查找它是否包含值1,2,4或

8.


为了更加图形化地表示,当与1000或

0010进行AND运算时,值1010将产生true或大于0的值。我相信我需要使用的

运算符是& ;.谁能告诉我这是怎么做的?

I know that the bitwise AND of 8 and 4 will return 0 or false and the
bitwise AND of 8 and 9 will return 1 or true but I don''t know how to write
the synax for it in C#. I have a value that ranges from 0 to 15 and I need
to compare it to 15 in order to find if it contains the values 1, 2, 4, or
8.

To represent it more graphically, the value 1010 when ANDed with 1000 or
0010 will produce either true or a value greater than 0. I believe the
operator I need to use is &. Can anyone tell me how this is done?

推荐答案

克里斯托弗,


if( (值& 0x01)== 0x01)

包含1();

if((value& 0x02)== 0x02)

包含2();





问候 - Octavio


" Christopher Weaver" <我们***** @ nospamverizon.net> escribióenel mensaje

news:%2 **************** @ tk2msftngp13.phx.gbl ...
Christopher,

if ((value & 0x01) == 0x01)
Contains1();
if ((value & 0x02) == 0x02)
Contains2();

etc.

Regards - Octavio

"Christopher Weaver" <we*****@nospamverizon.net> escribió en el mensaje
news:%2****************@tk2msftngp13.phx.gbl...
I知道8和4的按位AND将返回0或false并且8和9的按位AND将返回1或者为真,但我不知道如何写入它的synax C#。我有一个从0到15的值,我需要
将它与15比较,以​​便找出它是否包含值1,2,4或
8.
运算符是& ;.谁能告诉我这是怎么做的?

I know that the bitwise AND of 8 and 4 will return 0 or false and the
bitwise AND of 8 and 9 will return 1 or true but I don''t know how to write
the synax for it in C#. I have a value that ranges from 0 to 15 and I need
to compare it to 15 in order to find if it contains the values 1, 2, 4, or
8.

To represent it more graphically, the value 1010 when ANDed with 1000 or
0010 will produce either true or a value greater than 0. I believe the
operator I need to use is &. Can anyone tell me how this is done?



这适用于Status是包含各种选项的值,并且

STATUS_CANCELLED是一个表示其中一个的常量:


if((Status& STATUS_CANCELLED)> 0)

{

ckbCancelled.Checked = true;

}

else

{

ckbCancelled。 Checked = false;

}


还有其他想法吗?

" Christopher Weaver" <我们***** @ nospamverizon.net>在消息中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
This works where Status is the value containing the various options and
STATUS_CANCELLED is a constant that represents one of them:

if ((Status & STATUS_CANCELLED) >0)
{
ckbCancelled.Checked = true;
}
else
{
ckbCancelled.Checked = false;
}

Any other ideas?
"Christopher Weaver" <we*****@nospamverizon.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
我知道8和4的按位AND将返回0或false并且8和9的按位AND将返回1或true但我不知道如何在C#中为它编写synax# 。我有一个从0到15的值,我需要
将它与15比较,以​​便找出它是否包含值1,2,4或
8.
运算符是& ;.谁能告诉我这是怎么做到的?

I know that the bitwise AND of 8 and 4 will return 0 or false and the
bitwise AND of 8 and 9 will return 1 or true but I don''t know how to write
the synax for it in C#. I have a value that ranges from 0 to 15 and I need
to compare it to 15 in order to find if it contains the values 1, 2, 4, or
8.

To represent it more graphically, the value 1010 when ANDed with 1000 or
0010 will produce either true or a value greater than 0. I believe the
operator I need to use is &. Can anyone tell me how this is done?





实际上,8和9给出了8不是1.


AFAIK,因为CLR 0为FALSE且任何不为FALSE都是正确的(即

任何不为零)


ie

System.Convert.ToBoolean(8)= True


所以8和9是真的,但因为它不相等到0,不是因为它是
等于1.

回到主题:按位AND和OR运算符是&和|


8 AND 9 ==> 8& 9

进行任何位测试


((价值和位)==位)


例如


((15& 4)== 4)是真的,因此4开启

((9& 4)= = 4)是假的,因此4关闭

最简单的方法是设置一个查找表。


bool [] [] lookup = new bool [16] [] {new bool [4] {false,

false,false,false},

new bool [4] {false,

false,false,true},

...

new bool [4] {true,

true ,true,true}};

索引到表中,例如lookup [9]会给你一个bool [4]

表示哪些位被打开。

hth,


Alan 。


Actually, 8 AND 9 gives 8 not 1.

AFAIK, for the CLR 0 is FALSE and anything NOT FALSE is TRUE (ie.
anything not zero)

i.e.
System.Convert.ToBoolean(8) = True

So 8 AND 9 is true but becuase it is not equal to 0, not because it is
equal to 1.
Back on topic: the bitwise AND and OR operators are & and |

8 AND 9 ==> 8 & 9

for any bit test

(( value & bit) == bit)

e.g.

((15 & 4) == 4) is true, thus 4 is turned on
((9 & 4) == 4) is false, thus 4 is turned off
The simplest way to do this is to set up a lookup table.

bool[][] lookup = new bool[16][]{ new bool[4]{false,
false,false,false},
new bool[4]{false,
false,false,true},
...
new bool[4]{true,
true,true,true}};
Indexing into the table e.g. lookup[9] will give you a bool[4]
indicating which bits are turned on.
hth,

Alan.


这篇关于按位操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆