Tuesday, February 2, 2016

C Operators - Bitwise Operators and Conditional Operators

As the name suggests, bitwise operator operate at bit level. There are six bitwise operators and they are all binary operators, except '~'. These operators can be applied on operands of type intand char. Bitwise operators do not work on float or double. Different bitwise operators are explained in the table given below.
SymbolOperator
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~One's compliment/ Bitwise NOT
<<Left Shift Operator
>>Right Shift Operator

Bitwise AND Operator

Bitwise "AND" operator will take bit by bit from two operands and generate the AND output of those two bits. For example, (4&5) generates output as 4. Let's see how that Bitwise AND Operator works. "AND" operation is applied on binary representation of 4 (0000 0100) and 5 (0000 0101) as given below to produce the result 4 (0000 0100).
  0000 0100 (4)
  0000 0101 (5)
-------------
  0000 0100 (4)

Bitwise OR Operator

Bitwise OR operator will take bit by bit from two operands and generate the OR output of those two bits. For example, (5|6) will generate output: 7. That is, "OR" operation is applied on corresponding bits of binary representation of 5 (0000 0101) and 6 (0000 0110) to produce the result 7 (0000 0111) as given below.


  0000 0101 (5)
  0000 0110 (6)
-------------
  0000 0111 (7)

Bitwise XOR Operator

Bitwise XOR operator will take bit by bit from two operands and generate the XOR output of those two bits. For example, (5|6) will generate output: 3. That is, XOR operation is applied on corresponding bits of binary representation of 5 (0000 0101) and 6 (0000 0110) to produce the result 3 (0000 0011) as given below.
  0000 0101 (5)
  0000 0110 (6)
-------------
  0000 0011 (3)
One's Compliment Operator/Bitwise NOT
Unlike other bitwise operators, One's complement (~) is a unary operator. One's complement operator will invert each bit of the operand (1 will be changed to 0 and Vice versa). For example, '~5' will produce output '-6' as given below
  0000 0101 (5)
-------------
  1111 1010 (This is -6 in 2's complement form)

Right Shit Operator

The right shift (>>) operator shifts each bit of its first operand to right by n times where n is the second operand. For example, If a is 75 (binary: 0100 1011) and we perform a right shift operation on 'a' like a >> 3, then the output will be 9.
a = 0100 1011 -> Initial state (75)
a = 0010 0101 -> After one bit right shift (37)
a = 0001 0010 -> After two bit right shift (18)
a = 0000 1001 -> After three bit right shift (9)
If you take a closer look at the result of each right shift operation, you can see that a right shift operation is equivalent to dividing the number by 2.
Left Shift Operator
The left shift (<<) operator shifts each bit of its first operand to left by n times where n is the second operand. For example, consider 'a' is 7, then expression a << 7 will produce output 56. Let's take a look how this expression is evaluated
a = 0000 0111 -> Initial state (7)
a = 0000 1110 -> After one bit left shift (14)
a = 0001 1100 -> After two bit left shift (28)
a = 0011 1000 -> After three bit left shift (56)
If you take a closer look at the result of each left shift operation, you can see that a left shift operation is equivalent to multiplying the number by 2.

Examples of Bitwise Operators

  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int a = 7, b = 8;
  5. printf("Bitwise AND (&) : %d \n", a&b);
  6. printf("Bitwise OR (|) : %d \n", a|b);
  7. printf("Bitwise XOR (^) : %d \n", a^b);
  8. printf("Left Shift (<<) : %d \n", a<<b);
  9. printf("Right Shift (>>): %d \n", a>>b);
  10. printf("Bitwise Not (~) : %d \n", ~a);
  11. return 0;
  12. }
  13.  
  14. Output:
  15.  
  16. Bitwise AND (&) : 0
  17. Bitwise OR (|) : 15
  18. Bitwise XOR (^) : 15
  19. Left Shift (<<) : 1792
  20. Right Shift (>>): 0
  21. Bitwise Not (~) : -8

Conditional Operator or Ternary Operator

As we discussed earlier, a ternary operator operates on three operands. Syntax of a conditional or ternary operator is boolean_expression? result_expression_1 : result_expression_2)
A ternary operator first evaluates the boolean_expression. If it evaluates to true, it will return the value of result_expression_1 as response. If the boolean_expression evaluates to false (0), value of result_expression_2 will be returned. For example, consider 'a' is 10
( a < 20 ) ? 100 : 1000
will return result as 100 because the expression a < 20 evaluates to true.

Example of Conditional Operator

  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int a = 7, b = 8;
  5. char result = (a &gt b) ? 'a' : 'b';
  6. printf("Greater is: %c", result);
  7. return 0;
  8. }
  9.  
  10. Output:
  11. Greater is: b

No comments: