Skip to main content

array based binary counters with useful functions attached

Project description

BinaryCounters

an Array-based binary counter with functions designed for cellular automata experimentation

how to use:

BinaryCounter(<int>=val,<int>=bitLength)

  • val is the starting value of the binary,

  • bitLength is the size of the array holding the binary.

FUNCTIONS:

  • these are the logical gates - AND,NAND,OR,XOR,XNOR,NOR,NOT.
  • they are included for convinence - and can be used anywhere an 'arbitray function' can be used (they fit the requirments of having the inputs i,bit1,bit2, and give a 0/1 as the output).
  • note: these are not meant for anything other than binary, these are not meant to be called, but to be passed into a method as an argument.

METHODS

KEEP_EVEN(<bool>=increaseBool):

  • increase bool determines if number increases or decreases
  • forces an even number by shifting value by 1

KEEP_ODD(<bool>=increaseBool):

  • increase bool determines if number increases or decreases
  • forces an even number by shifting value by 1

SORT_B(<bool>=fromSig):

  • fromSig if true, sorts all 1's to the most significant digit
  • sorts the binary

SECTION(<int>=start,<int>=stop):

  • returns a new array of that section of binary

SPAWN(<int>=start,<int>=stop):

  • returns a new <BinaryCounter> from that section of binary

COUNT(<bool>=willCountOnes):

  • willCountOnes if true, this function will return number of 1's, otherwise it will return the number of 0's

SHIFT_UP( <int>=amount):

  • logically shifts binary << up

SHIFT_DOWN( <int>=amount):

  • logically shifts binary >> down

SHIFT_CIRCLE_F( <int>=amount):

  • rotates the binary by an amount = 1: (0,0,0,1)=>(1,0,0,0) amount = 2: (0,0,0,1)=>(0,1,0,0)

SHIFT_CIRCLE_R( <int>=amount):

  • rotates the binary by an amount = 1: (1,1,0,1)=>(1,0,1,1) amount = 2: (1,1,1,0)=>(1,0,1,1)

BIT_OP( <BinaryCounter>=binary, <str>=operator):

  • operator options are = "XOR","OR","AND","NAND,"XNOR","NOR","NOT"
  • binary must be the same length as owner of method.

INCREASE(<int>=amount):

  • amount must be a positive integer, or results in no change
  • amount will not increase set bits, it will max at all 1's

DECREASE(<int>=amount):

  • amount must be a positive integer, or results in no change
  • amount will not wrap around in value, it will end at all 0's

READ():

  • prints a status in the console

SET_BIT_LENGTH(<int>=len):

  • len sets the length of the array holding the bits.

B_TO_I(<int>[]=bin):

  • bin is an array of binary numbers (or any numbers), such as <BinaryCounter>.binary
  • returns an int

VAL():

  • returns value of binary

BIN():

  • returns array that represents binary

SET_V(<int>=value):

  • sets value of binary

GET_BIN_OF(<int>=b):

  • b is calculated into an array of zero's and one's that represent it's binary, with a leading significant digit

SET_BIT(<int>=val,<int>=pos)

  • val is 1/0,
  • pos is goes from least significant digit to most
  • pos is 0 based

W_BIT_OP(<BinaryCounter>=binary, <Function>=fn):

  • WRAPPED BIT OPERATION: the second array is looped over if it is smaller than the method's owner
  • binary may be a different length from the owner of this method
  • this method iterates though and modifys its binary by running a fn on it, and the the matching index of the binary
  • note that this wraps around the binary you supply, so it may be of any length.

S_BIT_OP(<BinaryCounter>=binary, <Function>=fn):

  • STATIONARY BIT OPERATION: there is no wrap lke in W_BIT_OP, if the binary you pass in has a smaller bitLength, S_BIT_OP will operate from smallest significant digit to largest, and return the original binary when it runs of of the binary you pass in.
  • binary may be a different length from the owner of this method
  • this method iterates though and modifys its binary by running a fn on it, and the the matching index of the binary
  • note that this wraps around the binary you supply, so it may be of any length.
  • notes on fn
    • it is a function you supply
    • it must return 0 or 1
    • it's inputs are: (<int>=pos, <int>=bit1, <int>=bit2)
      • pos begins at the least significant digit and move to the most significant digit in the array.
      • bit1 is the method owner's bit
      • bit2 is the comparison bit. derived from pos % the length of the comparison array

example usage:

 BC1 = BinaryCounter(0,10)  
 BC2 = BinaryCounter(10,10)
 print(BC1.val()) #[0,0,0,0,0,0,0,0,0,0]  left if most significant digit, right is least
 print(BC2.val()) #[0,0,0,0,0,0,1,0,1,0]
 BC1.increase(1) 
 BC2.increase(386) 
 print(BC1.val()) #[0,0,0,0,0,0,0,0,0,1]
 print(BC2.val()) #[0,1,1,0,0,0,1,1,0,0]
 BC1.BIT_OP(BC2,"OR")
 print(BC1.val()) #[0,1,1,0,0,0,1,1,0,1]
 print(BC2.val()) #[0,1,1,0,0,0,1,1,0,0]
 def randomFn(index,bit,b):#just a random equation that spits out 0,1
      return ((bit+b)*(index*b+1))%2  
 BC3 = BinaryCounter(15,5) #[0,1,1,1,1]
 BC1.R_BIT_OP(BC3,randomFn) 
 print(BC1.val()) #[1,0,0,0,0,0,0,0,1,1]

 #to have a smaller array act on a larger:
 #use W_BIT_OP and pass in the logical function
 #give it the binary (any array of bits)
 BC1.W_BIT_OP(BC3, OR) # OR these arrays together, and since BC3 is smaller, loop over until you cover all elements of BC1    
 #use S_BIT_OP to 
 BC1.S_BIT_OP(BC3, OR) # OR these arrays together, and since BC3 is smaller, so it returns BC1 partially unmodified    

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

BinaryCounters-0.0.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

BinaryCounters-0.0.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: BinaryCounters-0.0.1.tar.gz
  • Upload date:
  • Size: 6.5 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.6.4

File hashes

Hashes for BinaryCounters-0.0.1.tar.gz
Algorithm Hash digest
SHA256 8796abb137eb333fb2ad6af5fdf4c0f6cfbc587ff34b11dfe4e25689e11a937a
MD5 6b629edf878027c378157bec3c33a8e3
BLAKE2b-256 b64b199b87c9e527017d8c921e98a185cffd1e52cbdb1ed6db7a423c39d16806

See more details on using hashes here.

File details

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

File metadata

  • Download URL: BinaryCounters-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.0 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.6.4

File hashes

Hashes for BinaryCounters-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d902bd862cc45f1231186910258bc71a19499ee3d7c4e69ba3816542ed752136
MD5 9e79cb919b3f7e08952a429654c3735d
BLAKE2b-256 de6543caf72e8cfd2f41761612b5cdd25a55351a0fcc77dcb3782a078e947f37

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