Skip to main content

Interpreting integer bit patterns as two's complement or unsigned representation

Project description

twos

Converting between two's complement and unsigned integer interpretation (i.e. sum of powers of two) of integer bit patterns.

PyPI - Python Version PyPI

Installation

twos is available on pypi:

pip install twos

Example

Basic usage example (can be found in examples/convert.py):

from twos import to_signed, to_unsigned

bit_width = 8
x = 170 # 0b10101010
pattern = f"{x:b}"

y = to_signed(value=x, bit_width=bit_width)
print(f"Interpreting {pattern} as {bit_width}-bit two's complement: {y}")

z = to_unsigned(value=y, bit_width=bit_width)
print(f"Interpreting {pattern} as {bit_width}-bit unsigned integer: {z}")

Output:

Interpreting 10101010 as 8-bit two's complement: -86
Interpreting 10101010 as 8-bit unsigned integer: 170

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

twos-0.0.1.tar.gz (2.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

twos-0.0.1-py3-none-any.whl (2.5 kB view details)

Uploaded Python 3

File details

Details for the file twos-0.0.1.tar.gz.

File metadata

  • Download URL: twos-0.0.1.tar.gz
  • Upload date:
  • Size: 2.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for twos-0.0.1.tar.gz
Algorithm Hash digest
SHA256 308d9081056920ae0953abdf377836f3056616e45af000f8a6e0bb42b8a80649
MD5 c8c1e6e80c2f6b7b5f8aad0f3eb68ecb
BLAKE2b-256 7eef854fdbd2f39a0f373c2c9da3e52f6387db1c35e8781799cc2cdde48fd8ee

See more details on using hashes here.

File details

Details for the file twos-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: twos-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 2.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for twos-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 551fb04aa0995c5e30d923a8356f1d182145a45edcb15408fef8284907a448dd
MD5 77222fbb274b47e6e4564211837007dd
BLAKE2b-256 7895cd0a7afbdff63fe0dd60997f50c7d5d98d1646116f5ec6f46538055bf723

See more details on using hashes here.

Supported by

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