class IntToBit(object):
def __init__(self,word:int):
if not isinstance(word,int):
raise StopIteration('数据类型不对')
if word<0 or word>65535:
raise StopIteration("数字不在范围")
self.word=word
def __getitem__(self, key):
if not isinstance(key, int):
raise StopIteration('数据类型错误')
if (key<0) or (key > 16):
raise StopIteration("数字不在范围")
return bin(self.word & (1<16):
raise StopIteration("数字不在范围")
if value==True:
self.word=self.word | (1< str:
if not isinstance(key, int):
raise TypeError('数据类型错误')
if not 0 <= key <= 16:
raise ValueError("数字不在范围")
return bin(self.word & (1 << key))
def __setitem__(self, key: int, value: bool):
if not isinstance(key, int):
raise TypeError('数据类型错误')
if not 0 <= key <= 16:
raise ValueError("数字不在范围")
bit = 1 << key
if value:
self.word |= bit
else:
self.word &= ~bit
def __repr__(self) -> str:
return f"{self.word} 的二进制: {bin(self.word)}"
__str__ = __repr__
a=IntToBit(5)
print(a[0])
a[0]=False
print(a)
print(a[0])
print(1 ==True)