- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Bitwise Operators in C++
There are 3 bitwise operators available in c++. These are the bitwise AND(&), bitwise OR(|) and the bitwise XOR(^).
The bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. Both operands to the bitwise inclusive AND operator must be of integral types. For example,
Example
#include <iostream> using namespace std; int main() { unsigned short a = 0x5555; // pattern 0101 ... unsigned short b = 0xAAAA; // pattern 1010 ... cout << hex << ( a & b ) << endl; }
Output
This gives the output −
0
The bitwise OR operator (|) compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. Both operands to the bitwise inclusive OR operator must be of integral types. For example,
Example
#include <iostream> using namespace std; int main() { unsigned short a = 0x5555; // pattern 0101 ... unsigned short b = 0xAAAA; // pattern 1010 ... cout << hex << ( a | b ) << endl; }
Output
This gives the output −
ffff
The bitwise exclusive OR operator (^) compares each bit of its first operand to the corresponding bit of its second operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. Both operands to the bitwise exclusive OR operator must be of integral types. For example,
Example
#include <iostream> using namespace std; int main() { short a = 0x5555; // pattern 0101 ... unsigned short b = 0xFFFF; // pattern 1111 ... cout << hex << ( a ^ b ) << endl; }
Output
This gives the output −
aaaa
Which represents the pattern 1010...
- Related Articles
- Bitwise Operators in C
- What are bitwise operators in C#?
- Bitwise right shift operators in C#
- C# Bitwise and Bit Shift Operators
- Perl Bitwise Operators
- Java Bitwise Operators
- Python Bitwise Operators
- Bitwise operators in Dart Programming
- Explain about bitwise operators in JavaScript?
- What are JavaScript Bitwise Operators?
- C++ Program to Perform Addition Operation Using Bitwise Operators
- What are the bitwise operators in Java?
- What are the differences between bitwise and logical AND operators in C/C++
- What are different bitwise operators types in Python?
- How to multiply a given number by 2 using Bitwise Operators in C#?
