Flutter for Beginners
上QQ阅读APP看书,第一时间看更新

Bits manipulation

Dart provides bitwise and shift operators to manipulate individual bits of numbers, usually with the num type. They are as follows:

  • &: To apply logical AND to operands, checking whether the corresponding bits are both 1
  • |: To apply logical OR to operands, checking whether at least one of the corresponding bits is 1
  • ^: To apply logical XOR to operands, checking whether only one but not both of the corresponding bits is 1 
  • ~operand: To invert the bits of the operand, such as 1s becoming 0s and 0s becoming 1s
  • <<: To shift the left operand in x bits to the left (this shifts 0s from the right)
  • >>: To shift the left operand in x bits to the right (discarding the bits from the left)

Like arithmetic operators, the bitwise ones also have shortcut assignment operators, and they work in the exact same way as the previously presented ones; they are <<=, >>=, &=, ^=, and |=.