Programs

# Bitwise Operator in C & C++ Programming

In RAM, data is organized as a sequence of bytes. Every byte contains eight consecutive bits. The C & C++ bitwise operators are helpful when you want to perform some arithmetic operations on bits of the data. They are quite faster and are occasionally used to enhance a program’s efficiency. They work on Bitwise Algorithms that perform operations at the bit-level or manipulate bits in various ways.

The types of Bitwise Operator in C & C++:

1. Bitwise AND
2. Bitwise OR
3. Bitwise NOT
4. Bitwise Exclusive-Or (XOR)
5. Left shift Operator
6. Right Shift Operator

Let’s get into the details of each of them:

## 1. Bitwise AND:

The bitwise AND operator in C or C++ uses a single ampersand (&).  It takes two numbers as operands and performs AND on all of their bits. The result is 1 only if both the bits are 1. If any of the bits is 0, the result is 0. Essentially, this binary operator in C & C++ takes the logical AND of the bits in every position of the given two numbers in binary form.

Example:

Suppose the binary representation of two numbers a & b is:

a = 01011000

b = 10111001

So, a & b = 00011000

As seen from the above example, the result shows ‘1’ only at those places where both the numbers a and b have 1. For all other places, the result shows ‘0’.

## 2. Bitwise OR:

In C & C++, Bitwise OR works quite similarly to bitwise AND. The only difference is that in the bitwise OR operator, for the bit’s position to be ‘1’ in the result, only one of the two bits should be ‘1’. This operator takes two numbers as operands and performs OR on every bit of the two numbers.

It works in the following way:

• If both the bits are ‘1’, the result is ‘1’.
• If both the bits are ‘0’, the result is ‘0’.
• If any of the bits are ‘1’, the result is ‘1’.

It is one of the simplest and most frequently used C++ bitwise operators. Its symbol is the pipe |. Moreover, it is identical to the Boolean logical operator ||.

Example:

Suppose the binary representation of two numbers a| b is:

a = 01101001

b = 10111000

So, a| b = 11111001

## 3. Bitwise NOT:

Also known as the Bitwise NOT operator, it flips every bit in the result. Its symbol is the tilde (~). An easy way to remember the working of this operator is that the tilde is occasionally known as a twiddle, and the bitwise complement twiddles each bit. It means that if you have a 1, the result is 0, and vice versa. In other words, this bitwise operator in C & C++ takes one number and inverts all bits.

When determining the largest possible value for any unsigned number, this one is one of the most useful C++ bitwise operators.

Example:

unsigned int num = ~0;

Here, 0 means all 0s: 00000000. So, implementing the Bitwise NOT operator gives the result as 1s: 11111111. Because num is an unsigned int, you need not be concerned about sign bits or two’s complement. 1s is the largest possible number in the result.

One of the crucial characteristics of the Bitwise NOT operator is 2’s Complement. The 2’s complement of a number equals the complement of that number plus 1.

Example:

a = 00000000

So, its 2’s complement = -(11111111+1) = -00000000 = -0(decimal)

Note: The bitwise complement of any number N equals -(N+1).

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

## 4. Bitwise Exclusive-OR (XOR):

The Exclusive-OR operation accepts two inputs and outputs a 1 if either one or the other inputs is a 1. But the result is 0 if both are 1. If both inputs are 0 or both are 1, the result is 0. The result of XOR is 1 only if the two bits are different. The symbol of this binary operator in C & C++ is caret (^). Moreover, this bitwise operator in C & C++ is abbreviated as XOR. It performs the exclusive-OR operation on every pair of bits. It is essential to note that there is no Boolean operator counterpart to this operator.

Example:

Suppose we have two numbers as

a = 10101010

b = 01110010

Now a ^ b = 10101010 ^ 01110010 = 11011000

You can understand XOR in the following way. Suppose you have some bit, either 0 or 1, that you call A. Now when you take A XOR 0, you will get A back. So, if A is 1, you get 1, and vice versa.  When you take A XOR 1, it means that you flip A. So, if A is 1, you get 0 and vice versa.

If you implement the bitwise XOR operation twice, i.e., you have two bits, A and B, and you set C = A XOR B, then perform C XOR B. In that case, the result you get is A XOR B XOR C. It either flips each bit of A twice or doesn’t flip any bit. Hence, you simply get back an A as it is.

You can consider the binary XOR operation as a kind of selective twiddle. If you implement XOR on two numbers, one of which is all 1s, the result is equivalent to a twiddle.

## 5. Left shift Operator:

This bitwise operator shifts all bits to the left by a certain number of the specified bits. The bit positions that the left shift operator has emptied get filled with 0. Its symbol is <<.

In C or C++, it works such that it first takes two numbers, left shifts the first operand’s bits, and the second operand indicates by how many places to shift the number.

Syntax:

a <<= 5

