7

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

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • 15
    Did you think to look this up *anywhere*? – Oliver Charlesworth Feb 19 '11 at 14:36
  • 5
    Nope Oli, there aren't any resources for that. Bitwise and logical operations only exist for a few days, you know? – Femaref Feb 19 '11 at 14:38
  • http://www.google.com/search?q=Bitwise+AND+%26+and+Logical+AND+%26%26&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a – gbvb Feb 19 '11 at 14:39
  • @Femaref, I can't stop laughing...Good sense of humor – Ken D Feb 19 '11 at 14:44
  • in some instances, sarcasm is the only valid solution. – Femaref Feb 19 '11 at 14:45
  • sorry guys i did not realize it was duplicate. I am a beginner in developing and I never study IT at school... why did you vote down my question? It could make sense for a beginners like me have some explanation about basic concept? – GibboK Feb 19 '11 at 15:46
  • because such information can be found on the net (or wikipedia for the matter) in a matter of seconds. – Femaref Feb 19 '11 at 15:57
  • Ok Femaref I understand I Will be more careful next time. Anyway thanks for your explanation. – GibboK Feb 19 '11 at 17:19

3 Answers3

22

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

Femaref
  • 60,705
  • 7
  • 138
  • 176
5

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.

Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
3

Well, Good question (duplicated though).

Bitwise AND will affect its operators on the bit-level i.e. looping and doing logical AND operation on every bit.

On the other hand,

Logical AND will take 2 boolean operators to check their rightness (as a whole) and decide upon (notice that bool in C# is 2 bytes long).

Ken D
  • 5,880
  • 2
  • 36
  • 58