Python Exercise
Research the arange
function from the Numpy library and the list
and map
functions. On the Python command line, code a procedure to convert an 8-bit binary string to a base-10 integer. Hint: With arange
, generate a Numpy array that contains the powers of 2 in their correct positions. In other words, the array will be [7, 6, 5, 4, 3, 2, 1, 0]. Use list
and map
together to obtain an array or list of the integers 0 and 1 that can be used in vector computations. For example, if the binary string representation is ‘00110101’, use list
and map
to obtain the list (or array) [0, 0, 1, 1, 0, 1, 0, 1]. Then, using vector multiplication and exponentiation, multiply that list by powers of 2 obtained from the position array ([7, 6, 5, …]).
Vector operations in Python are quite easy. For instance, if [7, 6, 5, …] is a Numpy array, and [0, 0, 1, 1, 0, 1, 0, 1] is a list, then:
There are multiple correct approaches to solve this problem.
>>> b = '00110101'
>>> len(b)
8
>>> blist = list(b)
>>> blist
['0', '0', '1', '1', '0', '1', '0', '1']
>>> posn = [7,6,5,4,3,2,1,0]
>>> posn = np.arange(7,0,step=-1)
>>> posn
array([7, 6, 5, 4, 3, 2, 1])
>>> posn = np.arange(8,0,step=-1) - 1
>>> posn
array([7, 6, 5, 4, 3, 2, 1, 0])
>>> bb = blist == '1'
>>> bb
False
>>> blist0 = list(map(int, blist))
>>> blist0
[0, 0, 1, 1, 0, 1, 0, 1]
>>> blist0 * posn
array([0, 0, 5, 4, 0, 2, 0, 0])
>>> blist0 * 2**posn
array([ 0, 0, 32, 16, 0, 4, 0, 1])
>>> sum(blist0 * 2**posn)
53
>>>
Floating point numbers, or non-integers, as well as negative numbers, can be expressed in a variety of ways. There are various standards that are used by microprocessor manufacturers to represent these non-integral values. This topic is beyond the scope of the current discussion. However, a short discussion on representing fractions is needed. Just as bits (having values 0 or 1) are used for integers, bits to the right of the decimal point (i.e. the fractional part) represent fractional powers of 2. For instance, 21 = 2, 20 = 1, and 2-1 = 1/21 = ½. Similarly. 2-2 = 1/22 = 1/4, 2-3 = 1/23 = 1/8, 24 = 1/24 = 1/16, etc. Consequently,
0.12 (0.1 in binary) = 2-1 = 1/21 = ½
0.012 = 2-2 = 1/22 = 1/4,
0.0012 = 2-3 = 1/23 = 1/8
0.00012 = 24 = 1/24 = 1/16, etc.
As was the case with integers, one can determine the base-10 representation of a fraction through sums of various powers of 2. For example,
0.1012 = (1 x 2-1) + (0 x 2-2) + (1 x 2-3) = ½ + 0 + 1/8 = 5/8 = 0.625
0.00112 = (0 x 2-1) + (0 x 2-2) + (1 x 2-3) + (1 x 2-4) = 0 + 0 + 1/8 + 1/16 = 3/16 = 0.1875
The same procedure can be applied if the number has an integer part (i.e., mixed fractions). For example,
11011.011012 = (1 x 24) + (1 x 23) + (0 x 22) + (1 x 21) + (1 x 20) + (0 x 2-1) + (1 x 2-2) + (1 x 2-3) + (0 x 2-4) + (0 x 2-5)
= 16 + 8 + 0 + 2 + 1 + 0 + ¼ + 1/8 + 0 + 1/32
= 27 + ¼ + 1/8 + 1/32
= 27 13/32
= 27.40625
The reader may have observed that although any integer can be expressed perfectly in binary, fractional numbers can only be perfe