Skip to content

Instantly share code, notes, and snippets.

@ellisonbg
Created November 14, 2009 17:17
Show Gist options
  • Save ellisonbg/234643 to your computer and use it in GitHub Desktop.
Save ellisonbg/234643 to your computer and use it in GitHub Desktop.
Functions for reading digits of pi in binary form
"""Functions for reading binary files with the digits of pi stored
as 4-bit ints."""
def bin_file_to_digits(filename, the_type=str):
raw_bytes = np.fromfile(filename, dtype=np.byte)
for b in raw_bytes[0:10000]:
digits = byte_to_digits(b)
yield the_type(digits[0])
yield the_type(digits[1])
def gen_bin(filename, the_type=str, chunksize=10000):
with open(filename, 'rb') as f:
while True:
data = f.read(chunksize)
if not data: break
for b in data:
print repr(b)
digits = byte_to_digits(b)
yield the_type(digits[0])
yield the_type(digits[1])
def byte_to_digits(b):
return ((b & 0xf0) >> 4, (b & 0x0f))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment