Bitwise operators in Arduino

Share

With prior knowledge of arithmetic and boolean operators in Arduino programming, we now shift the focus to a new concept, i.e., Bitwise operators.

Bitwise operators in Arduino

Bitwise operators let you perform logical operations bitwise between two operands (numbers). These operators allow you to apply the Boolean operators on decimals by converting them into binary. Then, the operation is performed on the binary of the number (decimal) bit by bit.

Decimal to binary conversion
Decimal to binary conversion

Any operation can be performed once the numbers are converted from decimal to binary. There exist six Bitwise operators named:

  • Bitwise AND operator

  • Bitwise OR operator

  • Bitwise NOT operator

  • Bitwise XOR operator

  • Bitwise shift left

  • Bitwise shift right

Bitwise AND operator

The Bitwise AND is an operator that gives 1 in output if and only if both the input bits are one or true. For Bitwise AND the & operator is used. The AND operation is performed bit by bit.

Bitwise AND operation
Bitwise AND operation
int val1 =12;
int val2=8;
int ans; // for storing the result
void setup() { // put your setup code here, to run once:
Serial.begin(9600);//initializing serial communication
Serial.print("AND :");
ans=val1 & val2;
Serial.println(ans);
}
void loop() {
// put your main code here, to run repeatedly:
}
Implementation of Bitwise AND operator

Bitwise OR operator

The Bitwise OR operator gives 1 in output if any of the inputs is one or true. It provides 0 or false as output only when both the inputs are 0 or false. For Bitwise OR the | operator is used.

Bitwise OR operation
Bitwise OR operation
int val1 =12;
int val2=8;
int ans; // for storing the result
void setup() { // put your setup code here, to run once:
Serial.begin(9600);//initializing serial communication
Serial.print("OR:");
ans=val1|val2;
Serial.println(ans);
}
void loop() {
// put your main code here, to run repeatedly:
}
Implementation of Bitwise OR operator

Bitwise NOT operator

The Bitwise NOT operator inverts all the bits of the input number. All 1's are inverted to 0's and 0's to 1's. For the Bitwise NOT the ~ operator is used. We do not need two inputs for the Bitwise NOT operator as we already know the NOT operator takes in only one input.

Bitwise NOT operation
Bitwise NOT operation
int val1 =12;
int ans; // for storing the result
void setup() { // put your setup code here, to run once:
Serial.begin(9600);//initializing serial communication
Serial.print("NOT:");
ans=~val1;
Serial.println(ans);
}
void loop() {
// put your main code here, to run repeatedly:
}
Implementation of Bitwise NOT operator

Bitwise XOR operator

The Bitwise XOR operator works similarly to the XOR gate. It gives 1 in output if and only if both the inputs are different, i.e., one input value is 1, and the other is 0, or vice versa. For the Bitwise XOR the ^ operator is used.

Bitwise XOR operation
Bitwise XOR operation
int val1 =12;
int val2=8;
int ans; // for storing the result
void setup() { // put your setup code here, to run once:
Serial.begin(9600);//initializing serial communication
Serial.print("XOR:");
ans=val1^val2;
Serial.println(ans);
}
void loop() {
// put your main code here, to run repeatedly:
}
Implementation of Bitwise XOR operator

Bitwise shift left operator

The Bitwise shift left operator shifts the bits to the left. It shifts the bits to the defined side, the number of times it is asked to. For the Bitwise shift left the <<c operator is used. The number of shifts to be taken is written ahead of this symbol, i.e., c in this case.

Bitwise shift left operation
Bitwise shift left operation
int val1 =12;
int ans; // for storing the result
void setup() { // put your setup code here, to run once:
Serial.begin(9600);//initializing serial communication
Serial.print("Left Shift:");
ans=val1<<1;
Serial.println(ans);
}
void loop() {
// put your main code here, to run repeatedly:
}
Implementation of Bitwise shift left

Bitwise shift right operator

The Bitwise shift right operator shifts the bits to the right. It shifts the bits to the defined side, the number of times it is asked to. For the Bitwise shift right the >>c operator is used. The number of shifts to be taken is written ahead of this symbol, i.e., c in this case.

Bitwise shift right operation
Bitwise shift right operation
int val1 =12;
int ans; // for storing the result
void setup() { // put your setup code here, to run once:
Serial.begin(9600);//initializing serial communication
Serial.print("Right Shift:");
ans=val1>>1;
Serial.println(ans);
}
void loop() {
// put your main code here, to run repeatedly:
}
Implementation of Bitwise shift right

It is to remember what type of operand the shifting is done. If that is int type, then there's no issue, but in the case of byte type, only 8 bits are included and readable.

Conclusion

The Bitwise operators are used to apply the logical operation on two integers bit by bit, and these operators aid in making the condition needed to accomplish a particular task. This Answer concisely covers each sort of Bitwise operator with examples to provide a good understanding of these operators and a basic Arduino code.

Copyright ©2024 Educative, Inc. All rights reserved