Little explanation

A

Angelo Secchi

Hi,
few days ago I had a problem of converting floating from an IBM/370
system to the actual standard. Thanks to a couple of you (Anton and
Howard) I got the code to solve the problem that as expected works fine.
I (a newbie not only with Python but also with programming) tried to
understand the code by myself but finally I decided to bother the list
again for a couple of explanations. First here the code I'm referring
to:

def ibm370tofloat(fourbytes):
i = struct.unpack('>I',fourbytes)[0]
sign = [1,-1][bool(i & 0x100000000L)]
characteristic = ((i >> 24) & 0x7f) - 64
fraction = (i & 0xffffff)/float(0x1000000L)
return sign*16**characteristic*fraction

What is difficult to understand for me is the meaning of the bitwise
operator &. In particular what do expressions like

i & 0x100000000L

(i >> 24) & 0x7f

i & 0xffffff

mean? I know what the objects involved are, I found what 0x stands for
and I read the tutorial about Numerical Literals but it is not very
clear to me the underlying logic of this three lines of code. Can anyone
give me some hints? Also a suggestion where I can find the answers
by myself would be very helpful.

Angelo




--
========================================================
Angelo Secchi PGP Key ID:EA280337
========================================================
Current Position:
Graduate Fellow Scuola Superiore S.Anna
Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
ph.: +39 050 883365
email: (e-mail address removed) www.sssup.it/~secchi/
========================================================
 
B

Ben Caradoc-Davies

What is difficult to understand for me is the meaning of the bitwise
operator &. In particular what do expressions like
i & 0x100000000L
(i >> 24) & 0x7f
i & 0xffffff
mean?

This syntax is taken from directly from the C programming language. Consult
"ANSI C", second edition, by Kernighan and Ritchie, or see the Python Language
Reference:

http://www.python.org/doc/current/ref/bitwise.html
http://www.python.org/doc/current/ref/shifting.html

"&" is "bitwise and". ">>" is "bitshift right". Bitwise boolean operators
operate on each bit of the operands, rather than their values as a whole. "&"
allows you to select only those bits which are set in both operands. ">>"
allows you to move your bits around so that you can interpret your bits as a
small integer (selecting a few bits from within a word or longer).

Write the *binary* representation of these numbers and try it yourself. Much
easier than trying to understand it in hexadecimal.
 
S

Scott David Daniels

Angelo said:
Hi,
few days ago I had a problem of converting floating from an IBM/370
system to the actual standard. Thanks to a couple of you (Anton and
Howard) I got the code to solve the problem that as expected works fine.
I (a newbie not only with Python but also with programming) tried to
understand the code by myself but finally I decided to bother the list
again for a couple of explanations. First here the code I'm referring
to:

def ibm370tofloat(fourbytes):
i = struct.unpack('>I',fourbytes)[0]
sign = [1,-1][bool(i & 0x100000000L)]
characteristic = ((i >> 24) & 0x7f) - 64
fraction = (i & 0xffffff)/float(0x1000000L)
return sign*16**characteristic*fraction

This could also have been done as:

def ibm370(fourbytes):
first = ord(fourbytes[0])
fraction = struct.unpack('>I', chr(0)+fourbytes[1:]) * 2. ** -24
if first > 127: fraction = -fraction #or sign =[1,-1][first>127]
characteristic = (first & 0x7F) - 64
return fraction * 16 ** characteristic

Clearer?
 

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

Similar Threads


Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top