0
Operations
what ist the difference between && and & or between| and || ?
3 Answers
+ 4
Logical operators:
OR ||
takes two conditionals, return true if EITHER is true
AND &&
takes two conditionals, return true if BOTH are true
Bitwise operators:
work on the bit level, compare each bit that build a value and act accordingly
i will use small values which can be represented with 4 bits only.
we will find x on each example
OR | example:
5 | 3 = x
5 = 0101
3 = 0011
x = 0111 = 7 (bits in x are set by the bits of 5,3)
AND & example:
5 & 3 = x
5 = 0101
3 = 0011
x = 0001 = 1
+ 3
"|"Â and "&" work on more than just booleans, while "&&" and "||" work only on booleans.
While "&&" and "||" evaluates both sides of the operation, the others don't.
In a && operation for example, if the left side value is false, then it'll short circuit and cease the evaluation. But in a & evaluation, always evaluate both sides, making possible side-effects.
Examples:
int a = 0;
if(a == 9 && ++a == 9){}
Console.Write(a)
//Output: 0
------------------
int a = 0;
if(a == 9 & ++a == 9){}
Console.Write(a)
//Output: 1
I'am sorry if my english is not good enough to explain better.
0
thank you for ur answer :)