-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbits.py
43 lines (35 loc) · 1.01 KB
/
bits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#bits.py @author Kai Barclay 2020
#all of the binary based functions for the program
MAX = 2**64-1
def ones(n): #returns n-bits of 1s
return MAX >> (64-n)
def num(str): #returns a binary string as an int
return int(str, 2)
def numBits(str): #returns number of bits in binary string
return len(str)
def leftRotate(key): #performs a left rotation on given binary string
n = num(key)
b = numBits(key)
x = (n << 1) | (n >> (b-1))
x = x & ones(b)
s = toBits(x, b)
return s
def xor(e, k): #xors and formats given binary strings
a = num(e)
b = num(k)
c = numBits(e)
x = a ^ b
s = toBits(x, c)
return s
def toBits(num, bits): #formats an int as a binary string with bits-bits
s = format(num, 'b')
while len(s) < bits:
s = '0' + s
return s
def toChar(bin): #converts a binary string to its (8-bit) ascii representation
s = ""
while bin != "":
x = bin[0:8]
s = s + chr(int(x, 2))
bin = bin[8:]
return s