Bitmask representation of integers

Discussion in 'Python' started by =?iso-8859-9?Q?Tongu=E7?= Yumruk, Oct 8, 2003.

  1. Hi,

    I'm looking for a way to represent bit-level data in python. Something
    like 0xDEADBEEF representation, but not for hex i'm looking for binary.
    Something like the %00110101 representation I used in pascal... Of
    course I can make my operations with ordinary integers but binary
    representation makes the code easier to read.

    --
    Love, Respect, Linux
    ############################################################################
    > I've hacked the Xaw3d library to give you a Win95 like interface and it
    > is named Xaw95. You can replace your Xaw3d library.

    Oh God, this is so disgusting!
    -- seen on c.o.l.development.apps, about the "Win95 look-alike"
    ############################################################################
    Tonguç Yumruk

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (GNU/Linux)

    iD8DBQE/g7FE1xWu4MLSyoYRAtrkAJ0XHQ+cLhE64NQnCzgYLQO6iKYufgCfa8nl
    3Y2jbiBhXOWtc8CUb/cGzpk=
    =kxv0
    -----END PGP SIGNATURE-----
    =?iso-8859-9?Q?Tongu=E7?= Yumruk, Oct 8, 2003
    #1
    1. Advertising

  2. =?iso-8859-9?Q?Tongu=E7?= Yumruk

    Peter Abel Guest

    Tonguç Yumruk <> wrote in message news:<>...
    > Hi,
    >
    > I'm looking for a way to represent bit-level data in python. Something
    > like 0xDEADBEEF representation, but not for hex i'm looking for binary.
    > Something like the %00110101 representation I used in pascal... Of
    > course I can make my operations with ordinary integers but binary
    > representation makes the code easier to read.
    >
    > --
    > Love, Respect, Linux
    > ############################################################################
    > > I've hacked the Xaw3d library to give you a Win95 like interface and it
    > > is named Xaw95. You can replace your Xaw3d library.

    > Oh God, this is so disgusting!
    > -- seen on c.o.l.development.apps, about the "Win95 look-alike"
    > ############################################################################
    > Tongu Yumruk
    >
    > --


    >>> a=255
    >>> b=0xff
    >>> c=int('FF',16)
    >>> d=int('11111111',2)
    >>> a,b,c,d

    (255, 255, 255, 255)
    >>>


    Regards
    Peter
    Peter Abel, Oct 8, 2003
    #2
    1. Advertising

  3. =?iso-8859-9?Q?Tongu=E7?= Yumruk

    Miki Tebeka Guest

    Hello Tonguç,

    > I'm looking for a way to represent bit-level data in python. Something
    > like 0xDEADBEEF representation, but not for hex i'm looking for binary.
    > Something like the %00110101 representation I used in pascal... Of
    > course I can make my operations with ordinary integers but binary
    > representation makes the code easier to read.

    Just use strings.
    >>> int("101", 2)

    5
    >>>


    HTH.
    Miki
    Miki Tebeka, Oct 8, 2003
    #3
  4. Tonguç Yumruk wrote:
    > Hi,
    >
    > I'm looking for a way to represent bit-level data in python. Something
    > like 0xDEADBEEF representation, but not for hex i'm looking for binary.
    > Something like the %00110101 representation I used in pascal... Of
    > course I can make my operations with ordinary integers but binary
    > representation makes the code easier to read.
    >

    octal is a simple starting point:

    _octalchunks = '000', '001', '010', '011', '100', '101', '110', '111'

    _encoder = dict(enumerate(_octalchunks))
    _decoder = dict([(txt, str(n)) for n, txt in _encoder.items()
    + [(0,''), (0,'0'), (1,'1'),
    (0,'00'), (1,'01'), (2,'10'), (3,'11')]])

    def tobinary(number):
    initial = ''.join([_encoder[digit] for digit in oct(number)])
    # but the above certainly has too many leading zeroes, so:
    return initial.lstrip('0') or '0'


    def frombinary(text):
    firstdigitlen = len(text) % 3
    digits = [_decoder[text[n:n+3]]
    for n in range(firstdigitlen, len(text), 3)]
    digits.insert(0, _decoder[text[:firstdigitlen]])
    return int(''.join(digits), 8)

    if __name__ == '__main__':
    tests = [ (0,'0'), (5,'101'), (15,'1111'), (16,'10000')]

    for n,t in tests:
    assert t == tobinary(n)
    assert n == frombinary(t)
    assert n == frombinary('0'+t)
    assert n == frombinary('00'+t)
    assert n == frombinary('000'+t)



    -Scott David Daniels
    Scott David Daniels, Oct 8, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrey Brozhko
    Replies:
    1
    Views:
    1,073
    Andrey Brozhko
    Dec 10, 2004
  2. Mantorok Redgormor

    Representation of integers

    Mantorok Redgormor, Sep 13, 2003, in forum: C Programming
    Replies:
    10
    Views:
    622
    Kevin D. Quitt
    Sep 17, 2003
  3. Mantorok Redgormor

    displaying underlying representation of integers

    Mantorok Redgormor, Sep 26, 2003, in forum: C Programming
    Replies:
    5
    Views:
    345
  4. Mantorok Redgormor

    representation of integers(again) very annoying..

    Mantorok Redgormor, Oct 13, 2003, in forum: C Programming
    Replies:
    4
    Views:
    448
    Barry Schwarz
    Oct 14, 2003
  5. Guest
    Replies:
    3
    Views:
    367
    Cameron Laird
    Sep 9, 2004
Loading...

Share This Page