a=0100; print a ; 64 how to reverse this?

mensanator at aol.com mensanator at aol.com
Tue Jul 17 20:25:05 EDT 2007


On Jul 17, 7:40 am, mosi <skawan... at gmail.com> wrote:
> Thank you,
> this is great,
> I thought that this should be standard in python 2.4 or 2.5 or in some
> standard library (math ???)
> Didn`t find anything.

You can also look up the gmpy module (not part of standard library).

It contains a lot of functions for use with binary numbers
(there are many others, I'm just showing the ones used for
binary numbers):

DESCRIPTION
    gmpy 1.04 - General Multiprecision arithmetic for PYthon:
    exposes functionality from the GMP 4 library to Python 2.{2,3,4}.

    Allows creation of multiprecision integer (mpz), float (mpf),
    and rational (mpq) numbers, conversion between them and to/from
    Python numbers/strings, arithmetic, bitwise, and some other
    higher-level mathematical operations; also, pretty good-quality
    linear-congruential random number generation and shuffling.

    mpz has comparable functionality to Python's builtin longs, but
    can be faster for some operations (particularly multiplication
    and raising-to-power) and has many further useful and speedy
    functions (prime testing and generation, factorial, fibonacci,
    binary-coefficients, gcd, lcm, square and other roots, ...).

    mpf and mpq only offer basic arithmetic abilities, but they
    do add the ability to have floating-point numbers ensuring at
    least a predefined number of bits' worth of precision (and with
    potentially-huge or extremely-tiny magnitudes), as well as
    unlimited-precision rationals, with reasonably-fast operations,
    which are not built-in features of Python.

FUNCTIONS

    digits(...)
        digits(x[,base]): returns Python string representing x in the
        given base (2 to 36, default 10 if omitted or 0); leading '-'
        present if x<0, but no leading '+' if x>=0. x must be an mpz,
        or else gets coerced into one.

    getbit(...)
        getbit(x,n): returns 0 or 1, the bit-value of bit n of x;
        n must be an ordinary Python int, >=0; x is an mpz, or else
        gets coerced to one.

    hamdist(...)
        hamdist(x,y): returns the Hamming distance (number of bit-
positions
        where the bits differ) between x and y.  x and y must be mpz,
or else
        get coerced to mpz.

    lowbits(...)
        lowbits(x,n): returns the n lowest bits of x; n must be an
        ordinary Python int, >0; x must be an mpz, or else gets
        coerced to one.

    numdigits(...)
        numdigits(x[,base]): returns length of string representing x
in
        the given base (2 to 36, default 10 if omitted or 0); the
value
        returned may sometimes be 1 more than necessary; no provision
        for any 'sign' characte, nor leading '0' or '0x' decoration,
        is made in the returned length.  x must be an mpz, or else
gets
        coerced into one.

    popcount(...)
        popcount(x): returns the number of 1-bits set in x; note that
        this is 'infinite' if x<0, and in that case, -1 is returned.
        x must be an mpz, or else gets coerced to one.

    scan0(...)
        scan0(x, n=0): returns the bit-index of the first 0-bit of x
(that
        is at least n); n must be an ordinary Python int, >=0.  If no
more
        0-bits are in x at or above bit-index n (which can only happen
for
        x<0, notionally extended with infinite 1-bits), None is
returned.
        x must be an mpz, or else gets coerced to one.

    scan1(...)
        scan1(x, n=0): returns the bit-index of the first 1-bit of x
(that
        is at least n); n must be an ordinary Python int, >=0.  If no
more
        1-bits are in x at or above bit-index n (which can only happen
for
        x>=0, notionally extended with infinite 0-bits), None is
returned.
        x must be an mpz, or else gets coerced to one.

    setbit(...)
        setbit(x,n,v=1): returns a copy of the value of x, with bit n
set
        to value v; n must be an ordinary Python int, >=0; v, 0 or !
=0;
        x must be an mpz, or else gets coerced to one.




More information about the Python-list mailing list