Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Bitwise AND & and LOGICAL AND && [duplicate]

Possible Duplicate:
What is the difference between logical and conditional AND, OR in C#?

What is the difference between Bitwise AND & and Logical AND &&??

like image 842
GibboK Avatar asked Feb 19 '11 14:02

GibboK


2 Answers

& modifies integers with bitwise operations, ie. 1000 & 1001 = 1000, && compares boolean values. However, & doubles as the non-shortcircuiting logical and, meaning if you have false & true, the second parameter would still be evaluated. This won't be the case with &&.

like image 73
Femaref Avatar answered Sep 28 '22 08:09

Femaref


Bitwise, as its name implies, it's an AND operation at the BIT level.

So, if you perform a BITWISE AND on two integers:

int a = 7;     // b00000111
int b = 3;     // b00000011
int c = a & b; // b00000011 (bitwise and)

On the other hand, in C#, logical AND operates at logical (boolean) level. So you need boolean values as operators, and result is another logical value:

bool a = true;
bool b = false;
bool c = a && b; // c is false
c = a && true; // c is true

But only at the logical level.

like image 36
Pablo Santa Cruz Avatar answered Sep 28 '22 08:09

Pablo Santa Cruz