Skip to main content

Convert two's complement integer to gray code and vice versa

Project description

Introduction

Gray code or reflected binary code (RBC) is an ordering of binary numbers such that two successive numbers only differ in one bit. In essence, this is an alternative to two's complement integers when it is useful to change only one bit between successive numbers.

This library implements functions to convert gray code to two's complement integer and vice versa.

See LICENSE.md for copyright information. This library is licensed under the MIT license.

See https://en.wikipedia.org/wiki/Gray_code for details and applications of gray codes.

Installation

$ python3 setup.py install

Or from PyPi:

$ pip3 install graycode

Example

import graycode

for i in range(8):
    print(i, '{:03b}'.format(graycode.tc_to_gray_code(i)))

=>

0 000
1 001
2 011
3 010
4 110
5 111
6 101
7 100

Reverse is also provided:

graycode.gray_code_to_tc(0b011)

=>

2

gen_gray_codes(n) produces all n bit gray codes:

graycode.gen_gray_codes(3)

=>

[0, 1, 3, 2, 6, 7, 5, 4]

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

graycode-1.0.5.tar.gz (3.8 kB view details)

Uploaded Source

File details

Details for the file graycode-1.0.5.tar.gz.

File metadata

  • Download URL: graycode-1.0.5.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for graycode-1.0.5.tar.gz
Algorithm Hash digest
SHA256 43d4a79e7787c5aea89c554d2c462783af91cbb65ebb13284f519874145be0f8
MD5 dab54466bedcd1418fe298c50e4c3a7c
BLAKE2b-256 3bc17431f895ae3bc6c9554922bd1bb959b78138a348183332c7f22da1a32c1a

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