Advertisement
Korotkodul

20-21_N2_v3

Mar 27th, 2023
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. def dig(l):
  2.     if ord(l) >= ord('A') and ord(l) <= ord('F'):
  3.         return 10 + ord(l) - ord('A')
  4.     return ord(l) - ord('0')
  5.  
  6. def from_16(s):
  7.     ar = []
  8.     for i in s:
  9.         ar.append(dig(i))
  10.     ar = ar[::-1]
  11.     tot = 0
  12.     for i in range(len(s)):
  13.         tot += (16**i) * ar[i]
  14.     return tot
  15.  
  16.  
  17. def from_2(s):
  18.     ar = []
  19.     for i in s:
  20.         ar.append(dig(i))
  21.     ar = ar[::-1]
  22.     tot = 0
  23.     for i in range(len(s)):
  24.         tot += (2**i) * ar[i]
  25.     return tot
  26.  
  27.  
  28. def xor(a, b):
  29.     return a ^ b
  30.  
  31. def shift(x):
  32.     #print(bin(x))
  33.     #print("raw len = ", len(bin(x)))
  34.     s = bin(x)[2:]
  35.     #print("len = ", len(s))
  36.     #print("s = ")
  37.     #print(s)
  38.     s1 = s[len(s) - 1]
  39.     s2 = s[:len(s) - 1]
  40.     t = s1 + s2
  41.     #print("s1")
  42.     #print(s1)
  43.     #print("s2")
  44.     #print(s2)
  45.     #print("t")
  46.     #print(t)
  47.     res = int(t, 2)
  48.     #print("check res")
  49.     #print(res, from_2(t))
  50.     #print("t len = ", len(t))
  51.     #print()
  52.     #print()
  53.     return res
  54.  
  55. def K_shift(x, K):
  56.     for i in range(K):
  57.         x = shift(x)
  58.     return x
  59.  
  60. def f(a, b, K):
  61.    
  62.    
  63.     res =  xor(a,b)
  64.     res = K_shift(res, K)
  65.     return res
  66.  
  67.  
  68. a = 'AB00'
  69. a = from_16(a)
  70. b = '1234'
  71. b = from_16(b)
  72.  
  73. print("a b = ", a, b)
  74. print(10 * 16**3 + 11 * 16**2)
  75. print(16**3 + 2*16**2 + 3*16 + 4)
  76.  
  77. bina = bin(a)[2:]
  78. binb = bin(b)[2:]
  79. print(bina)
  80. for i in range(20):
  81.     a = shift(a)
  82.     bina = bin(a)[2:]
  83.     print(bina)
  84.  
  85. """
  86. K = 0
  87. must = from_16('5C9A')
  88. print(f(a, b, K), must)
  89. """
  90.  
  91.  
  92. """
  93. a = '1A2B'
  94. a = from_16(a)
  95. b = 'FFFF'
  96. b = from_16(b)
  97. K = 4
  98. must = from_16('4E5D')
  99. print("a b = ", from_16(a), from_16(b))
  100. print(16**3 + 10*16**2 + 2*16 + 11)
  101. print(15 * (16**3 + 16**2 + 16 + 1))
  102.  
  103. print(f(a,b, K), must)
  104. """
  105.  
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement