Reputation: 3923
I am confused why we can use 'or'(|)
operator in statments like int i = 1 | 2 ; bool b = false | true;
and what does this syntax really do ? and when is this syntax useful , can someone please enlighten mean about that syntax ?
Upvotes: 0
Views: 169
Reputation: 5264
Bit OR
1 = 0000 0001
2 = 0000 0010
1|2 = 0000 0011
OR Table
A B A|B
0 1 1
0 0 0
1 0 1
1 1 1
Use Case
BitWise OR is used to set the perticular bit.
Example : Suppose we have to set bit 2 in 0000 0001
0000 0001 | 0000 0100 = 0000 0101
Upvotes: 1
Reputation: 6543
It is bitwise OR operator which does digital ORing between 2 values like 1 has binary value 0001 and 2 has binary value 0010, so if you write 1 | 2 then it will return 0011 value which is 3.
For bool, it does boolean OR, i.e. gives false only if both the operands are false.
It is defined for bool and integral types. It can also be overloaded for user defined types. Have a look here at msdn.
Upvotes: 3
Reputation: 23935
Since the integer question is answered already:
For bool values, like bool x = true | false
x is true
.
It is false, if both operants are false, and only BOTH. Everything else returns true
true | false => true
false | true => true
true | true => true
false | false => false
Upvotes: 1