Operators in C programming language [ Part-2 ]

Click Here for Part-1

Bitwise Operators : Bitwise operators work on bits and perform bit-by-bit operation. The truth table for &, |, and ^ is as follows :

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assume A = 60 and B = 13; in binary format, they will be as follows :

A = 0011 1100
B = 0000 1101

A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011

The following table lists the bitwise operators supported by C. Assume variable ‘A’ holds 60 and variable ‘B’ holds 13, then :

Operators Description Examples
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) = 12
| Binary OR Operator copies a bit if it exists in either operand. (A | B) = 61
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) = 49
~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) = -61
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 = 240
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 = 15

Example :
#include <stdio.h>
int main()
{
      unsigned int a=60; /* 60 = 0011 1100 */
      unsigned int b=13; /* 13 = 0000 1101 */
      int c=0;
      c= a & b;
      printf("Value of c is %d\n",c); /* 12 = 0000 1100 */
      c= a | b;
      printf("Value of c is %d\n",c); /* 61 = 0011 1101 */
      c= a ^ b;
      printf("Value of c is %d\n",c); /* 49 = 0011 0001 */
      c= ~a;
      printf("Value of c is %d\n",c); /*-61 = 1100 0011 */
      c= a << 2;
      printf("Value of c is %d\n",c); /* 240 = 1111 0000 */
      c= a >> 2;
      printf("Value of c is %d\n",c); /* 15 = 0000 1111 */
}

Output :
Value of c is 12
Value of c is 61
Value of c is 49
Value of c is -61
Value of c is 240
Value of c is 15

Assignment Operators : The following tables lists the assignment operators supported by the C language :

Operators Description Examples
= Simple assignment operator. Assigns values from right side operands to left side operand. C = A + B will assign the value of A + B to C
+= Add AND assignment operator. It adds the right operand to the left operand and assigns the result to the left operand. C += A is equivalent to C = C + A
-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. C -= A is equivalent to C = C - A
*= Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. C *= A is equivalent to C = C * A
/= Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. C /= A is equivalent to C = C / A
%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator. C <<= 2 is same as C= C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C= C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C=C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise exclusive OR and assignment operator. C |= 2 is same as C =C | 2

Example :
#include <stdio.h>
int main()
{
      int a=21, c;
      c=a;
      printf("Value if c is %d\n",c);
      c+=a;
      printf("Value if c is %d\n",c);
      c-=a;
      printf("Value if c is %d\n",c);
      c*=a;
      printf("Value if c is %d\n",c);
      c/=a;
      printf("Value if c is %d\n",c);
      c%=a;
      printf("Value if c is %d\n",c);
      c<<=a;
      printf("Value if c is %d\n",c);
      c>>=a;
      printf("Value if c is %d\n",c);
      c&=a;
      printf("Value if c is %d\n",c);
      c^=a;
      printf("Value if c is %d\n",c);
      c|=a;
      printf("Value if c is %d\n",c);
}

Output :
Value if c is 21
Value if c is 42
Value if c is 21
Value if c is 441
Value if c is 21
Value if c is 0
Value if c is 0
Value if c is 0
Value if c is 0
Value if c is 21
Value if c is 21

Misc Operators : The following tables lists the misc operators supported by the C language :

Operators Description Examples
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
& Returns the address of a variable. &a; returns the actual address of the variable.
* Pointer to a variable. *a;
*= Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. C *= A is equivalent to C = C * A
?: Conditional Expression. If Condition is true ? then value X : otherwise value Y

Example :
#include <stdio.h>
int main()
{
int a=5;
short b;
double c;
int *ptr;
sizeof(a);
printf("Size of variable a = %d\n", sizeof(a) );
ptr = &a;           /* 'ptr' now contains the address of 'a'*/
printf("value of a is %d\n", a);
printf("*ptr is %d.\n", *ptr);     /* example of ternary operator */
a = 10;
b = (a == 1) ? 20: 30;
printf( "Value of b is %d\n", b );
b = (a == 10) ? 20: 30;
printf( "Value of b is %d\n", b );
}

Output :
Size of variable a = 4
value of a is 5
*ptr is 5
Value of b is 30
Value of b is 20

0 Comments