473,320 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
################################################## ##########################
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+cLhE64NQnCzgYLQO6iKYufg Cfa8nl
3Y2jbiBhXOWtc8CUb/cGzpk=
=kxv0
-----END PGP SIGNATURE-----

Jul 18 '05 #1
3 8727
Tonguç Yumruk <tr*****@ttnet.net.tr> wrote in message news:<ma**********************************@python. org>...
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
Jul 18 '05 #2
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
Jul 18 '05 #3
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
Sc***********@Acm.Org

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrey Brozhko | last post by:
Hi I need to represent custom type system (ints, bytes, chars, enums, bitmasks, arrays and some other types) in xml. It is easy to see how to represent enums in xml (using xs:enumeration), the...
10
by: Mantorok Redgormor | last post by:
I always see posts that involve the representation of integers, where some poster claims that the unerlyding representation of an integer doesn't have to reflect on the actual integer, for example:...
5
by: Mantorok Redgormor | last post by:
Is it possible to display the underlying representation of integers? Not just integers but also float/double and is it also possible to display the padding bits of signed integer types in standard...
5
by: Andy | last post by:
Sigh... working on it for whole day and got no idea... Basically I want to have a bitmask filtering function, I will throw in 3 parameters: bitMaskValue - current bitmask value filterValue -...
9
by: johan.tibell | last post by:
I'm in the process of writing an interpreter for lambda calculus (i.e. a small functional programming language) in C. I've previously written one in Haskell so I understand at least some of the...
26
by: Carramba | last post by:
Hi! How can I output value of char or int in binary form with printf(); ? thanx in advance
6
by: Army1987 | last post by:
Reliable sources (whose names I'm not allowed to disclose) told me that on the next version of the Deathstation (version 10000, or DS10K) an integral type (they didn't tell which) will have a odd...
3
by: TD | last post by:
Is there a way to DataBind controls to a specific Bit in Bitmask? Here is some sample code of what I am trying to do ... I am trying to bind the Visible and Enabled properties of a Button to...
10
by: lithiumcat | last post by:
Hi, This question seems to come up quite often, however I haven't managed to find an answer relevant to my case. I often use binary flags, and I have alaways used a "bitmask" technique, that...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.