瀏覽代碼

Initial checkin. Just counts set bits in a file

dstromberg 13 年之前
父節點
當前提交
e5d0cac903
共有 1 個文件被更改,包括 23 次插入0 次删除
  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)
+