What is bitwise or | operator?

I read about flag enumerations and bitwise operators and came across this code:

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

I read somewhere about why there is an inclusive or statement and how it cannot be &, but cannot find an article. Can someone update my memory and explain the arguments?

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

thank

+1
source share
4 answers

First question:

A | ; , . ( enums , ). , .

:

[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. Read ReadWrite ( 0); Write ( ).

// 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

:

, "/", ", ". , || ( ). " , ", ^ ( ).

(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)
+13
+2

. " -", OR, AND NOT b.

+1

Well, there are two different questions here, but to answer # 2, the logical OR found in most programming languages ​​is what you mean by and / or, I think.

if (dropdown == "1" || dropdown == "2") // Works if either condition is true.

Exclusive-OR, however, means "one or the other, but not both."

0
source

All Articles