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 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.
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
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.
int val1 =12;int val2=8;int ans; // for storing the resultvoid setup() { // put your setup code here, to run once:Serial.begin(9600);//initializing serial communicationSerial.print("AND :");ans=val1 & val2;Serial.println(ans);}void loop() {// put your main code here, to run repeatedly:}
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.
int val1 =12;int val2=8;int ans; // for storing the resultvoid setup() { // put your setup code here, to run once:Serial.begin(9600);//initializing serial communicationSerial.print("OR:");ans=val1|val2;Serial.println(ans);}void loop() {// put your main code here, to run repeatedly:}
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.
int val1 =12;int ans; // for storing the resultvoid setup() { // put your setup code here, to run once:Serial.begin(9600);//initializing serial communicationSerial.print("NOT:");ans=~val1;Serial.println(ans);}void loop() {// put your main code here, to run repeatedly:}
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.
int val1 =12;int val2=8;int ans; // for storing the resultvoid setup() { // put your setup code here, to run once:Serial.begin(9600);//initializing serial communicationSerial.print("XOR:");ans=val1^val2;Serial.println(ans);}void loop() {// put your main code here, to run repeatedly:}
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.
int val1 =12;int ans; // for storing the resultvoid setup() { // put your setup code here, to run once:Serial.begin(9600);//initializing serial communicationSerial.print("Left Shift:");ans=val1<<1;Serial.println(ans);}void loop() {// put your main code here, to run repeatedly:}
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.
int val1 =12;int ans; // for storing the resultvoid setup() { // put your setup code here, to run once:Serial.begin(9600);//initializing serial communicationSerial.print("Right Shift:");ans=val1>>1;Serial.println(ans);}void loop() {// put your main code here, to run repeatedly:}
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.
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.