count_bits.py 648 B

123456789101112131415161718192021222324
  1. #!/usr/local/pypy-1.6/bin/pypy
  2. import sys
  3. total_bits = 0
  4. bits_set = 0
  5. while True:
  6. block = sys.stdin.read(2 ** 19)
  7. if not block:
  8. break
  9. total_bits += len(block) * 8
  10. # print('got block of length %d' % len(block))
  11. for char in block:
  12. byte = ord(char)
  13. # print('got char %d' % byte)
  14. for exponent in range(8):
  15. bitmask = 2 ** exponent
  16. # print('checking mask %d' % bitmask)
  17. if byte & bitmask != 0:
  18. # print('adding 1 to count')
  19. bits_set += 1
  20. print('%s set, %s present, %6.2f%%' % (bits_set, total_bits, bits_set * 100.0 / total_bits))