Skip to main content

Fixed-width integer and calculation for Python

Project description

uint CI Coverage Status

Fixed-width integer and calculation for Python

$ pip install uint

Fixed-width integers for Python

uint provides two classes: Uint and Int. They are calculated like int but is fixed-width so they behave like ones that you're familiar with ... like in C language.

In [1]: from uint import Uint, Int

In [2]: u, i = Uint(0xff, 8), Int(0xff, 8)

In [3]: u
Out[3]: <uint8, value=255>

In [4]: i
Out[4]: <int8, value=-1>

In [5]: u << 2 >> 1
Out[5]: <uint8, value=126>

In [6]: i << 2 >> 1
Out[6]: <int8, value=-2>

Of course they overflows when you do like:

In [1]: from uint import Uint, Int

In [2]: u, i = Uint(0xff, 8), Int(0x7f, 8)

In [3]: u, i
Out[3]: (<uint8, value=255>, <int8, value=127>)

In [4]: u+1, i+1
Out[4]: (<uint8, value=0>, <int8, value=-128>)

Register with multiple fields inside

The original purpose of this package is to reduce labor of calculating "register values" in several MCUs and SoCs. See examples directory for detailed usage.

In [1]: from uint import Register

In [2]: reg = Register('reg')

In [3]: reg[15:8] = 'f1'

In [4]: reg[7:0] = 'f2'

In [5]: reg['f1'] = 0x5A

In [6]: reg['f2'] = 0x0F

In [7]: hex(reg.encode())
Out[7]: '0x5a0f'

In [8]: reg.decode(0x1234)

In [9]: reg['f1'].value.wire.hex
Out[9]: "8'h12"

In [10]: reg['f2'].value.wire.hex
Out[10]: "8'h34"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uint-0.2.1.tar.gz (7.1 kB view details)

Uploaded Source

File details

Details for the file uint-0.2.1.tar.gz.

File metadata

  • Download URL: uint-0.2.1.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.2

File hashes

Hashes for uint-0.2.1.tar.gz
Algorithm Hash digest
SHA256 f114406f330c9d2caa1fc974280d3d19bab175191712fdb0475ba25941b685fd
MD5 4624431c206ac7a116f5b4d84bab8f6a
BLAKE2b-256 b95e423241e382403a3a52dd32a51298ca32bb63d0d404cf8e04e0b449a5191d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page