Skip to main content

Bit, Bytes and Logical Gates Abstraction.

Project description

BitJoy

Bit, Bytes and Logical Gates Abstraction.

About

Repository to 'Padrões de Projeto' subject at university. A height level Bit/Bytes and logical circuits abstractions.
Also Udacity Machine Learning Engineer Nanodegre program to add these package to PyPi.

Usage

Installing

pip install bitjoy

Starting with Bit

Importing and instantiating Bit class.

from bitjoy.dtypes import Bit

zero = Bit(0)
# ZeroBit(0)
one = Bit(1)
# OneBit(1)

Creating a bit passing 0 in the __init__ method, it will create another class: ZeroBit. The same away, passing 1, it will create a OneBit.

Logical Gates

There is also support to logical operators (called logical gates or logical circuits).

from bitjoy.dtypes import LogicalOperator
  • NOT
LogicalOperator.not_(zero)
# OneBit(1)
LogicalOperator.not_(one)
# OneBit(0)
  • OR
LogicalOperator.or_(zero, zero)
# ZeroBit(0)
LogicalOperator.or_(zero, one)
# OneBit(1)
LogicalOperator.or_(one, zero)
# OneBit(1)
LogicalOperator.or_(one, one)
# OneBit(1)
  • AND
LogicalOperator.and_(zero, zero)
# ZeroBit(0)
LogicalOperator.and_(zero, one)
# ZeroBit(0)
LogicalOperator.and_(one, zero)
# ZeroBit(0)
LogicalOperator.and_(one, one)
# OneBit(1)

There is also support to others Logical Gates:
nor_, nand_, xor_andxnor_`

Bytes

There is also a Bytes class.

from bitjoy.dtypes import Bytes

Passing a list of Bit to the Bytes' constructor to creating a bytes instance. NOTE that bytes only have 8 bits. So passing more or less it'll throw an error.

bits = [Bit(0), Bit(0), Bit(0), Bit(0), Bit(1), Bit(1), Bit(0), Bit(0)]
b = Bytes(bits)
# Bytes(0, 0, 0, 0, 1, 1, 0, 0)

To creating a easy bytes, use int_to_bytes function from utils to help.

from bitjoy.utils import int_to_bytes

b1 = int_to_bytes(10)
# Bytes(0, 0, 0, 0, 1, 0, 1, 0)
b2 = int_to_bytes(175)
# Bytes(1, 0, 1, 0, 1, 1, 1, 1)
  • Adding Bytes Bytes class also has support to + operand with __add__ method.
b1_b2 = b1 + b2
# Bytes(1, 0, 1, 1, 1, 0, 0, 1)
  • Casting Bytes supports casting with other number types: int, bin, oct and hex.
int(b1_b2)
# 185

bin(b1_b2)
# '0b10111001'

oct(b1_b2)
# '0o271'

hex(b1_b2)
# '0xb9'

Adders

For arithmetic operators, the class Adder has two method to make additions:
half: to half-adder
full: to full-adder

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

bitjoy-1.1b.tar.gz (4.4 kB view details)

Uploaded Source

File details

Details for the file bitjoy-1.1b.tar.gz.

File metadata

  • Download URL: bitjoy-1.1b.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0.post20200127 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

File hashes

Hashes for bitjoy-1.1b.tar.gz
Algorithm Hash digest
SHA256 16a71f552c20ed4ccb3b5ffca9b486c284e30e73247b492bad2ae3e78decdae3
MD5 cdcf970ab2a806cdd42b39092f5095ed
BLAKE2b-256 1645428a20fed4e40995e40fbb9105268d9ed250b121cc227ab8685e59ffc989

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