Browse Source

Initial checkin. Just counts set bits in a file

dstromberg 13 years ago
parent
commit
e5d0cac903
1 changed files with 23 additions and 0 deletions
  1. 23 0
      count-bits

+ 23 - 0
count-bits

@@ -0,0 +1,23 @@
+#!/usr/bin/python
+
+import sys
+
+set_bits = 0
+
+while True:
+	block = sys.stdin.read(2**16)
+	if not block:
+		break
+	#print('got block of length %d' % len(block))
+	for char in block:
+		byte = ord(char)
+		#print('got char %d' % byte)
+		for exponent in range(8):
+			bitmask = 2**exponent
+			#print('checking mask %d' % bitmask)
+			if byte & bitmask != 0:
+				#print('adding 1 to count')
+				set_bits += 1
+
+print(set_bits)
+