位、字节和逻辑门抽象。
bitjo的Python项目详细描述
比特乔伊
位、字节和逻辑门抽象。在
关于
大学的“项目伙伴”主题知识库。高度级位/字节和逻辑电路抽象。
同时Udacity机器学习工程师Nanodegre程序将这些包添加到PyPi中。在
使用
安装
pip install bitjoy
从Bit
开始导入和实例化位类。在
^{pr2}$在__init__
方法中创建一个位传递0
,它将创建另一个类:ZeroBit
。同样的,传递1
,它将创建一个OneBit
。在
逻辑门
还支持逻辑运算符(称为逻辑门或逻辑电路)。在
frombitjoy.dtypesimportLogicalOperator
- 不是
LogicalOperator.not_(zero)# OneBit(1)LogicalOperator.not_(one)# OneBit(0)
- 或者
LogicalOperator.or_(zero,zero)# ZeroBit(0)LogicalOperator.or_(zero,one)# OneBit(1)LogicalOperator.or_(one,zero)# OneBit(1)LogicalOperator.or_(one,one)# OneBit(1)
- 以及
LogicalOperator.and_(zero,zero)# ZeroBit(0)LogicalOperator.and_(zero,one)# ZeroBit(0)LogicalOperator.and_(one,zero)# ZeroBit(0)LogicalOperator.and_(one,one)# OneBit(1)
还支持其他逻辑门:nor_
,nand_
,异或{
字节
还有一个字节类。在
frombitjoy.dtypesimportBytes
将位列表传递给Bytes的构造函数以创建Bytes实例。注意字节只有8位。所以或多或少的传递都会抛出一个错误。
bits=[Bit(0),Bit(0),Bit(0),Bit(0),Bit(1),Bit(1),Bit(0),Bit(0)]b=Bytes(bits)# Bytes(0, 0, 0, 0, 1, 1, 0, 0)
要创建一个简单的字节,请使用int_to_bytes
函数来帮助您。在
frombitjoy.utilsimportint_to_bytesb1=int_to_bytes(10)# Bytes(0, 0, 0, 0, 1, 0, 1, 0)b2=int_to_bytes(175)# Bytes(1, 0, 1, 0, 1, 1, 1, 1)
- 添加字节
Bytes类还支持使用
__add__
方法的+
操作数。在
b1_b2=b1+b2# Bytes(1, 0, 1, 1, 1, 0, 0, 1)
- 铸造
字节支持其他数字类型的转换:
int
、bin
、oct
和hex
。在
int(b1_b2)# 185bin(b1_b2)# '0b10111001'oct(b1_b2)# '0o271'hex(b1_b2)# '0xb9'
加法器
对于算术运算符,Adder
类有两种方法进行加法:half
:到half-adderfull
:到full-adder
- 项目
标签: