15

I saw a couple of questions here about the diference between && and & operators in C#, but I am still confused how it is used, and what outcome results in different situations. For example I just glimpsed the following code in a project

bMyBoolean = Convert.ToBoolean(nMyInt & 1);
bMyBoolean = Convert.ToBoolean(nMyInt & 2);

When it will result 0 and when >0? What is the logic behind this operator? What are the diferences between the operator '|'?

bMyBoolean = Convert.ToBoolean(nMyInt | 1);
bMyBoolean = Convert.ToBoolean(nMyInt | 2);

Can we use the &&, || operators and get the same results (possibly with different code)?

Sunscreen
  • 3,452
  • 8
  • 34
  • 40

7 Answers7

11

The && is a conditional and used in if statements and while

if(x>1 && y<3)

this means that x should be greater than 1 and y less than 3, satisfy both conditions

if(x>1 || y<3)

satisfy one of them

However, & and | are bitwise AND and OR respectively. ex:

 1 | 0  => 1
 1 & 0  => 0
 1 & 1  => 1

if this apply for straight integers, their corresponding binary value will be calculated and applied

2&1
=>   10  // the binary value of 2
     &
     01  // the binary value of 1
     --
     00  // the result is zero
Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
  • 2
    I think this doesn't explain the difference well. What happens when you use `&` on booleans? What happens when you use `&&` on integers? – svick Sep 26 '12 at 11:00
  • If anyone wants to know why `10 & 01 == 00`, you may understand how bitwase operations are done [here](https://en.wikipedia.org/wiki/Bitwise_operation#AND). – Alisson Reinaldo Silva Jan 17 '18 at 13:36
8

The ampersand does bitwise AND on the integers in their binary representations. The pipe does bitwise OR.

See here what those bitwise operations mean: http://en.wikipedia.org/wiki/Bitwise_operation

Tomas Grosup
  • 6,396
  • 3
  • 30
  • 44
4

& and | is bit operations. You must use it on bit masks. && and || is logical operations so you can use it for only bool values.

Example of bit operation:

var a = 1;
var b = 2;
var c = a|b;

in binary format this means a = 00000001, b = 00000010 c = 00000011

So if you use bitmask c it will pass values 1, 2 or 3.

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
2

One more difference is that & operator computes the logical bitwise AND of its operands, if operands are not bool (integer in your case)

berliner
  • 1,887
  • 3
  • 15
  • 23
2

& operator is BItwise AND operator,it does manipulation on bits. e.g. 5 & 3

        0101    //5
        0011   //3
    ----------
5&3=    0001   //1

| operator is BItwise OR operator,it does manipulation on bits. 5|3

        0101    //5
        0011   //3
    ----------
  5|3=  0111   //7

&& operator is logical AND operator- it returns true if all conditions are true
e.g.

       if((3>5)&&(3>4))   //returns true
       if((6>5)&&(3>4))   //returns false

|| operator is logical OR operator- it returns true if one of the conditions is true
e.g.

   if((3>5)||(3>4))   //returns true
   if((6>5)||(3>4))   //returns true
   if((6>5)||(5>4))   //returns false
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
1

Other answers explains for you the different between && and &, so assume you understand this. In here, I just try to explain your specified case.

First case

bMyBoolean = Convert.ToBoolean(nMyInt & 1);

bMyBoolean false when nMyInt = 0 because:

  00 
& 01 
= 00;

Second case:

bMyBoolean = Convert.ToBoolean(nMyInt & 2);

bMyBoolean false when nMyInt = 0 or 1 because

  00 
& 10 
= 00;

Or:

  01 
& 10 
= 00;

The third and fourth cases with bitwise | are trivial because bMyBoolean always true with any nMyInt

bMyBoolean = Convert.ToBoolean(nMyInt | 1);
bMyBoolean = Convert.ToBoolean(nMyInt | 2);

You cannot apply && or || in this case because they are constraint only for bool, you will compiled errors.

cuongle
  • 74,024
  • 28
  • 151
  • 206
0

Here is something interesting for & . bit-wise as & be, it can be used to bool as in example below.

bool result = true;
result &= false;
Console.WriteLine("result = true & false => {0}", result );
//result = true & false => False

result = false;
result &= false;
Console.WriteLine("result = false & false => {0}", result );
//result = false & false => False


result = true;
result &= true;
Console.WriteLine("result = true & true => {0}", result );
//result = true & true => True
yantaq
  • 3,968
  • 2
  • 33
  • 34