Little explanation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Angelo Secchi

    Little explanation


    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(f ourbytes):
    i = struct.unpack(' >I',fourbytes )[0]
    sign = [1,-1][bool(i & 0x100000000L)]
    characteristic = ((i >> 24) & 0x7f) - 64
    fraction = (i & 0xffffff)/float(0x1000000 L)
    return sign*16**charac teristic*fracti on

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

  • Ben Caradoc-Davies

    #2
    Re: Little explanation

    On Tue, 2 Mar 2004 11:52:49 +0100, Angelo Secchi <secchi@sssup.i t> wrote:[color=blue]
    > 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?[/color]

    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:

    The official home of the Python Programming Language



    "&" 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.

    --
    Ben Caradoc-Davies <ben@wintersun. org>
    A milestone document in the history of human rights, the Universal Declaration of Human Rights set out, for the first time, fundamental human rights to be universally protected. It has been translated into over 500 languages.

    Imprisonment on arrival is the authentic Australian immigration experience.

    Comment

    • Scott David Daniels

      #3
      Re: Little explanation

      Angelo Secchi wrote:
      [color=blue]
      > 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(f ourbytes):
      > i = struct.unpack(' >I',fourbytes )[0]
      > sign = [1,-1][bool(i & 0x100000000L)]
      > characteristic = ((i >> 24) & 0x7f) - 64
      > fraction = (i & 0xffffff)/float(0x1000000 L)
      > return sign*16**charac teristic*fracti on[/color]

      This could also have been done as:

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

      Clearer?
      --
      -Scott David Daniels
      Scott.Daniels@A cm.Org

      Comment

      Working...