Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. def count_zeros(s):
  2. if len(s) == 1: return ~s
  3. upper_bits = 1 << ceil_log2(len(s)) - 1
  4. upper = count_zeros(s[-upper_bits:])
  5. lower = count_zeros(s[:-upper_bits])
  6. lower = Cat(lower, C(0, len(upper) - len(lower)))
  7.  
  8. msbs = Cat(lower[-1], upper[-1])
  9. out = Mux(msbs.matches("11"), C(1 << len(upper)),
  10. Mux(msbs.matches("10"), Cat(lower[:-1], C(1, 2)), Cat(upper, C(0))))
  11. return out
  12.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement