Bitmask representation of integers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tonguç Yumruk

    Bitmask representation of integers

    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
    ############### ############### ############### ############### ############### #[color=blue]
    > I've hacked the Xaw3d library to give you a Win95 like interface and it
    > is named Xaw95. You can replace your Xaw3d library.[/color]
    Oh God, this is so disgusting!
    -- seen on c.o.l.developme nt.apps, about the "Win95 look-alike"
    ############### ############### ############### ############### ############### #
    Tonguç Yumruk

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

    iD8DBQE/g7FE1xWu4MLSyoY RAtrkAJ0XHQ+cLh E64NQnCzgYLQO6i KYufgCfa8nl
    3Y2jbiBhXOWtc8C Ub/cGzpk=
    =kxv0
    -----END PGP SIGNATURE-----

  • Peter Abel

    #2
    Re: Bitmask representation of integers

    Tonguç Yumruk <trooper@ttnet. net.tr> wrote in message news:<mailman.1 065595274.17690 .python-list@python.org >...[color=blue]
    > 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
    > ############### ############### ############### ############### ############### #[color=green]
    > > I've hacked the Xaw3d library to give you a Win95 like interface and it
    > > is named Xaw95. You can replace your Xaw3d library.[/color]
    > Oh God, this is so disgusting!
    > -- seen on c.o.l.developme nt.apps, about the "Win95 look-alike"
    > ############### ############### ############### ############### ############### #
    > Tongu Yumruk
    >
    > --[/color]
    [color=blue][color=green][color=darkred]
    >>> a=255
    >>> b=0xff
    >>> c=int('FF',16)
    >>> d=int('11111111 ',2)
    >>> a,b,c,d[/color][/color][/color]
    (255, 255, 255, 255)[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Regards
    Peter

    Comment

    • Miki Tebeka

      #3
      Re: Bitmask representation of integers

      Hello Tonguç,
      [color=blue]
      > 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.[/color]
      Just use strings.[color=blue][color=green][color=darkred]
      >>> int("101", 2)[/color][/color][/color]
      5[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      HTH.
      Miki

      Comment

      • Scott David Daniels

        #4
        Re: Bitmask representation of integers

        Tonguç Yumruk wrote:[color=blue]
        > 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.
        >[/color]
        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(firstdigi tlen, len(text), 3)]
        digits.insert(0 , _decoder[text[:firstdigitlen]])
        return int(''.join(dig its), 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.Daniels@A cm.Org

        Comment

        Working...