Difference of & in perl and php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fritz-bayer@web.de

    Difference of & in perl and php

    Hello,

    I'm porting a program from php to perl and discovered, that the bitwise
    operator & seems to work differently, which I don't understand why.

    I tested it with the values:

    a=5543039447 and b=13

    Then in perl the expressions:

    $result= ($z & $a);

    returns 2147483648.

    But in php the same expression returns zero. Why is that and how can I
    fix it?!

    Fritz

  • d

    #2
    Re: Difference of & in perl and php

    <fritz-bayer@web.de> wrote in message
    news:1141404106 .485472.53880@z 34g2000cwc.goog legroups.com...[color=blue]
    > Hello,
    >
    > I'm porting a program from php to perl and discovered, that the bitwise
    > operator & seems to work differently, which I don't understand why.
    >
    > I tested it with the values:
    >
    > a=5543039447 and b=13
    >
    > Then in perl the expressions:
    >
    > $result= ($z & $a);
    >
    > returns 2147483648.
    >
    > But in php the same expression returns zero. Why is that and how can I
    > fix it?![/color]

    If you have defined $a and $b, then bitwise-anded $z and $a, you *should*
    get zero, as zero AND anything is zero. If, however, you do bitwise-and $a
    and $b, you shouldn't get 2147483648 in the first place, so I don't know
    where your problem is :)
    [color=blue]
    > Fritz
    >[/color]


    Comment

    • fritz-bayer@web.de

      #3
      Re: Difference of &amp; in perl and php


      d wrote:[color=blue]
      > <fritz-bayer@web.de> wrote in message
      > news:1141404106 .485472.53880@z 34g2000cwc.goog legroups.com...[color=green]
      > > Hello,
      > >
      > > I'm porting a program from php to perl and discovered, that the bitwise
      > > operator & seems to work differently, which I don't understand why.
      > >
      > > I tested it with the values:
      > >
      > > a=5543039447 and b=13
      > >
      > > Then in perl the expressions:
      > >
      > > $result= ($z & $a);
      > >
      > > returns 2147483648.
      > >
      > > But in php the same expression returns zero. Why is that and how can I
      > > fix it?![/color]
      >
      > If you have defined $a and $b, then bitwise-anded $z and $a, you *should*
      > get zero, as zero AND anything is zero. If, however, you do bitwise-and $a
      > and $b, you shouldn't get 2147483648 in the first place, so I don't know
      > where your problem is :)
      >[color=green]
      > > Fritz
      > >[/color][/color]

      sorry, i messed up the example. I posted a follow up thread, please
      forget about this one.

      Comment

      • Jim Michaels

        #4
        Re: Difference of &amp; in perl and php


        <fritz-bayer@web.de> wrote in message
        news:1141404106 .485472.53880@z 34g2000cwc.goog legroups.com...[color=blue]
        > Hello,
        >
        > I'm porting a program from php to perl and discovered, that the bitwise
        > operator & seems to work differently, which I don't understand why.
        >
        > I tested it with the values:
        >
        > a=5543039447 and b=13
        >
        > Then in perl the expressions:
        >
        > $result= ($z & $a);
        >
        > returns 2147483648.
        >
        > But in php the same expression returns zero. Why is that and how can I
        > fix it?!
        >
        > Fritz
        >[/color]

        In my perl (ActivePerl 5.8.8.816 in WInXP 32-bit) I get 13 for a result.
        5543039447=0x00 00 0001 4A64 11D7
        it's a 33-bit number, so that may be the cause of your problem right there.
        undefined behavior after the bits get too big.


        Comment

        Working...