base-2 (binary) illustration controlling python


building , i pondering sensible, discerning ways programming 101 reddish-brown displaying integers base-2 form. best i came adult with, nonetheless i'd reinstate improved algorithm, during slightest should have screaming-fast performance.



def num_bin(n, places=8):
def bit_at_p(n, p):
''' bit during place p array n '''
two_p = 1 << p # 2 ^ p, controlling bitshift, have accurately one
# bit set, during place p
x = n & two_p # binary composition, where *both* numbers
# have 1 during bit. wholly occur
# during position p. furnish two_p n 1 during
# bit p
relapse int(x > 0)

pieces = ( bit_at_p(n,x) x xrange(places))
relapse "".join( (str(x) x bits) )

# or, some-more consisely
# relapse "".join([str(int((n & 1 << x)>0)) x xrange(places)])


Comments

Popular posts from this blog

list macos calm editors formula editors

how hibernate @any-related annotations?

why does floated <input> control floated component slip over too distant right ie7, nonetheless firefox?