Here is the example C/C++ program to implement the left shift operator:

int main()

{

int a = 5;

a <<= 2;

count << “x : ” << x << endl;

}

Output: a = 20

## Popular Courses & Articles on Software Engineering

 Popular Programs Executive PG Program in Software Development - IIIT B Blockchain Certificate Program - PURDUE Cybersecurity Certificate Program - PURDUE MSC in Computer Science - IIIT B Other Popular Articles Cloud Engineer Salary in the US AWS Solution Architect Salary in US Backend Developer Salary in the US Front End Developer Salary in US Web developer Salary in USA Scrum Master Interview Questions How to Start a Career in Cyber Security Career Options in the US for Engineering Students

## 6. Right shift Operator:

This bitwise operator shifts all bits to the right by a certain number of the specified bits. Its symbol is >>. In C or C++, this operator first takes two numbers, right shifts the first operand’s bits, and the second operand specifies the amount of shift on the number.

Syntax:

a >>= 5

An example C/C++ program to implement the right shift operator:

int main() {

int a = 5;

a >> = 2;

count << “x : ” << x << endl;

}

Output: a = 1

## Popular Courses & Articles on Software Engineering

 Popular Programs Executive PG Program in Software Development - IIIT B Blockchain Certificate Program - PURDUE Cybersecurity Certificate Program - PURDUE MSC in Computer Science - IIIT B Other Popular Articles Cloud Engineer Salary in the US AWS Solution Architect Salary in US Backend Developer Salary in the US Front End Developer Salary in US Web developer Salary in USA Scrum Master Interview Questions How to Start a Career in Cyber Security Career Options in the US for Engineering Students

## When should you use the bitwise operators in C & C++?

• Saves space:

Bitwise operators help save space. One common issue encountered when working at the individual bits level is that if you want more space or to save time, you may have to redesign the huge portions of your program. But using bitwise operators in C & C++ can eliminate dependencies; for example, you can use ~0 to get the largest possible integer. The bit shifting to multiply by two is a typical operation. Hence, it doesn’t impact the readability, unlike the advanced use of the bit manipulation can impact in some instances.

• Encryption:

If you are working on some type of encryption or on a system that needs bit fields to be used to store the Boolean attributes.

## Unique facts about bitwise operators

• The bitwise OR of two numbers is simply the sum of those numbers if no carry is involved. Else, you simply need to add their bitwise AND. For example, suppose x = 5(101) and y = 2(010). Because there is no carry involved, the sum is simply x|y. But, if we now change ‘x’ to 6 (110), the sum will change to x|y + x&y because now carry is involved.
• The bitwise XOR operator is the most valuable from the technical interview viewpoint. An example of one of the interview problems related to this is discussed here. “For the given set of numbers consisting of all elements occurring for an even number of times except one number, find out the odd occurring number.” You need to do XOR to all numbers to solve this problem.
• The right and left shift bitwise operators must not be used for the negative numbers. The second operand, which determines the number of shifts, contains a negative number, which leads to undefined behaviour in C & C++. For example, the results of both 1 >>- 1 and 1 << -1 are undefined.
• The behaviour is unspecified if the number’s shift exceeds the integer’s capacity. For example, 1 << 65 is unspecified if integers are stored in 64 bits format.
• No shift operation occurs if the second operand (the one that determines the number of shifts) is 0.

If you aim to learn in detail about the frontend development (JavaScript, HTML, CSS), backend (NoSQL-MongoDB), and microservices, then you can pursue UpGrad’s Master of Science in Computer Science course. Delivered by IIIT Bangalore & LJMU Alumni Status, this course helps you land your career as a software engineer/full-stack developer with the tech giants throughout the world.

The course covers complimentary access to the Career Transition Bootcamp for beginner and non-tech coders. You will learn more than ten programming languages & tools in this course, thoroughly preparing you to take over complex roles in the industry.

## What is the use of the bitwise operators in C++?

In C++, bitwise operators carry out operations on integer data at the binary level. They operate on bit patterns that involve the manipulation of individual bits. Therefore, a bitwise operation is also known as bit-level programming. Bitwise operators in C++ are primarily used for faster calculation since they work only on two digits, i.e., 0 and 1. These operators can also do testing and shift the actual bits.

## Can you use the left and right shift operators together in a code?

Yes, you can combine the left shift and right shift operators, and after that, you can extract the data from an integer expression.

## Does the complementary operator flip the whole sequence of code?

No, it doesn’t. It flips the 1s and 0s. So, all the 1s become 0s and vice-versa.

## Why the other name of the Bitwise complement operator is one’s complement operator?

The reason is it always takes only one operand or value. It is a unary operator. When you perform a complement on any bits, all the 0s become 1s and vice versa. For example, if you have an integer expression with the value 1111 0000. After performing the bitwise complement operation, the value becomes 0000 1111.