Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def dig(l):
- if ord(l) >= ord('A') and ord(l) <= ord('F'):
- return 10 + ord(l) - ord('A')
- return ord(l) - ord('0')
- def from_16(s):
- ar = []
- for i in s:
- ar.append(dig(i))
- ar = ar[::-1]
- tot = 0
- for i in range(len(s)):
- tot += (16**i) * ar[i]
- return tot
- def from_2(s):
- ar = []
- for i in s:
- ar.append(dig(i))
- ar = ar[::-1]
- tot = 0
- for i in range(len(s)):
- tot += (2**i) * ar[i]
- return tot
- def xor(a, b):
- return a ^ b
- def shift(x):
- #print(bin(x))
- #print("raw len = ", len(bin(x)))
- s = bin(x)[2:]
- #print("len = ", len(s))
- #print("s = ")
- #print(s)
- s1 = s[len(s) - 1]
- s2 = s[:len(s) - 1]
- t = s1 + s2
- #print("s1")
- #print(s1)
- #print("s2")
- #print(s2)
- #print("t")
- #print(t)
- res = int(t, 2)
- #print("check res")
- #print(res, from_2(t))
- #print("t len = ", len(t))
- #print()
- #print()
- return res
- def K_shift(x, K):
- for i in range(K):
- x = shift(x)
- return x
- def f(a, b, K):
- res = xor(a,b)
- res = K_shift(res, K)
- return res
- a = 'AB00'
- a = from_16(a)
- b = '1234'
- b = from_16(b)
- print("a b = ", a, b)
- print(10 * 16**3 + 11 * 16**2)
- print(16**3 + 2*16**2 + 3*16 + 4)
- bina = bin(a)[2:]
- binb = bin(b)[2:]
- print(bina)
- for i in range(20):
- a = shift(a)
- bina = bin(a)[2:]
- print(bina)
- """
- K = 0
- must = from_16('5C9A')
- print(f(a, b, K), must)
- """
- """
- a = '1A2B'
- a = from_16(a)
- b = 'FFFF'
- b = from_16(b)
- K = 4
- must = from_16('4E5D')
- print("a b = ", from_16(a), from_16(b))
- print(16**3 + 10*16**2 + 2*16 + 11)
- print(15 * (16**3 + 16**2 + 16 + 1))
- print(f(a,b, K), must)
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement