Bitmask representation of integers

  • Thread starter =?iso-8859-9?Q?Tongu=E7?= Yumruk
  • Start date
?

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

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-----
 
P

Peter Abel

Tonguç Yumruk said:
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
############################################################################
Oh God, this is so disgusting!
-- seen on c.o.l.development.apps, about the "Win95 look-alike"
############################################################################
Tongu Yumruk

--

Regards
Peter
 
M

Miki Tebeka

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.

HTH.
Miki
 
S

Scott David Daniels

Tonguç Yumruk said:
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
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top