Python中的固定宽度整数类型(例如uint32) [英] Fixed width integer types (e.g. uint32) in Python

查看:587
本文介绍了Python中的固定宽度整数类型(例如uint32)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些数学运算,尤其是从硬件驱动程序读取的数据,可能取决于数据类型的固定宽度.示例:按位移位.创建具有固定宽度(例如uint32,int16等)的整数变量(会相应地溢出/移位)的Python方法是什么?

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 creating integer variables with fixed width (e.g. uint32, int16 etc.) that would overflow/shift accordingly?

推荐答案

我建议 fixedint 库.该库中的类按以下约定命名:

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

[Mutable][U]Int<N>

因此,对于您的两个示例,类将是

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)

这篇关于Python中的固定宽度整数类型(例如uint32)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