Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

c# - What does the bitwise or | operator do?

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?

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

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First Question:

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.

It gets used as follows:

[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

Second Question:

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).

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...