In this tutorial you will learn about the Dart Bitwise operators and its application with practical example.
Dart Bitwise Operators
Bitwise operator are used to perform bit level operation over its operand. Let A = 60; and B = 13;
Binary Equivalent:-
1 2 |
A = 0011 1100 B = 0000 1101 |
Operator | Meaning | Example | Description |
---|---|---|---|
& | Binary AND | (A & B) | It returns 12 which is 0000 1100 |
| | Binary OR | (A | B) | It returns 12 which is 0000 1100 |
^ | Binary XOR | (A ^ B) | It returns 49 which is 0011 0001 |
~ | Ones Complement | (~A ) | It returns -60 which is 1100 0011 |
<< | shift left | A << 2 | It returns 240 which is 1111 0000 |
>> | shift right | A >> 2 | It returns 15 which is 0000 1111 |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
void main() { var num1 = 40; var num2 = 5; var num3 =0; print("W3Adda - Dart Bitwise Operators"); //Bitwise AND print("num1 & num2 = ${(num1 & num2)}"); //Bitwise OR print("num1 | num2 = ${(num1 | num2)}"); //Bitwise XOR print("num1 ^ num2 = ${(num1 ^ num2)}" ); //Binary Complement Operator print("~num1 = ${(~num1)}" ); //Binary Left Shift Operator num3 = num1 << 2; print("num1 << 1 = ${num3}"); //Binary Right Shift Operator num3 = num1 >> 2; print("num1 >> 1 = ${num3}" ); } |
When you run the above Dart program, you will see the following output.
Output:-