operator |=

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

    operator |=

    hello,

    what does the operator |= mean?

    I saw it in a script :

    $output = "png";

    $style = BCS_ALIGN_CENTE R;
    $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
    $style |= ($output == "jpeg") ? BCS_IMAGE_JPEG : 0;


    thanks


  • Luke Ross

    #2
    Re: operator |=

    Hi,

    ··········· sylvain wrote:[color=blue]
    >
    > what does the operator |= mean?
    >
    > I saw it in a script :
    >
    > $output = "png";
    >
    > $style = BCS_ALIGN_CENTE R;
    > $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
    > $style |= ($output == "jpeg") ? BCS_IMAGE_JPEG : 0;[/color]

    I assume this is the same as the Perl operator, in which case it means,
    if the left hand side isn't true, set it to the right hand side. Useful
    if you want to set up defaults.

    # If $a isn't set, set it to the string "undefined"
    $a |= "undefined" ;

    Regards,

    Luke

    Comment

    • Zac Hester

      #3
      Re: operator |=

      Luke Ross wrote:
      [color=blue]
      > Hi,
      >
      > ··········· sylvain wrote:
      >[color=green]
      >>
      >> what does the operator |= mean?
      >>
      >> I saw it in a script :
      >>
      >> $output = "png";
      >>
      >> $style = BCS_ALIGN_CENTE R;
      >> $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
      >> $style |= ($output == "jpeg") ? BCS_IMAGE_JPEG : 0;[/color]
      >
      >
      > I assume this is the same as the Perl operator, in which case it means,
      > if the left hand side isn't true, set it to the right hand side. Useful
      > if you want to set up defaults.
      >
      > # If $a isn't set, set it to the string "undefined"
      > $a |= "undefined" ;
      >
      > Regards,
      >
      > Luke
      >[/color]

      I think you correctly answered the wrong question. He was asking what
      the |= operator is doing, not the ?/: trinary operator. The trinary
      operator in question does the same as the Perl conditional assignment
      operator.

      However, the |= operator is the unary equivalent of the bitwise OR. It
      means the same thing as:

      $lhs = $lhs | $rhs

      So, this code:

      <?
      $lhs = 6;
      $rhs = 13;
      $lhs |= $rhs;
      echo $lhs;
      ?>

      Would display:

      15

      (Since 00000110 | 00001101 = 00001111)

      The code he found is probably a streamlined way of conditionally setting
      a binary sequence (or set of flags) within a preexisting bitfield. The
      binary operator doesn't have any _expected_ effect on strings (notice
      how the results of the expressions within the trinary operator are
      probably both boolean [or a bitfield in the case of the second
      operand--a full-out guess based on the fact that the second operand is a
      "macroscopi c" definition]).

      The trinary operator is also interesting in this example, though.

      $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;

      Says this:

      1. Check if the variable "$output" is equivalent to the constant "png"
      2. If this expression is true, return the results of the definition
      "BCS_IMAGE_ PNG" to the outer expression...
      3. If this expression is false, return a constant 0 to the outer
      expression...
      4. The resulting macro or '0' is sent to the unary operator which
      updates the variable "$style" using the bitwise OR.

      If $output contains the string "png", and BCS_IMAGE_PNG is set to 1,
      then set the low bit of the $style bitfield (makes the number odd and
      adds 1 to the value, if it was even, _or_ just leaves the number odd and
      maintains its value if it was already odd). The nice thing about this
      method is that if $style already has some information in the bitfield, a
      bitwise OR with "0" will not affect its contents.

      The $style string is probably processed through a bit mask to extract
      individual values from the field:

      <?
      function is_low_bit_set( $bitfield) {
      return($bitfiel d & 1);
      }
      ?>

      This says that if the low bit is set, return a 1, otherwise, return a 0.
      That means that any odd, integer will return a 1, and any even integer
      will return a 0.

      HTH,
      Zac

      Comment

      • Andy Hassall

        #4
        Re: operator |=

        On Fri, 19 Sep 2003 10:48:18 +0200, "·········· · sylvain"
        <sylvainantispa m@sylvax.net> wrote:
        [color=blue]
        >what does the operator |= mean?
        >
        >I saw it in a script :
        >
        >$output = "png";
        >
        >$style = BCS_ALIGN_CENTE R;
        >$style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
        >$style |= ($output == "jpeg") ? BCS_IMAGE_JPEG : 0;[/color]

        $x |= $y is equivalent to $x = $x | $y

        | is bitwise-or.



        e.g.

        <pre>
        <?php
        printf("1 = %03b\n", 1);
        printf("6 = %03b\n", 6);
        printf("1 | 6 = %03b\n", 1|6);
        ?>
        </pre>

        Outputs:

        1 = 001
        6 = 110
        1 | 6 = 111

        --
        Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
        Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

        Comment

        • BKDotCom

          #5
          Re: operator |=

          More specifically,
          $a |= $b;
          is the same as
          $a = $a || $b;


          hey, look at that... a whole section of the manual dedicated to operators.

          Luke Ross <lukeross@sys31 75.co.uk> wrote in message news:<vmli6itl5 sfd44@corp.supe rnews.com>...[color=blue]
          > Hi,
          >
          > ··········· sylvain wrote:[color=green]
          > >
          > > what does the operator |= mean?
          > >
          > > I saw it in a script :
          > >
          > > $output = "png";
          > >
          > > $style = BCS_ALIGN_CENTE R;
          > > $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
          > > $style |= ($output == "jpeg") ? BCS_IMAGE_JPEG : 0;[/color]
          >
          > I assume this is the same as the Perl operator, in which case it means,
          > if the left hand side isn't true, set it to the right hand side. Useful
          > if you want to set up defaults.
          >
          > # If $a isn't set, set it to the string "undefined"
          > $a |= "undefined" ;
          >
          > Regards,
          >
          > Luke[/color]

          Comment

          • Andy Hassall

            #6
            Re: operator |=

            On 19 Sep 2003 13:46:25 -0700, BradKent@swbell .net (BKDotCom) wrote:
            [color=blue]
            >More specifically,
            >$a |= $b;
            >is the same as
            >$a = $a || $b;[/color]

            No, it's not.

            $a |= $b is $a = $a | $b

            $a | $b is bitwise OR.
            $a || $b is logical OR.

            <pre>
            <?php
            var_dump(1 | 4);
            var_dump(1 || 4);
            ?>
            </pre>

            Outputs:

            int(5)
            bool(true)

            --
            Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
            Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

            Comment

            Working...