C Operators - Bitwise Operators and Conditional Operators

    0 Votes

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 int and 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)

Also Read - Top C Interview Questions and Answers for Freshers & Advanced C Interview Questions and Answers

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.

Also Read - Top Embedded C Interview Questions and Answers & C++ Interview Questions and Answers

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

#include <stdio.h>

int main(void) {
    int a = 7, b = 8;
    printf("Bitwise AND (&) : %d \n", a&b);
    printf("Bitwise OR (|)  : %d \n", a|b);
    printf("Bitwise XOR (^) : %d \n", a^b);
    printf("Left Shift (<<) : %d \n", a<<b);
    printf("Right Shift (>>): %d \n", a>>b);
    printf("Bitwise Not (~) : %d \n", ~a);
    return 0;
}

Output:

    Bitwise AND (&) : 0 
    Bitwise OR (|)  : 15 
    Bitwise XOR (^) : 15 
    Left Shift (<<) : 1792 
    Right Shift (>>): 0 
    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

#include <stdio.h>

int main(void) {
    int a = 7, b = 8;
    char result = (a &gt b) ? 'a' : 'b'; 
    printf("Greater is: %c", result);
    return 0;
}

Output:
    Greater is: b

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

C Overview and Basic Syntax
Data Types in C Language
C Qualifiers - Constant and Volatile type Qualifier
C Type Casting with examples - float to int / int to char / int to long
C Constants and Literals
C Operators - Arithmetic, Assignment, Relational and Logical
C Operator Precedence and Associativity Table with Examples
C Input and Output Statements
C Format Specifiers for printf and scanf
C Decision Making Control Structures - 'if', 'if - else', nested 'if', 'switch'
Loops in C - for, while and do...while Loops
C 'break', 'continue' and 'goto' Statements to Control Program Flow
C Preprocessor Directives and Macros
C Functions Tutorial with Examples
C Arrays - Dynamic, 2d and Multidimensional with Examples
C Strings - Internal Working and Functions with example
C Pointers basics explained with examples
Structures in C Programming with Example
Unions in c programming with example
C Storage Class Specifiers - auto, register, static and extern