什么是按位或 |运营商做什么? [英] What does the bitwise or | operator do?

查看:18
本文介绍了什么是按位或 |运营商做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关标志枚举和按位运算符的内容,并遇到了以下代码:

I was reading about flag enums and bitwise operators, and came across this code:

enum file{
read = 1,
write = 2,
readandwrite = read | write
}

我在某处阅读了有关为什么有包含性或声明以及如何不能有 & 的内容,但找不到该文章.有人可以刷新我的记忆并解释原因吗?

I read somewhere about why there is a inclusive or statement and how there can't be an &, but can't find the article. Can someone please refresh my memory and explain the reasoning?

另外,我怎么说和/或?例如.如果 dropdown1="hello" 和/或 dropdown2="hello"....

Also, how can I say and/or? Eg. if dropdown1="hello" and/or dropdown2="hello"....

谢谢

推荐答案

第一个问题:

A | 做按位或;如果设置在第一个值或第二个值中,则结果中将设置一个位.(您可以在 enums 上使用它来创建由其他值组合而成的值)如果您要使用按位和,这将没有多大意义.

A | does a bitwise or; a bit will be set in the result if it is set in the first value or the second value. (You use it on enums to create values that are combinations of other values) If you were to use a bitwise and, it wouldn't make very much sense.

它的用法如下:

[Flags] 
enum FileAccess{
None = 0,                    // 00000000 Nothing is set
Read = 1,                    // 00000001 The read bit (bit 0) is set
Write = 2,                   // 00000010 The write bit (bit 1) is set
Execute = 4,                 // 00000100 The exec bit (bit 2) is set
// ...
ReadWrite = Read | Write     // 00000011 Both read and write (bits 0 and 1) are set
// badValue  = Read & Write  // 00000000 Nothing is set, doesn't make sense
ReadExecute = Read | Execute // 00000101 Both read and exec (bits 0 and 2) are set
}
// Note that the non-combined values are powers of two, 
// meaning each sets only a single bit

// ...

// Test to see if access includes Read privileges:
if((access & FileAccess.Read) == FileAccess.Read)

本质上,您可以测试是否设置了 enum 中的某些位;在这种情况下,我们正在测试是否设置了对应于 Read 的位.值 ReadReadWrite 都会通过这个测试(两者都设置了位零);Write 不会(它没有设置零位).

Essentially, you can test if certain bits in an enum are set; in this case we are testing to see if the bits corresponding to a Read are set. Values Read and ReadWrite would both pass this test (the both have bit zero set); Write would not (it does not have bit zero set).

// if access is FileAccess.Read
        access & FileAccess.Read == FileAccess.Read
//    00000001 &        00000001 => 00000001

// if access is FileAccess.ReadWrite
        access & FileAccess.Read == FileAccess.Read
//    00000011 &        00000001 => 00000001

// uf access is FileAccess.Write
        access & FileAccess.Read != FileAccess.Read
//    00000010 &        00000001 => 00000000

第二个问题:

我认为当您说和/或"时,您的意思是一个、另一个或两个".这正是 ||(或运算符)所做的.要说一个或另一个,但不是两个",您可以使用 ^(异或运算符).

I think when you say "and/or", you mean "one, the other or both". This is exactly what the || (or operator) does. To say "one or the other, but not both", you'd use ^ ( the exclusive or opertor).

真值表(真==1,假==0):

Truth tables (true==1, false==0):

     A   B | A || B 
     ------|-------
OR   0   0 |    0
     0   1 |    1 
     1   0 |    1
     1   1 |    1 (result is true if any are true)

     A   B | A ^ B 
     ------|-------
XOR  0   0 |    0
     0   1 |    1 
     1   0 |    1
     1   1 |    0  (if both are true, result is false)

这篇关于什么是按位或 |运营商做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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