Skip to main content

bitn.BitBin

  • Super fast bitslicing.
  • bitshift speed without bitwise complexity.

bitn.NBin (New!)

  • the reverse bitn.BitBin
  • Encodes values to bytes

Install

pip install bitn

Help(BitBin)

NAME
    bitn

CLASSES
    builtins.object
        BitBin
        NBin

    class BitBin(builtins.object)
     |  bitn.Bitbin takes a byte string and
     |  converts it to a integer, a very large integer
     |  if needed. A 1500 bit integer is no problem.
     |  several methods are available for slicing off bits.
     |  
     |  Methods defined here:
     |  
     |  __init__(self, bites)
     |  
     |  as90k(self, num_bits)
     |      Returns num_bits
     |      of bits as 90k time
     |  
     |  asascii(self, num_bits)
     |      Returns num_bits
     |      as bytes decoded to ascii
     |  
     |  asflag(self, num_bits=1)
     |      Returns one bit as True or False
     |  
     |  ashex(self, num_bits)
     |      Returns the hex value
     |      of num_bits of bits
     |  
     |  asint(self, num_bits)
     |      Starting at self.idx of self.bits,
     |      slice off num_bits of bits.
     |  
     |  forward(self, num_bits)
     |      Advances the start point
     |      forward by num_bits
     |  
     |  negative_shift(self, num_bits)
     |      negative_shift is called instead of
     |      throwing a negative shift count error.
     |  
     |  ----------------------------------------------------------------------


    class NBin(builtins.object)
     |  bitn.NBin is
     |  the reverse BitBin.
     |  Encodes data to integers
     |  and then bytes
     |  
     |  Methods defined here:
     |  
     |  __init__(self)
     |  
     |  add_90k(self, pts, bit_len=33)
         |      Converts 90k  float timestamps
     |      to an int and appends it to nbits
     |      via self.add_int
     |  
     |  add_flag(self, flg)
     |      add_flag takes a boolean
     |      value and adds it as an integer
     |      to self.nbits via self.add_int
     |  
     |  add_hex(self, hex_str, bit_len)
     |      add_hex converts a
     |      hex encoded string to an int
     |      and appends it to self.nbits
     |      via self.add_int
     |  
     |  add_int(self, int_bits, bit_len)
     |      left shift nbits and append new_bits
     |  
     |  forward(self, num)
     |      Currently just an alias to reserve
     |  
     |  nbits2bites(self)
     |      nbits2bites converts
     |      the int self.nbits to bytes as self.bites
     |      and sets self.nbits  and self.idx to 0
     |  
     |  reserve(self, num)
     |      reserve sets 'num'  bits to 1
     |      and appends them to self.nbits
     |      via self.add_int
     |  
     |  ----------------------------------------------------------------------


I used to do this

   from struct import unpack

    sync,two_bytes,one_byte = unpack('>BHB', packet[:4])
    tei = two_bytes >> 15 
    pusi = two_bytes >> 14 & 0x1
    ts_priority = two_bytes >>13 & 0x1
    pid = two_bytes & 0x1fff
    scramble = one_byte >>6
    afc = (one_byte & 48) >> 4
    count = one_byte & 15

Now I use bitn and do this

    from bitn import BitBin

    header= BitBin(packet[:4])
    sync=header.asint(8)
    tei=header.asflag(1)
    pusi=header.asflag(1)
    ts_priority=header.asflag(1)
    pid=header.asint(13)
    scramble=header.asint(2)
    afc=header.asint(2)
    count=header.asint(4)

Example Usage

  • Parse a SCTE 35 splice information section from a hex encoded string
>>> from bitn import BitBin   


>>> bites= bytes.fromhex('FC302F000000000000FFFFF00506FEAEF17C4C0019021743554549480000077F9F0808000000002CA56C97110000C4876A2E')

>>> class Splice_Info_Section:

        def __init__(self,bitbin):
            self.table_id =bitbin.ashex(8)
            self.section_syntax_indicator = bitbin.asflag(1)
            self.private = bitbin.asflag(1)
            self.reserved=bitbin.asint(2)
            self.section_length = bitbin.asint(12)
            self.protocol_version = bitbin.asint(8)
            self.encrypted_packet =  bitbin.asflag(1)
            self.encryption_algorithm =bitbin.asint(6)
            self.pts_adjustment = bitbin.as90k(33)
            self.cw_index = bitbin.ashex(8)
            self.tier = bitbin.ashex(12)
            self.splice_command_length = bitbin.asint(12)
            self.splice_command_type = bitbin.asint(8)


>>> bitbin=BitBin(bites)

>>> sps=Splice_Info_Section(bitbin)

>>> vars(sps)

{'table_id': '0xfc', 'section_syntax_indicator': False, 'private': False, 'reserved': 3, 'section_length': 47,
'protocol_version': 0, 'encrypted_packet': False, 'encryption_algorithm': 0, 'pts_adjustment': '0.000000', 
'cw_index': '0xff', 'tier': '0xfff', 'splice_command_length': 5, 'splice_command_type': 6, 'descriptor_loop_length': 25}

>>> 

Download files

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

Source Distribution

bitn-0.0.45.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

bitn-0.0.45-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file bitn-0.0.45.tar.gz.

File metadata

  • Download URL: bitn-0.0.45.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for bitn-0.0.45.tar.gz
Algorithm Hash digest
SHA256 b9cbe02f82e3e2faadedaa740d27c6cbb49d2e0d7ddf277bae8d170b6a96e83f
MD5 5189808457a438745be8f373e5ca8e4d
BLAKE2b-256 d916e72fe6675b524dfa57e1490e95ea2df77af9ce3744b9ac4aa55b8135cf8b

See more details on using hashes here.

File details

Details for the file bitn-0.0.45-py3-none-any.whl.

File metadata

  • Download URL: bitn-0.0.45-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for bitn-0.0.45-py3-none-any.whl
Algorithm Hash digest
SHA256 5d1f6302a5a1d32e6755714c2c6c888a870858b5d7e8629d09c72607bb35fc93
MD5 d7c7e273ef47e001e2924abfcb465bd0
BLAKE2b-256 d8e344bf2b1868734da2f92547c724c97e332a0f82f3849094f2cf5ac5a8b7fc

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.45 This release

2 files

0.0.43

2 files

0.0.41

2 files

0.0.39

2 files

0.0.37

2 files

0.0.35

2 files

0.0.33

2 files

0.0.31

2 files

0.0.29

2 files

0.0.27

2 files

0.0.25

2 files

0.0.23

2 files

0.0.21

2 files

0.0.19

2 files

0.0.17

2 files

0.0.15

2 files

0.0.13

2 files

0.0.11

2 files

0.0.5

2 files

0.0.3

2 files

Supported by

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