& And && difference

1) & && and can be used as the logical AND operator, and represents a logical (and).

When the result of the expression operators on both sides are true, the result was the entire operation is true, otherwise, as long as one of them is false, the result is false.

           // A is true, b enters one of the two is false false output            
            BOOL A = to true ;
             BOOL B = to false ;
             IF (A & B) 
            { 
                Console.WriteLine ( " both are true enter " ); 
            } 
            the else 
            { 
                Console.WriteLine ( " both enter one false " ); 
                Console.Read (); 
            } 
         // A is false, b is true, one false entering both output            
           BOOL A = to false ;
             BOOL B = true;
             IF (A & B) 
            { 
                Console.WriteLine ( " both are true enter " ); 
            } 
            the else 
            { 
                Console.WriteLine ( " both enter one false " ); 
                Console.Read (); 
            } 
          // A is false, b is false, false one entering both output            
           BOOL A = to false ;
             BOOL B = to false ;
             IF (A & B) 
            { 
                Console.WriteLine ( " both are true enter "); 
            } 
            The else 
            { 
                Console.WriteLine ( " both enter one false " ); 
                Console.Read (); 
            }

  


 

           // A is true, b is true, both are true output enters 
           BOOL A = to true ;
             BOOL B = to true ;
             IF (A & B) 
            { 
                Console.WriteLine ( " both are true enter " ); 
                Console. the Read (); 
            } 
            the else 
            { 
                Console.WriteLine ( " both enter one false " ); 
                Console.Read (); 
            }

   

2) && also has a function of short-circuit, i.e. if the first expression is false, the second expression no longer calculated, for example, the following example

          // & no short circuit, x is 1, do not meet x = 2, but continues 
           int X = 1 ;
             int Y = 0 ;
             IF (X == 2 & Y ++> 0 ) 
            { 
              
            }            
            Console.WriteLine ( " Y Initial value is 0, no short-circuit the value: " + Y); 
            Console.Read ();


          // && shorted, x is not 2, directly out, is not performed ++ Y 
           int X = . 1 ;
             int Y = 0 ;
             IF (X == 2 && ++ Y> 0 ) 
            { 
              
            }            
            Console.WriteLine ( " Y initial value 0, after the short circuit is: " + Y); 
            Console.Read ();

3) & bitwise operators may also be used, when both sides of the expression is not a boolean operators &, & represents a bitwise AND operation, we usually use the lowest 0x0f to 4 with a & integer arithmetic, to obtain the integer a bit position, e.g., as a result of 0x31 & 0x0f 0x01.

Guess you like

Origin www.cnblogs.com/TechSingularity/p/12105952.html