0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

~ bit演算 ~ チートシート

Posted at

#目次
二進数に変換 bin()
論理演算子
 AND &
  OR  |
 NOT ~
 XOR ^

#はじめに

チートシートの扱いついてはここを読んでください

#二進数に変換

bit.py
print(bin(334))
ans.py
>>> 0b101001110

最初の0b以降が2進数表記になってる

#論理演算子

bit.py
# 114 =    1110010(2)
# 514 = 1000000010(2)

print(114 & 514) # AND
print(114 | 514) # OR
print(114 ^ 514) # XOR
print(~114)      # NOT
ans.py
>>> 2    # 0b10
>>> 626  # 0b1001110010
>>> 624  # 0b1001110000
>>> -115 # -0b1110011 ( = 0b0001101)

それぞれの桁に対し、
AND:どちらも1ならば1を返し、それ以外は全て0を返す
OR :どちらか一方のみでも1ならば1を返し、それ以外は全て0を返す
XOR:どちらか一方のみが1ならば1を返し、それ以外は全て0を返す
NOT:1ならば0を返し、0ならば1を返す(2の補数表現より、~N = -(N+1))
Atcoderで時々出てくるXORの性質として、A XOR B = C ⇔ A XOR C = B がある

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?