Advertisement
Korotkodul

20-21_N2_v2

Mar 27th, 2023 (edited)
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 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 xor(a, b):
  18.     return a ^ b
  19.  
  20. def shift(x):
  21.     s = bin(x)
  22.     t = s[len(s) - 1] + s[:len(s) - 1]
  23.     t = t[3:]
  24.     res = int(t, 2)
  25.     return res
  26.  
  27. def K_shift(x, K):
  28.     for i in range(K):
  29.         x = shift(x)
  30.     return x
  31.  
  32. def f(a, b, K):
  33.     a = from_16(a)
  34.     b = from_16(b)
  35.     res =  xor(a,b)
  36.     res = K_shift(res, K)
  37.     return res
  38.  
  39.  
  40. a = 'AB00'
  41. b = '1234'
  42.  
  43. print("a b = ", from_16(a), from_16(b))
  44. print(10 * 16**3 + 11 * 16**2)
  45. print(16**3 + 2*16**2 + 3*16 + 4)
  46.  
  47. K = 0
  48. must = from_16('5C9A')
  49. print(f(a, b, K), must)
  50.  
  51.  
  52. """
  53. a = '1A2B'
  54. b = 'FFFF'
  55. K = 4
  56. must = from_16('4E5D')
  57. print("a b = ", from_16(a), from_16(b))
  58. print(16**3 + 10*16**2 + 2*16 + 11)
  59. print(15 * (16**3 + 16**2 + 16 + 1))
  60.  
  61. print(f(a,b, K), must)
  62. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement