Use of Arithmetic Operators in Arduino
Bitwise operators in Arduino work directly on the binary representation of numbers.
They manipulate individual bits (0s and 1s) within an integer or byte variable.
Types of Bitwise Operators in Arduino:
-------------------------------------
1. AND (&)
- Compares each bit of two numbers.
- Result bit is 1 only if both bits are 1.
2. OR (|)
- Compares each bit of two numbers.
- Result bit is 1 if at least one bit is 1.
3. XOR (^)
- Compares each bit of two numbers.
- Result bit is 1 if bits are different.
4. NOT (~)
- Inverts all bits (0 becomes 1, 1 becomes 0).
5. LEFT SHIFT (<<)
- Shifts bits to the left by a specified number of positions (adds zeros on the right).
6. RIGHT SHIFT (>>)
- Shifts bits to the right by a specified number of positions (drops bits on the right).
Page 1
Use of Arithmetic Operators in Arduino
Example Arduino Sketch:
-----------------------
void setup() {
[Link](9600);
byte a = 6; // 00000110 in binary
byte b = 3; // 00000011 in binary
[Link]("a & b: ");
[Link](a & b); // 2 (00000010)
[Link]("a | b: ");
[Link](a | b); // 7 (00000111)
[Link]("a ^ b: ");
[Link](a ^ b); // 5 (00000101)
[Link]("~a: ");
[Link]((byte)~a); // 249 (11111001)
[Link]("a << 1: ");
[Link](a << 1); // 12 (00001100)
[Link]("a >> 1: ");
[Link](a >> 1); // 3 (00000011)
Page 2
Use of Arithmetic Operators in Arduino
void loop() {
// Empty loop
Explanation:
- Bitwise operations are useful for low-level programming tasks like hardware control, setting or
clearing bits in registers, and efficient data manipulation.
Page 3