[mus422] powers of two

Craig Sapp craigsapp at gmail.com
Wed Jan 20 17:13:09 PST 2010


Hello Music 422 Class,

When you implement the quantization algorithms for numbers which are
raised from the 2 base, I would recommend using the left shift operator.
For example, the first step in the Floating-point quantizer algorithm:

"I. Quantize the number as an R-bit code where R=2**Rs-1+Rm"

would be more efficiently implemented as:
     R = (1 << Rs) - 1 + Rm
Rather than:
     R = 2**Rs - 1 + Rm
or
     R = pow(2, Rs) - 1 + Rm

The parenthesis around (1 << Rs) are required, because that operator
has a lower precedence than the minus sign.

Comparing the two methods for 10,000,000 iterations, it seems that
the shift method is 15% faster (which isn't all that great; there's
usually a larger difference in C):


###########################################
import time

iterations = 10 ** 7
time1 = time.time()
n = 0
while n < iterations:
   n = n + 1
   R = 2**Rs - 1 + Rm

time2 = time.time()
print("pow method", time2 - time1)

time1 = time.time()
n = 0
while n < iterations:
   n = n + 1
   R = (1 << Rs) - 1 + Rm

time2 = time.time()
print("shift method", time2 - time1)

###########################################


results for my computer:
    ('pow method', 9.0244870185852051)
    ('shift method', 8.437330961227417)
another run:
    ('pow method', 8.9181380271911621)
    ('shift method', 8.3476979732513428)


-=+Craig


Also, here is a list useful python operators for bits:


====================================================================
Python bit operator notes

Important ones:

Python Operator:        <<
Name:   Left-shift operator
C language equivalent:  <<     (same)
Example:
    1 << 3   gives 8, or in base-2: 1 --> 1000
    5 << 4   gives 80, or in base-2: 101 --> 1010000
    Equivalent to multiplying the number on the left by a power
    of two using the number on the right as the power. Example:
       1 << 3  ==  1 * 2**3

Python Operator:        >>
Name:   Right-shift operator
C language equivalent:  >>     (same)
Example:
    1 >> 3   gives 0 because the 1 bits drops off the small end of the number
    6 >> 1   gives 3, or in base-2: 110 --> 11


Python Operator:        |
Name:   Bitwise Or
C language equivalent:  |     (same)
Example:
    5 | 2    gives 7: in base-2:   101 | 010 = 111
    0 | 67   gives 67


Python Operator:        &
Name:   Bitwise And
C language equivalent:  &     (same)
Example:
    5 & 2    gives 0: in base-2:   101 | 010 = 000
    0 & 67   gives 0


Python Operator:        ~
Name: Bitwise Not
C language equivalent:  !     (not the same)
Example:
   ~0           gives -1.
      On 32-bit systems:
           00000000000000000000000000000000
           11111111111111111111111111111111

Less common bit-wise operators:

Python Operator:        ^
Name: Bitwise Exclusive Or
C language equivalent:  ^     (same)
Example:
    5 ^ 2     gives 7; in base-2:    101 ^ 010  = 111
    7 ^ 2     gives 5; in base-2:    111 ^ 010  = 101

=============================================================

Hexadecimal numbers note

Hexadecimal numbers in Python are similar to C syntax.  You place a "0x"
in front of the number to indicate that it is in base-16:
     0x80
is the hexadecimal value for 128. Example python code:

>>>  value = 0x7f

if you print the contents of value, it will automatically print
in decimal format:

>>>  value
127

If you want to see the hexadecimal value for a number, then
you could type:

>>> hex(value)
'0x7f'

Hexadecimal values are traditionally used by computer scientists when
working with binary digits.  The conversion from base-2 to base-16
can be done in your head (with a little memorization and/or some quick
addition).  Consider the binary sequence:
     111000110101001101
Break it into four-digit groupings (starting from the lsb) -- like
commas separate
1,000s in English:
     11 1000 1101 0100 1101
Then substitute a hex digits for each 4-bit grouping (as shown in
the slides from class last Friday):

0000 = 0         1000 = 8
0001 = 1         1001 = 9
0010 = 2         1010 = A
0011 = 3         1011 = B
0100 = 4         1100 = C
0101 = 5         1101 = D
0110 = 6         1110 = E
0111 = 7         1111 = F

So in hex the binary number 111000110101001101 is:
       0x38D4D
or in decimal 232781



More information about the 422 mailing list