Bitwise operation - cocoder39/coco39_LC GitHub Wiki

  • set a bit num |= 1 << x;
  • clear a bit num &= ~(1 << x)
  • toggle a bit num ^= 1 << x
  • check a bit bit = (num >> x) & 1
  • change the nth bit to x num ^= (-x ^ number) & (1 << n)
  • get the low bit x & (-x) (eg, 0b100100 -> 0b100, 0b101 -> 0b1)