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?

電子工作メモ - MicroPython

Last updated at Posted at 2024-03-21

MicroPythonメモ

ビット演算

# ビット列表示する
# data   : バイナリデータ
# pattern: 表示フォーマット
def show_bit(data, pattern="08b"):
    print(format(data, pattern))

# 位置を表すビットパターンを作成する
# index  : 位置
# default: ビットシフトする初期値
def bit_pattern(index, default=0x01):
    return default << index

# 指定した位置にビットが立っているか?
# data : バイナリデータ
# index: インデックス
def flag(data, index):
    bit = bit_pattern(index)
    return (data & bit) == bit

def show_flag(data, index):
    print(flag(data, index))

data = 0xc5

show_bit(data)
print("")
for i in range(8):
    show_bit(bit_pattern(i))
    show_flag(data, i)

# 11000101
#
# 00000001
# True
# 00000010
# False
# 00000100
# True
#  :
#  :
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?