Little explanation

Angelo Secchi secchi at sssup.it
Tue Mar 2 05:52:49 EST 2004


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: secchi at sssup.it	www.sssup.it/~secchi/
========================================================




More information about the Python-list mailing list