What is the use of bitwise operators in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

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

I have come across bitwise operators in C++, but I don't know why it is used. When I searched it didn't come. Please help.

26th Aug 2017, 2:17 PM
Abinanthan
Abinanthan - avatar
4 Answers
+ 3
The bitwise operators work on bits of a number rather than its value. The bitwise operators convert the respective number to bits, and then apply the operation on each bit one by one. Some of the bitwise operators are : & (Bitwise AND) - Performs AND of bits in numbers. Eg - 2&3 = 2. // Since 2 is 10 and 3 is 11. Thus 10 & 11 = 10. | (Bitwise OR) - Performs OR of bits in numbers. Eg - 2|3 = 3. // Since 2 is 10 and 3 is 11. Thus 10 | 11 = 11. ^ (Bitwise XOR) - Performs XOR of bits in numbers. Eg - 2^3 = 1. // Since 2 is 10 and 3 is 11. Thus 10^11 = 01. ~ (Bitwise NOT) - Performs NOT of bits in numbers. Eg ~3 = 0. //Since 3 is 11. Thus, ~3 = 00. (Inverting bits). >> (Right Shift) - Shift the first n bytes of a number to right. Eg - 2>>2 = 0. //Since 2 is 10 and shifting 2 bits to right gives 00. << (Left Shift) - Shift the first n bytes of a number to left. Eg - 3<<1 = 6. // Since 3 is 011 and shifting 1 bit to left gives 110. Unlike logical operators, they just return the post operated value, rather than checking for truth like in && , || or !...
26th Aug 2017, 2:51 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 9
bitwise operators are used in testing conditions like " &&, &, || , == " .
26th Aug 2017, 2:27 PM
P R
P R - avatar
+ 3
thanks
27th Aug 2017, 2:50 AM
Abinanthan
Abinanthan - avatar
+ 2
I was asking uses. I know how it works
26th Aug 2017, 3:26 PM
Abinanthan
Abinanthan - avatar