Bitwise Operators in C/C++

Filed Under: C Programming

In any programming language, operators carry out manipulations and operations on the operands, variables, etc. In C language or C++ language, Bitwise operators work at the bit level and serve the purpose of manipulations and operations on the Bits.

Process:

  • Bitwise operators perform operations on integers at a bit level.
  • The input (integer) gets converted to binary form after which the operations on the binary value are performed.
  • After the manipulations, the binary value obtained gets converted back to the integer format and gets displayed as a result.

Types of Bitwise Operators in C and C++

OperatorFunction
Bitwise AND Operator (&)The operator returns 1, if both the operand’s bits are 1.
Bitwise OR Operator (|)The operator returns 1, if either of the operand’s bits are 1.
Bitwise XOR Operator (^)The operator returns 1, if both the operand’s bits are opposite to each other, else it returns 0.
Bitwise 1’s Complement Operator (~)This operator inverts all the bits of the operand.
Bitwise Left Shift Operator (<<)This operator shifts the bits to left by an offset provided by the user.
Bitwise Right Shift Operator (>>)This operator shifts the bits to right by an offset provided by the user.

1. Bitwise AND Operator

The operator results 1 as output if both the bits are 1, else it results to 0.

Syntax:

operand1 & operand2

Example:

5 (Decimal) = 00000101 (Binary)

4 (Decimal) = 00000100

5 & 4 = 00000101 & 00000100 = 00000100 = 4

Result: 5 & 4 = 4

#include <stdio.h>
int main()
{
    int num1 = 5, num2 = 4;
    int result = num1&num2;
    printf("num1 & num2 = %d", result);
    return 0;
}

Output:

num1 & num2 = 4

2. Bitwise OR Operator

The operator results 1 as output if any of the bits are 1, else it results to 0.

Syntax:

operand1 | operand2

Example:

5 (Decimal) = 00000101 (Binary)

4 (Decimal) = 00000100 (Binary)

5 | 4 = 00000101 | 00000100 = 00000101 = 5

Result: 5 | 4 = 00000101 = 5

#include <stdio.h>
int main()
{
    int num1 = 5, num2 = 4;
    int result = num1|num2;
    printf("num1 | num2 = %d", result);
    return 0;
}

Output:

num1 | num2 = 5

3. Bitwise XOR Operator

The operator results 1 as output if both the bits are different/opposite to each other, else it results to 0.

Syntax:

operand1 ^ operand2

Example:

5 (Decimal) = 00000101 (Binary)

4 (Decimal) = 00000100 (Binary)

5 ^ 4 = 00000101 ^ 00000100 = 00000001 = 1

Result: 5 ^ 4 = 00000001 = 1

#include <stdio.h>
int main()
{
    int num1 = 5, num2 = 4;
    int result = num1^num2;
    printf("num1 ^ num2 = %d", result);
    return 0;
}

Output:

num1 ^ num2 = 1

4. Bitwise Left Shift Operator

This operator shifts the bits of the input to the left by the provided offset value and fill the void spaces as 0 in the result.

It is equivalent to multiplying a number by pow(2, offset) i.e. multiplying by 2 raised to the power of offset.

Syntax:

operand << offset

Example:

5 (Decimal) = 00000101 (Binary)

5 << 2 = 20

#include <stdio.h>
int main()
{
    int num = 5;
    int result = num << 2;
    printf("num << 2 = %d", result);
    return 0;
}

Output:

num << 2 = 20

5. Bitwise Right Shift Operator

This operator shifts the bits of the input to the right by the provided offset value and fills the void spaces as 0 in the result.

It is equivalent to dividing a number by pow(2, offset) i.e. dividing by 2 raised to the power of offset.

Syntax:

operand >> offset

Example:

5 (Decimal) = 00000101 (Binary)

5 >> 1 = 2

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

Output:

num >> 1 = 2

6. Bitwise 1’s Complement Operator

This operator performs 1’s complement on the input. It inverts the bits of the input.

Bitwise complement of input integer (x) will be – (x+1).

Syntax:

~(operand)

Example:

input = 5 (Decimal) = 00000101 (Binary)

~ input = ~ (00000101) = – (000000101 + 1) = – ( 00000110) = – 6

#include <stdio.h>
int main()
{
    int num = 5;
    int result = ~(num);
    printf("~num = %d", result);
    return 0;
}

Output:

~num = -6

Conclusion

Thus, in this article, we have understood the basic functionality served by the Bitwise Operators of C/C++.


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages