- 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 XOR in Arduino
Just like other bitwise operators, bitwise XOR also applies on the corresponding bits individually.
The operator is ^ and the syntax is,
a ^ b
where a and b are the two numbers to be XORed.
The truth table for XOR is given below −
P | Q | P^Q |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
As you can see, the XOR operator returns 1 only when the two bits are different.
If you perform 10 ^ 3, this is the computation that will happen at the bit level (assuming your board represents integers using 16 bits)
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 10 | |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 3 | |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 10 ^ 3 = 9 |
Example
Let’s verify this on the Serial Monitor. The code is given below −
void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); int a = 10; int b = 3; Serial.println(a ^ b); } void loop() { // put your main code here, to run repeatedly: }
Output
The Serial Monitor output is shown below −
As you can see, the output is exactly as expected.
- Related Articles
- Bitwise NOT in Arduino
- What is Bitwise XOR in C++?
- What is Bitwise XOR Operator (^) in JavaScript?
- Bitwise AND and OR in Arduino
- What is JavaScript Bitwise XOR (^) Operator?
- What is Bitwise XOR Assignment Operator (^=) in JavaScript?
- Bitwise Right/ left shift numbers in Arduino
- How to bitwise XOR of hex numbers in Python?
- Count pairs with Bitwise XOR as EVEN number in C++
- Count pairs with Bitwise XOR as ODD number in C++
- How to perform bitwise XOR operation on images in OpenCV Python?
- Program to find XOR sum of all pairs bitwise AND in Python
- How to perform Bitwise XOR operation on two images using Java OpenCV?
- Find subsequences with maximum Bitwise AND and Bitwise OR in Python
- Bitwise Operators in C

Advertisements