Skip to main content

Python Integer Representations & Arithmetic Library

Project description

Python Integer Representations & Arithmetic Library

This tool may be useful to system software developers (for example, compiler or binutils developers). Here are functions for representing integers in a convenient form and functions for arithmetic over integers of arbitrary format.


$ git clone
$ cd pirep
$ sudo python3 install clean

Alternative way:

$ sudo pip3 install pirep


The integer arguments of almost all functions can be of any of 4 formats:

  • decimal — usual python integer (0, 7, -2, ...);
  • hexadecimal — string with or without prefix '0x' ('0xf', 'a7', and even '-0xcf') or usual hexadecimal int (0x3, 0xd, ..., but python convert it to usual decimal int);
  • binary — string with prefix '0b' ('0b010110', '0b0', ...) or usual 0b... integers;
  • float — usual python float (Why not? 0.0, 17., -3., ...).

You can globally specify the default output format for arithmetic functions by call psetmode() or locally for each interface (by their parameter fmt). Hexadecimal / binary output format is string with prefix '0x' / '0b'.


c2repr gives two's complement representation in any output format in accordance with the current signedness and int width (signed 64-bit by default).

>>> from pirep import *

>>> c2repr(5, 'b')

>>> c2repr(-10)

>>> c2repr('8000000000000000', 'd')

>>> c2repr('4000000000000000', 'd')

Meaning of decomp by example. The operation sethi %hi(0x103c00), %o3 (see Sparc Instruction Set) is encoded to 1700040f. We can clearly expand the code instructions on its fields. For this we need to know the numbers of the last bits of all fields:

>>> decomp('1700040f', (31, 29, 24, 21))
['00', '01011', '100', '0000000000010000001111']

>>> decomp('1700040f', (31, 29, 24, 21), 'h')
['0x0', '0xb', '0x4', '0x40f']

Why '0x40f' and not 0x103c00? It's ok. sethi sets just 22 high bits:

# Left shift
>>> psll('0x40f', 10)

We could just decompose any integer by bytes:

>>> decomp(3932166)
['00111100', '00000000', '00000110']

More verbose way to decode — use decode() with object of class Enc:

>>> e = Enc('sethi', (('opc', 31), ('rd', 29), ('opc', 24), ('imm22', 21)))

>>> decode('1700040f', e, borders=True)
 opc     rd    opc           imm22
  00   01011   100   0000000000010000001111
31-30  29-25  24-22  21-------------------0

>>> e.field(('opc', 31)).add_only_true(0)
>>> e.field(('rd', 29)).add_verbose(11, 'eleven')

>>> decode('1700040f', e, 'h')
opc   rd  opc  imm22
0x0  0xb  0x4  0x40f

rd[29:25]:   eleven

It is convenient to have a separate module that contains all the encodings you often use.


You can globally specify the signedness and the integer width by psetmode():

>>> from pirep import *

# Default mode: signed, 64-bit, hexadecimal default output
>>> pgetmode()
[True, 64, 'h']
>>> psub('0x100', 15)

# Signed 8-bit int with decimal output by default
>>> psetmode(True, 8, 'd')
>>> psub('0x100', 15.)
>>> psub('0x100', '0b1111', 'b')

pirep contains several other elementary arithmetic functions:

>>> psetmode(True, 8, 'd')
>>> pmul(3, padd(pdiv('f', '0b100'), prem(11, '0x3')))
>>> psetbits(15, (3, 5), '0b110')
>>> padd(pintmin(), pintmax())

Project details

Download files

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

Files for pirep, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size pirep-0.0.2-py3-none-any.whl (8.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size pirep-0.0.2.tar.gz (7.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page