Skip to main content

Yet Another Fixed Width Integer (Library)

Project description


Yet Another Fixed Width Integer (Library)

Simple fixed with integers for developer experimentation.



Available through yafwi - PyPI using pip install yafwi.

Some Examples

>>> from yafwi import *
>>> from yafwi import __all__
>>> __all__
('FixedWidthInt', 'BaseFixedWidthInt', 'generate_int', 
 'int8', 'int16', 'int32', 'int64', 'int128', 'int256', 
 'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'uint256', 
 'sbyte', 'byte', 'short', 'ushort', 'int_', 'uint', 'long', 'ulong')

>>> int8(120)

>>> int8(120) + 120  # Arithmetic with Python int preserves type
>>> int8(120) + int16(120)  # Arithmetic will take result in larger width

>>> int16.max, int16.min  # Utility sentinels
(int16(32767), int16(-32768))
>>> uint32.min, uint32.max
(uint32(0), uint32(4294967295))

>>> int8(0b11110000)  # Utility represenations
>>> int8(0b11110000).bin
>>> int8(0b11110000).hex

>>> (~int8(0b10010110)).bin
>>> (int8(0b10010110) >> 2).bin
>>> (int8(0b10010110) << 2).bin
>>> ((uint32(uint8.max) << 10 | uint8.max) ^ uint32.max - (1 << 31)).bin

>>> bytes(uint32(8))  # Output depends on system byteorder
>>> uint32(8).hex  # Output independent of system byteorder

Convenient Aliases

>>> from yafwi import *

>>> uint is uint32
>>> long is int64
>>> ushort is uint16

Arbitrary Sizes

>>> import yafwi

>>> yafwi.generate_int(1024, unsigned=True)
<class 'yafwi.uint1024'>
>>> yafwi.int512
<class 'yafwi.int512'>

>>> yafwi.int3.max, yafwi.int3.min  # Including some fun ones... 
(int3(3), int3(-4))

Platform Independence

If run on a big-endian system, the bytes associated with the numbers will be different, but higher-level functionality should remain unchanged.

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

yafwi-1.0.1.tar.gz (11.1 kB view hashes)

Uploaded source

Built Distribution

yafwi-1.0.1-py3-none-any.whl (6.4 kB view hashes)

Uploaded py3

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