Skip to content

Instantly share code, notes, and snippets.

@rafaelnp
Created February 8, 2021 13:41
Show Gist options
  • Save rafaelnp/1ad9d063499907befe36de5789c1b716 to your computer and use it in GitHub Desktop.
Save rafaelnp/1ad9d063499907befe36de5789c1b716 to your computer and use it in GitHub Desktop.
numbers of digits vs powers of 2
"""
numbers of digits vs powers of 2
--------------------------------
plot (and print) in jupyter number of digits used by every power of 2 twom from 0 to 128, e.g.:
2^16 = 65536 (5 digits)
it is useful when converting binary to BCD (Binary Coded Decimal), often used in FPGA/ASIC
(using double-dabble algorithm) development, to convert a number into a human readable format, e.g.:
1. show in a 7-segment display
2. send via uart
3. print on a terminal, etc.
Given a number of digits, mutiply it by four to get the number of bits needed to represent it.
"""
from matplotlib import pyplot as plt
x = []
s = []
for i in range(0,129):
x.append(2**i)
s.append(len(str(x[i])))
#print(i, x[i],s[i])
%matplotlib inline
xaxis = s
yaxis = range(0,129)
fig = plt.figure()
ax = fig.add_axes([0,0,2, 1])
ax.plot(xaxis,yaxis)
ax.set_title("number of digits base on powers of two")
ax.set_xlabel('digits')
ax.set_ylabel('powers of 2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment