Different binary results with different PHP versions

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

    #1

    Different binary results with different PHP versions

    I am using Windows XP + PHP 5.1.2 at home for development and using
    Linux + PHP 4.4.2 at server side. In my code, I am doing some bitwise
    operations. While doing this, I have come accross to different results
    from my functions that uses bitwise xor (^) operator. When I digg into
    the code, I have tried to print out the binary of a decimal number. The
    result was so:

    echo decbin(-5817441119);
    // Prints 101001010100000 011100100101000 01 at local (PHP 5.1.2)
    // Prints 100000000000000 000000000000000 00 at server (PHP 4.4.2)

    Is this a bug? How can I workaround this problem? Thanks.

  • Erwin Moller

    #2
    Re: Different binary results with different PHP versions

    Skeleton wrote:
    [color=blue]
    > I am using Windows XP + PHP 5.1.2 at home for development and using
    > Linux + PHP 4.4.2 at server side. In my code, I am doing some bitwise
    > operations. While doing this, I have come accross to different results
    > from my functions that uses bitwise xor (^) operator. When I digg into
    > the code, I have tried to print out the binary of a decimal number. The
    > result was so:
    >
    > echo decbin(-5817441119);
    > // Prints 101001010100000 011100100101000 01 at local (PHP 5.1.2)
    > // Prints 100000000000000 000000000000000 00 at server (PHP 4.4.2)
    >
    > Is this a bug? How can I workaround this problem? Thanks.[/color]

    Hi,

    Did you read the Fine Manual?


    says:
    string decbin ( int number )
    ----------------
    Returns a string containing a binary representation of the given number
    argument. The largest number that can be converted is 4294967295 in decimal
    resulting to a string of 32 1's.
    ----------------

    Your number is bigger, or actually smaller. ;-)

    Read the usercontributed notes for details and solutions.

    Regards,
    Erwin Moller

    Comment

    • Skeleton

      #3
      Re: Different binary results with different PHP versions

      Yes, I have read the manual and notes below the manual. But actually, I
      don't need exactly the decbin function. I have used that just to show
      something going wrong. My point is the ^ operator. For ex, look at the
      following code and its outputs:

      echo (1 ^ -5817441119);
      // Prints -1522473824 which is true result, at PHP 5.1.2 + Windows
      // Prints -2147483647 which is false result, at PHP 4.4.2 + Linux

      I also thought that the reason is that the number is too small, but why
      PHP 5 calculates correctly? Is there a big difference in number types
      between PHP 4 and PHP 5? I don't know if it helps but when I checked
      PHP_INT_MAX, it is 2147483647 for both.

      Erwin Moller wrote:[color=blue]
      > Skeleton wrote:
      >[color=green]
      > > I am using Windows XP + PHP 5.1.2 at home for development and using
      > > Linux + PHP 4.4.2 at server side. In my code, I am doing some bitwise
      > > operations. While doing this, I have come accross to different results
      > > from my functions that uses bitwise xor (^) operator. When I digg into
      > > the code, I have tried to print out the binary of a decimal number. The
      > > result was so:
      > >
      > > echo decbin(-5817441119);
      > > // Prints 101001010100000 011100100101000 01 at local (PHP 5.1.2)
      > > // Prints 100000000000000 000000000000000 00 at server (PHP 4.4.2)
      > >
      > > Is this a bug? How can I workaround this problem? Thanks.[/color]
      >
      > Hi,
      >
      > Did you read the Fine Manual?
      > http://nl2.php.net/manual/en/function.decbin.php
      >
      > says:
      > string decbin ( int number )
      > ----------------
      > Returns a string containing a binary representation of the given number
      > argument. The largest number that can be converted is 4294967295 in decimal
      > resulting to a string of 32 1's.
      > ----------------
      >
      > Your number is bigger, or actually smaller. ;-)
      >
      > Read the usercontributed notes for details and solutions.
      >
      > Regards,
      > Erwin Moller[/color]

      Comment

      • David Haynes

        #4
        Re: Different binary results with different PHP versions

        Skeleton wrote:[color=blue]
        > Yes, I have read the manual and notes below the manual. But actually, I
        > don't need exactly the decbin function. I have used that just to show
        > something going wrong. My point is the ^ operator. For ex, look at the
        > following code and its outputs:
        >
        > echo (1 ^ -5817441119);
        > // Prints -1522473824 which is true result, at PHP 5.1.2 + Windows
        > // Prints -2147483647 which is false result, at PHP 4.4.2 + Linux
        >
        > I also thought that the reason is that the number is too small, but why
        > PHP 5 calculates correctly? Is there a big difference in number types
        > between PHP 4 and PHP 5? I don't know if it helps but when I checked
        > PHP_INT_MAX, it is 2147483647 for both.[/color]

        Be careful of your base assumptions. php 5.1.2 on Linux produces
        -2147483647 as well.

        -david-

        Comment

        • Skeleton

          #5
          Re: Different binary results with different PHP versions

          My PHP 5.1.2 is on Windows. So the problem is about the Operating
          Systems.

          David Haynes wrote:[color=blue]
          > Be careful of your base assumptions. php 5.1.2 on Linux produces
          > -2147483647 as well.
          >
          > -david-[/color]

          Comment

          • Anonymous

            #6
            Re: Different binary results with different PHP versions

            Skeleton wrote:[color=blue]
            >
            > My PHP 5.1.2 is on Windows. So the problem is about the Operating
            > Systems.
            >[/color]

            No, the problem is that the number is outside of the defined bounds.

            *Anything* could be the result, depending how that particular PHP
            version, operating system or CPU treats overflowing numbers.

            Ex falso quodlibet.

            Bye!

            Comment

            • Chung Leong

              #7
              Re: Different binary results with different PHP versions

              Skeleton wrote:[color=blue]
              > My PHP 5.1.2 is on Windows. So the problem is about the Operating
              > Systems.
              >
              > David Haynes wrote:[color=green]
              > > Be careful of your base assumptions. php 5.1.2 on Linux produces
              > > -2147483647 as well.
              > >
              > > -david-[/color][/color]

              No, it's difference is in the compiler. The operation you describe
              causes a double-to-long-integer conversion overflow. Gcc probably
              throws in a check which sets the value to the integer boundary whereas
              MS Visual C just lets it wrap around.

              Comment

              • Skeleton

                #8
                Re: Different binary results with different PHP versions

                So what should I do at this point? I need to make XOR operations with
                such big(small) numbers.
                Chung Leong wrote:[color=blue]
                > Skeleton wrote:[color=green]
                > > My PHP 5.1.2 is on Windows. So the problem is about the Operating
                > > Systems.
                > >
                > > David Haynes wrote:[color=darkred]
                > > > Be careful of your base assumptions. php 5.1.2 on Linux produces
                > > > -2147483647 as well.
                > > >
                > > > -david-[/color][/color]
                >
                > No, it's difference is in the compiler. The operation you describe
                > causes a double-to-long-integer conversion overflow. Gcc probably
                > throws in a check which sets the value to the integer boundary whereas
                > MS Visual C just lets it wrap around.[/color]

                Comment

                • Chung Leong

                  #9
                  Re: Different binary results with different PHP versions

                  Skeleton wrote:[color=blue]
                  > So what should I do at this point? I need to make XOR operations with
                  > such big(small) numbers.
                  > Chung Leong wrote:[color=green]
                  > > Skeleton wrote:[color=darkred]
                  > > > My PHP 5.1.2 is on Windows. So the problem is about the Operating
                  > > > Systems.
                  > > >
                  > > > David Haynes wrote:
                  > > > > Be careful of your base assumptions. php 5.1.2 on Linux produces
                  > > > > -2147483647 as well.
                  > > > >
                  > > > > -david-[/color]
                  > >
                  > > No, it's difference is in the compiler. The operation you describe
                  > > causes a double-to-long-integer conversion overflow. Gcc probably
                  > > throws in a check which sets the value to the integer boundary whereas
                  > > MS Visual C just lets it wrap around.[/color][/color]

                  What exactly are you trying to do? One possible solution is to perform
                  the operation on strings instead:

                  <?

                  $a = "\x01\x02\x 03";
                  $b = "\xFF\x02\x 03";

                  $c = $a ^ $b;

                  for($i = 0; $i < strlen($c); $i++) {
                  printf("%x, ", ord($c[$i]));
                  }

                  ?>

                  Comment

                  Working...