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

问题描述

我在读有关标志枚举和位运算符,并发现了这code:

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 =你好和/或dropdown2 =你好......

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

感谢

推荐答案

第一个问题:

A | 执行按位或;一点将在结果被设置,如果它是在第一值或第二值设置。 (你对枚举用它来创建其他值的组合值),如果你使用按位和,这不会使多大意义。

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)

从本质上讲,你可以,如果某些位测试一个枚举设置;在这种情况下,我们正在测试,看是否对应于设置位。值读写既能通过了测试(两者有位调零); 不会(它没有零位设置)。

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)

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

07-16 16:04
查看更多