位运算(持续更新)

判断奇偶性

  if num & 1 == 1:
      num % 2 == 1
      # nums is odd number
  if num & 1 == 0:
      num % 2 == 0
      # num is even number

异或

当 num 是偶数时,num + 1 = num ⊕ 1
当 num 是奇数时,num - 1 = num ⊕ 1

  if num % 2 == 1:
      num ^ 1 == num - 1
  if num % 2 == 0:
      num ^ 1 == num + 1
posted @ 2022-02-14 09:28  小艾衰  阅读(27)  评论(0编辑  收藏  举报