Fixed width integer types (e.g. uint32) in Python

后端 未结 2 1268
执念已碎
执念已碎 2021-01-21 19:49

Certain mathematical operations, especially on data read from hardware drivers, can depend on fixed width of the data type. Example: bitwise shift. What is the Pythonic way of c

2条回答
  •  囚心锁ツ
    2021-01-21 20:24

    I would suggest the fixedint library. The classes in that library are named in the following convention:

    [Mutable][U]Int
    

    So for your two examples, the classes would be

    #    C++                 Python fixedint
     std::uint32                 UInt32
     std::uint16                 UInt16
    

    This supports things like bit-shifting, etc

    >>> a = fixedint.UInt32(14)
    >>> a
    UInt32(14)
    >>> a << 2
    UInt32(56)
    

提交回复
热议问题