Bitwise Operators In C/C++

Bitwise Operators In C/C++

Bitwise operators in C/C++ are tools for working with individual bits in data. They might not be as famous as other operators, but they’re essential for tasks like making code more efficient or controlling hardware. In this guide, we’ll explain the basics of bitwise operators, show you how they’re used in practical situations, and teach you how to use them to solve coding problems. Whether you’re a beginner or an experienced programmer, this guide will help you understand how to use these operators effectively in C/C++.

What is Bitwise Operators

In C, there are six operators known as bitwise operators, or bit operators because they operate at the bit-level. These operators are used to carry out operations on individual bits in C programming.

In C and C++, there are six bitwise operators that work at the bit level:

  1. & (Bitwise AND): This operator takes two numbers as operands and performs the AND operation on each bit. The result is 1 only if both corresponding bits are 1.
  2. | (Bitwise OR): It also takes two numbers and performs the OR operation on each bit. The result is 1 if either of the two corresponding bits is 1.
  3. ^ (Bitwise XOR): This operator, taking two numbers, performs the XOR operation on each bit. The result is 1 if the two corresponding bits are different (one is 0 and the other is 1).
  4. << (Left Shift): It takes two numbers: the first operand has its bits left-shifted by the number of positions specified by the second operand.
  5. >> (Right Shift): Similarly, it takes two numbers, right-shifting the bits of the first operand by the number of positions indicated by the second operand.
  6. ~ (Bitwise NOT): This operator takes a single number and inverts (flips) all its bits. All 0s become 1s, and vice versa.

These bitwise operators are powerful tools for working at the binary level, which can be especially useful in tasks involving low-level programming, optimization, and manipulating individual bits within data.

Example of Bitwise Operators in C

// C Program to demonstrate use of bitwise operators
#include <stdio.h>
int main()
{
    // a = 5(00000101), b = 9(00001001)
    unsigned char a = 5, b = 9;
 
    // The result is 00000001
    printf("a = %d, b = %d\n", a, b);
    printf("a&b = %d\n", a & b);
 
    // The result is 00001101
    printf("a|b = %d\n", a | b);
 
    // The result is 00001100
    printf("a^b = %d\n", a ^ b);
 
    // The result is 11111010
    printf("~a = %d\n", a = ~a);
 
    // The result is 00010010
    printf("b<<1 = %d\n", b << 1);
 
    // The result is 00000100
    printf("b>>1 = %d\n", b >> 1);
 
    return 0;
   
}

Output

a = 5, b = 9
a & b = 1
a | b = 13
a ^ b = 12
~a = -6
b << 1 = 18
b >> 1 = 4

FAQ – Bitwise Operators In C/C++

Q1. Why is bitwise & operator used in C++?

Ans. It compares each bit of the first operand to the corresponding bit of the second operand, and if both bits are 1, the resulting bit is set to 1. If either or both of the corresponding bits are 0, the resulting bit is set to 0. This operation effectively checks whether both bits are “on” or equal to 1, and if they are, it produces a 1 in the corresponding result bit; otherwise, it produces a 0.

Q2. Can you do bitwise operations in C++?

Ans.
C++ offers a set of bitwise operators for performing operations on integers at the bit level. These operators include & (Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR), << (Left Shift), >> (Right Shift), and ~ (Bitwise NOT). They are valuable for tasks involving bit manipulation, bitmasking, and binary arithmetic in low-level programming.

Q3. How to write bitwise operator in C?

Ans. It is denoted by a single ampersand sign (&), and when used with two integer operands, it results in an output where the corresponding bits in both operands are checked. If both bits are 1, the output is 1; otherwise, it’s 0. This operator effectively checks if two bits are both “on” or equal to 1.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment