unary operators in c

 Relation Operators 

 A relation operator is used for comparing two values it returns 1 if the relation is true and 0 if relation is false.For eg if you want to check that you are adult or not then we can se use comparison operator to  check whether the entered age is  greater than 17 or not 'age>17'.

#include <stdio.h>

int main() {
    int age=12;
    if (age>17)
    {
        printf("You are adult");
    }
    else
    {
        printf("You are minor");
    }
    return 0;
}

Output:

You are minor



Logical Operators
 

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false.It is mostly used in decision making. This type of operators are used when we want to check multiple condition simultaneously.

                                     

1]&&:logical AND operators are used when we want to check multiple condition simultaneously.all the condition will return boolean value(1 0r 0),if all the condition return true(1) then it will become true otherwise false.Basically in background multiplication is  performed.

  • 1 X 0 = 0  False
  • 0 X1 = 0  False
  • 0 X 0 = 0 False
  • 1 X 1= 1 True

To remember the concept of logical AND we have to simply multiply all boolean values returned from conditions and after multiplication of all boolean if the result is 1 then we can say that the final result is true.

Let's understand with an example.

#include <stdio.h>

int main() {
int age=20;

int height=182;

if(age>18 && height>180)
{
    printf("You can join NCC ");
}
else
{
    printf("Oops,You are not eligible for NCC");
}
 
return 0;  
}

Output

You can join NCC 

In above eg,We have used logical AND(&&)operator in which if both of  the condition is satisfied then only it will become true.

2] | |:logical OR operators are used when we want to check multiple condition simultaneously.all the condition will return boolean value(1 0r 0),if any of the condition  return true(1) then it will become true otherwise false.Basically in background addition is  performed.

  • 1 + 0 = 1 True
  • 0 +1 = 0  True
  • 0 + 0 = 0 False
  • 1 + 1= 1 True

To remember the concept of logical OR we have to simply ADD all boolean values returned from conditions and after addition of all boolean if the result is 1 then we can say that the final result is true.

Let's understand with an example. 

#include <stdio.h>

int main() {
int no_of_family_member=5;


float family_annual_income=200000;

if(no_of_family_member<4 || family_annual_income<300000)
{
    printf("You can Apply for Scholarship ");
}
else
{
    printf("Sorry You Can't Apply for Scholarship");
}
 
 return 0;   
}

 

  Output 

You can Apply for Scholarship 

 

In above eg,We have used logical OR(||)operator in which if anyone of  the condition is satisfied then it will become true.

3]!:logical NOT operator is used to invert(opposite) the result.for eg if the result is true then it will become false if we have used logical NOT.

  • !1=0
  • !0=1

#include <stdio.h>
int main() {
int r=!1;
printf("%d",r);
return 0;
}

Output 

0

 

Bitwise Operator 

During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations i.e bit by bit.

         


#include <stdio.h>

int main() {
int num1=5,num2=7;
int bitwise_and,bitwise_or,bitwise_xor;
int compliment,rightshift,leftshift;
bitwise_and=num1&num2;
bitwise_or=num1|num2;
bitwise_xor=num1^num2;
compliment=~num2;
rightshift=num1>>2;
leftshift=num2<<1;
printf("bitwise AND of %d and %d is %d\n",num1,num2,bitwise_and);
printf("bitwise OR of %d and %d is %d\n",num1,num2,bitwise_or);
printf("bitwise XOR of %d and %d is %d\n",num1,num2,bitwise_xor);
printf("bitwise Compliment of %d is %d\n",num1,compliment);
printf("right shift  of %d is %d\n",num1,rightshift);
printf("left  shift of  %d is %d\n",num2,leftshift);
    
return 0;
}

Output 

bitwise AND of 5 and 7 is 5
bitwise OR of 5 and 7 is 7
bitwise XOR of 5 and 7 is 2
bitwise Compliment of 5 is -8
right shift  of 5 is 1
left  shift of  7 is 14


 

Bitwise AND(&):This operator is used to perform bit by bit ANDING between two numbers.

Lets's say num1=5,num2=7.

Binary of 5= 0 1 0 1

Binary of  7=0 1 1 1 (And)

Result:          0 1 0 1=5(in decimal)

As we have already learned about ANDING operation in Relation Operator i.e In ANDING operation multiplication is done bit by bit.                  

Bitwise OR(|):This operator is used to perform bit by bit ORING between two numbers.

Lets's say  num1=5,num2=7.

Binary of 5= 0 1 0 1

Binary of  7=0 1 1 1 (OR)

Result:          0 1 1 1=7(in decimal)

As we have already learn about ORING operation in Relation Operator i.e In ORING operation addition is done bit by bit,but here 1+1=1 not 2 since it is binary no. 

Bitwise XOR(|):This operator is similar to OR operator.The only difference between them is here 1+1=0 while in OR 1+1=1.

1 +0 =1

0 +1 =0 

0 +0= 0

1 +1= 0

Lets's say  num1=5,num2=7.

Binary of 5= 0 1 0 1

Binary of  7=0 1 1 1 (OR)

Result:          0 0 1 0=2(in decimal)

Bitwise Compliment or NOT(~):This operator is invert bit i.e if bit is 1 after compliment it will become 0 and if  bit is 0 then it will become 1.Complimented value refers to negative value.

Let's Say num=7 

Binary of 7=0 1 1 1

   Result :  - (1 0 0 0)=-8(in decimal)

Right Shift Operator:This operator is used to shift specified no.of bit from the right side of the number.

Let's Say num=4,num_of_bits_to_be_shifted=2;

exp=num>> num_of_bits_to_be_shifted;

Binary of 4=0 1 0 0 

1 Step     0 0 1 0 -->Removed one bit from the right side and added 0 at the beginning.

2 Step     0 0 0 1 -->Removed one bit from the right side and added 0 at the beginning.

Result :    0 0 0  1

In above eg we have shifted only two bit because the value of num_of_bits_to_be_shifted was 2.

Left Shift Operator:This operator is used to shift specified no.of bit from the left side of the number.

Let's Say num=7,num_of_bits_to_be_shifted=1;

exp=num<< num_of_bits_to_be_shifted;

Binary of 7=0 1 1 1

1 Step      1 1 1 0  -->Removed one bit from the left side and added 0 at the end.

Result : 1 1 1 0 

In above eg we have shifted onlyone  bit because the value of num_of_bits_to_be_shifted was 1.

 sizeof operator

The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).

#include <stdio.h>
int main()
{
    int a;
    float b;
    double c;
    char d;
    printf("Size of int=%lu bytes\n",sizeof(a));
    printf("Size of float=%lu bytes\n",sizeof(b));
    printf("Size of double=%lu bytes\n",sizeof(c));
    printf("Size of char=%lu byte\n",sizeof(d));

    return 0;
}

Output 

Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte

 

If you any have doubt,suggestion regarding this post or website then feel free to share with us.
 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎   

 

  Previous Next

2 Comments

Previous Post Next Post