Operators Part 2

Comparison operators

  1. Equals to Operator (a == b) - Returns true if a and b are equal.
  2. Not equals to Operator (a != b) - Returns true if a is not equal to b.
  3. Greater than Operator (a > b) - Returns true if a is greater than b
  4. Less than Operator (a < b) - Returns true if a is smaller than b
  5. Greater than equals to Operator (a >= b) - Returns true if a is greater than or equal to b
  6. Greater than Operator (a <= b) - Returns true if a is less than or equal to b

    #include <stdio.h>
    void main()
    {

        printf("Result of 5==5 = %d \n",5==5);        
        printf("Result of 6!=5 = %d \n",6!=5);        
        printf("Result of 6>3 = %d \n",6>3);         
        printf("Result of 4<1 = %d \n",4<1);         
        printf("Result of 9>=9 = %d \n",9>=9);        
        printf("Result of 3<=8 = %d \n",3<=8);        
    }            
        

Output

    Result of 5==5 = 1 
    Result of 6!=5 = 1 
    Result of 6>3 = 1 
    Result of 4<1 = 0 
    Result of 9>=9 = 1 
    Result of 3<=8 = 1 
    

Logical Operators

  1. AND Operator(&&) - Returns true if both the conditions are true.
  2. OR Operator(&&) - Return true if any one condition is true.
  3. AND Operator(&&) - Returns true if the condition is false.

    #include <stdio.h>
    void main()
    {
    int a=5,b=6,c=12;
    printf("Result of (a>b && c>a) = %d \n",a>b && c>a);        
    printf("Result of (c>b || b>a) =  %d \n",c>b || b>a);        
    printf("Result of (a>b) = %d \n",!(a>b));            
    }  
        

Output

    Result of (a>b && c>a) = 0 
    Result of (c>b || b>a) =  1 
    Result of (a>b) = 1 
    

Bitwise Operators

Bitwise operators are used to manipulate the numbers at the bit level. They are of two types -
1. Left Shift(<<) - Shifts a number to the left by 1 bit by adding 0 to LSB. MSB gets lost. Left shifting a number(n) r times multiplies n with the value 2 r.
2. Right Shift(>>) - Shifts a number to the right by 1 bit by adding 0 to MSB. LSB gets lost. Right shifting a number(n) r times divides n by the value 2 r.

    #include <stdio.h>
    void main()
    {
    int a = 4;
    printf("Result of a<<1 = %d \n",a<<1);       
    printf("Result of a<<2 = %d \n",a<<2);       
    printf("Result of a>>1 = %d \n",a>>1);       
    printf("Result of a>>1 = %d \n",a>>2);      
    }  
    

Output

    Result of a<<1 = 8
    Result of a<<2 = 16
    Result of a>>1 = 2
    Result of a>>2 = 1