Dart Operators
Dart provides a variety of operators for different kinds of operations, such as arithmetic, relational, logical, and assignment operations. Here is an overview of the common operators in Dart with examples:
1. Arithmetic Operators
Used for performing mathematical operations.
void main() { int a = 10; int b = 3; print('Addition: ${a + b}'); // 13 print('Subtraction: ${a - b}'); // 7 print('Multiplication: ${a * b}'); // 30 print('Division: ${a / b}'); // 3.3333333333333335 print('Modulus: ${a % b}'); // 1 }
2. Relational Operators
Used for comparing two values.
void main() { int a = 10; int b = 20; print('Equal to: ${a == b}'); // false print('Not equal to: ${a != b}'); // true print('Greater than: ${a > b}'); // false print('Less than: ${a < b}'); // true print('Greater than or equal to: ${a >= b}'); // false print('Less than or equal to: ${a <= b}'); // true }
3. Logical Operators
Used for logical operations, often with boolean values.
void main() { var x = 10; var y = 20; print('Logical AND: ${(x == 10) && (y == 20)}'); // true print('Logical OR: ${(x != 0) || (y != 0)}'); // true print('Logical NOT: ${!(x != 0)}'); // false }
4. Assignment Operators
Used to assign values to variables.
void main() { int a = 10; a += 5; A = a+5 // Equivalent to: a = a + 5 print('a += 5: $a'); // 15 a -= 3; // Equivalent to: a = a - 3 print('a -= 3: $a'); // 12 a *= 2; // Equivalent to: a = a * 2 print('a *= 2: $a'); // 24 a /= 4; // Equivalent to: a = a / 4 print('a /= 4: $a'); // 6.0 a %= 3; // Equivalent to: a = a % 3 print('a %= 3: $a'); // 0.0 } ```
5. Unary Operators
Used for operations on a single operand.
void main() { int a = 5; a++; //a = a + 1; print('Increment: $a'); // 6 a--; print('Decrement: $a'); // 5 }
6. Conditional (Ternary) Operator
Used for conditional expressions.
void main() { int a = 5; int b = 10; String result = a > b ? 'a is greater' : 'b is greater'; print(result); // b is greater } ```
7. Type Test Operators
Used for checking types at runtime.
void main() { var x = 100; var y = 'Hello'; print('x is int: ${x is int}'); // true print('y is String: ${y is String}'); // true print('x is not double: ${x is! double}'); // true } ```
8. Bitwise and Shift Operators
Used for bitwise and shift operations.
void main() { int a = 5; // 0101 in binary int b = 3; // 0011 in binary print('Bitwise AND: ${a & b}'); // 1 (0001) print('Bitwise OR: ${a | b}'); // 7 (0111) print('Bitwise XOR: ${a ^ b}'); // 6 (0110) print('Bitwise NOT: ${~a}'); // -6 print('Left shift: ${a << 1}'); // 10 (1010) print('Right shift: ${a >> 1}'); // 2 (0010) }
These operators cover most of the common operations you’ll need to perform in Dart. Each operator is used for specific types of operations, making Dart a powerful and flexible language for various programming tasks.
====================================
Bitwise in Details
I can help you visualize how bitwise operations work by breaking down the binary representations of the numbers and showing how the operations are performed step-by-step.
Let’s start with the numbers you provided:
- a = 5 (binary: 0101)
- b = 3 (binary: 0011)
Bitwise AND (a & b)
0101
& 0011
——
0001
The result of 5 & 3 is 1 (binary: 0001).
Bitwise OR (a | b)
0101
| 0011
——
0111
The result of 5 | 3 is 7 (binary: 0111).
Bitwise XOR (a ^ b)
0101
^ 0011
——
0110
The result of 5 ^ 3 is 6 (binary: 0110).
Bitwise NOT (~a)
0101 // a (5)
~ —-
1010 // Invert all bits: -6 in two’s complement form
The result of ~5 is -6.
Left Shift (a << 1)
0101 // a (5)
<< 1
——
1010 // Shift left by 1: 10 in decimal
The result of 5 << 1 is 10 (binary: 1010).
Right Shift (a >> 1)
0101 // a (5)
>> 1
——
0010 // Shift right by 1: 2 in decimal
The result of 5 >> 1 is 2 (binary: 0010).
Full Example in Dart
void main() { int a = 5; // 0101 in binary int b = 3; // 0011 in binary print('Bitwise AND: ${a & b}'); // 1 (0001) print('Bitwise OR: ${a | b}'); // 7 (0111) print('Bitwise XOR: ${a ^ b}'); // 6 (0110) print('Bitwise NOT: ${~a}'); // -6 print('Left shift: ${a << 1}'); // 10 (1010) print('Right shift: ${a >> 1}'); // 2 (0010) }
Explanation of Each Operation
- Bitwise AND (a & b):
- Both bits must be 1 to get 1. Result: 0001.
- Bitwise OR (a | b):
- Either bit can be 1 to get 1. Result: 0111.
- Bitwise XOR (a ^ b):
- Bits must be different to get 1. Result: 0110.
- Bitwise NOT (~a):
- Inverts all the bits. The result is -6 because in two’s complement, ~0101 becomes 1010, which is -6 in decimal.
- Left Shift (a << 1):
- Shifts bits to the left by 1 position, filling with 0. Result: 1010.
- Right Shift (a >> 1):
- Shifts bits to the right by 1 position, filling with the sign bit for signed integers. Result: 0010.
This breakdown should help you understand how the bitwise operations work with binary numbers.