Bitwise and shift operators

You can manipulate the individual bits of numbers in Dart. Usually,you’d use these bitwise and shift operators with integers.

OperatorMeaning
&AND
|OR
^XOR
~exprUnary bitwise complement (0s become 1s; 1s become 0s)
<<Shift left
>>Shift right

Here’s an example of using bitwise and shift operators:

  1. final value = 0x22;
  2. final bitmask = 0x0f;
  3. assert((value & bitmask) == 0x02); // AND
  4. assert((value & ~bitmask) == 0x20); // AND NOT
  5. assert((value | bitmask) == 0x2f); // OR
  6. assert((value ^ bitmask) == 0x2d); // XOR
  7. assert((value << 4) == 0x220); // Shift left
  8. assert((value >> 4) == 0x02); // Shift right