Bitwise operators manipulate individual binary bits of numeric values and only work with integers.
Bitwise & Shift Operator Reference Table
| Operator | Name | Operation Rule |
|---|---|---|
& | Bitwise AND | Result bit is 1 only if both matching binary bits are 1; otherwise returns 0 |
| | Bitwise OR | Result bit is 1 if either matching binary bit is 1; otherwise returns 0 |
^ | Bitwise XOR | Result bit is 1 when the two matching binary bits differ; returns 0 when they match |
~expr | Bitwise NOT (Unary Operator) | Inverts every binary bit: 0 swaps with 1, 1 swaps with 0 |
<< | Left Shift | Shifts all binary bits left by N positions, filling empty low-order bits on the right with 0 |
>> | Signed Right Shift | Shifts all binary bits right by N positions, filling empty high-order bits on the left with the sign bit (0 for positive numbers, 1 for negative numbers) |
>>> | Unsigned Right Shift | Shifts all binary bits right by N positions, filling empty high-order bits on the left with 0 for all values; negative numbers are first converted to unsigned 32-bit integers before shifting |
Bitwise AND Operator (&)
A result bit equals 1 only when both corresponding binary bits are 1; all other combinations produce 0.
| Bit from First Value | Bit from Second Value | Result Bit |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 0 |
Example:
void main() {
// Single bit validation
assert((1 & 1) == 1);
assert((1 & 0) == 0);
assert((0 & 1) == 0);
assert((0 & 0) == 0);
// Multi-bit integer demonstration
final a = 0x22; // Binary 00100010
final b = 0x0f; // Binary 00001111
assert((a & b) == 0x02); // Result binary 00000010
}Code language: PHP (php)
Bitwise OR Operator (|)
A result bit equals 1 if at least one of the two matching binary bits is 1; returns 0 only when both bits are 0.
| Bit from First Value | Bit from Second Value | Result Bit |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | 0 | 0 |
void main() {
// Single bit validation
assert((1 | 1) == 1);
assert((1 | 0) == 1);
assert((0 | 1) == 1);
assert((0 | 0) == 0);
// Multi-bit integer demonstration
final a = 0x22; // Binary 00100010
final b = 0x0f; // Binary 00001111
assert((a | b) == 0x2f); // Result binary 00101111
}Code language: PHP (php)
Bitwise XOR Operator (^)
A result bit equals 1 when the two matching binary bits hold different values; returns 0 when both bits match.
| Bit from First Value | Bit from Second Value | Result Bit |
|---|---|---|
| 1 | 1 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | 0 | 0 |
Example:
void main() {
// Single bit validation
assert((1 ^ 1) == 0);
assert((1 ^ 0) == 1);
assert((0 ^ 1) == 1);
assert((0 ^ 0) == 0);
// Multi-bit integer demonstration
final a = 0x22; // Binary 00100010
final b = 0x0f; // Binary 00001111
assert((a ^ b) == 0x2d); // Result binary 00101101
}Code language: PHP (php)
Bitwise NOT Operator (~)
This is a unary operator that takes exactly one operand. It flips every binary bit in the value: all 0s become 1s, all 1s become 0s.
| Original Bit | Flipped Result Bit |
|---|---|
| 1 | 0 |
| 0 | 1 |
Example:
void main() {
// Single bit validation
assert((~1 & 1) == 0);
assert((~0 & 1) == 1);
// Multi-bit integer demonstration
final a = 0x22; // Binary 00100010
final mask = 0x0f; // Binary 00001111
assert((a & ~mask) == 0x20); // Combine NOT mask with AND to clear the lower 4 bits
}Code language: PHP (php)
Full combined demo:
void main() {
// Define test values
final value = 0x22; // Hex 0x22 = decimal 34, binary 00100010
final bitmask = 0x0f; // Hex 0x0f = decimal 15, binary 00001111
// 1. Bitwise AND &
print('value & bitmask = ${value & bitmask}'); // 0x02 = 2
assert((value & bitmask) == 0x02);
// 2. Bitwise AND with NOT mask (& ~mask, common pattern to clear low bits)
print('value & ~bitmask = ${value & ~bitmask}'); // 0x20 = 32
assert((value & ~bitmask) == 0x20);
// 3. Bitwise OR |
print('value | bitmask = ${value | bitmask}'); // 0x2f = 47
assert((value | bitmask) == 0x2f);
// 4. Bitwise XOR ^
print('value ^ bitmask = ${value ^ bitmask}'); // 0x2d = 45
assert((value ^ bitmask) == 0x2d);
// 5. Left Shift <<
print('value << 4 = ${value << 4}'); // 0x220 = 544
assert((value << 4) == 0x220);
// 6. Signed Right Shift >> (positive value)
print('value >> 4 = ${value >> 4}'); // 0x02 = 2
assert((value >> 4) == 0x02);
// 7. Signed Right Shift >> (negative value, behavior varies across platforms)
print('-value >> 4 = ${-value >> 4}'); // -0x03 = -3
assert((-value >> 4) == -0x03);
// 8. Unsigned Right Shift >>> (positive value, available in Dart 2.14+)
print('value >>> 4 = ${value >>> 4}'); // 0x02 = 2
assert((value >>> 4) == 0x02);
// 9. Unsigned Right Shift >>> (negative value, always produces positive output)
print('-value >>> 4 = ${-value >>> 4}');
assert((-value >>> 4) > 0);
}Code language: PHP (php)
Program output:
D:\dartdemo\firstdart>dart run
Building package executable...
Built firstdart:firstdart.
value & bitmask = 2
value & ~bitmask = 32
value | bitmask = 47
value ^ bitmask = 45
value << 4 = 544
value >> 4 = 2
-value >> 4 = -3
value >>> 4 = 2
-value >>> 4 = 1152921504606846973
Step-by-step breakdown of each operation:
1. Bitwise AND
00100010
00001111
--------
00000010
The final binary result is 00000010, which equals decimal 2.
2. Bitwise AND with Inverted Mask & ~mask
00100010
11110000 // Result of ~00001111: every bit flipped, 0 ↔ 1
---------
00100000 // Equals 0x20 = decimal 32Code language: JavaScript (javascript)
The value 11110000 comes from ~mask — all bits of the original mask 00001111 are inverted, turning 0s to 1s and 1s to 0s.
3. Bitwise OR |
00100010
00001111
---------
00101111 // Equals 0x2f = decimal 47Code language: JavaScript (javascript)
The result bit is 1 if either input bit is 1; it only returns 0 when both matching bits are 0.
4. Bitwise XOR ^
00100010
00001111
---------
00101101 // Equals 0x2d = decimal 45Code language: JavaScript (javascript)
The result bit is 1 when the two input bits differ, and 0 when they match.
5. Left Shift << 4
00100010 << 4 = 001000100000
Result: 0x220 = decimal 544
6. Signed Right Shift >> 4 (Positive Value)
00100010 >> 4 = 00000010
Result: 0x02 = decimal 2
7. Signed Right Shift >> 4 (Negative Value)
-00100010 >> 4 = -00000011
Result: -0x03 = decimal -3
8. Unsigned Right Shift >>> 4 (Positive Value)
00100010 >>> 4 = 00000010
Result: 0x02 = decimal 2
9. Unsigned Right Shift >>> 4 (Negative Value)
-00100010 >>> 4 = (high bits filled with 0) 00001101... // Output is always a positive integerCode language: JavaScript (javascript)