how does this work

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

    how does this work

    Hi Folk

    I came accross this very simple function, but I have no idea how it works
    (the ampersands). Can you explain it.

    function is_even($num){
    return (is_numeric($nu m)&(!($num&1))) ;
    }

    TIA

    Nicolaas


  • milahu

    #2
    Re: how does this work

    Heh, nice solution! :)

    The 1st statement checks, if $num is a number.

    The 2nd one checks, if the the binary "one" is set, as it is with every
    odd number.
    Alternatively, you could use modulo:
    !($num % 2)

    IMO, the most odd thing about this is the single "&" between the two
    statements; I would use "&&" or "and" here. ;)

    Cheers..

    Comment

    • Tim Martin

      #3
      Re: how does this work

      milahu wrote:[color=blue]
      > Heh, nice solution! :)
      >
      > The 1st statement checks, if $num is a number.
      >
      > The 2nd one checks, if the the binary "one" is set, as it is with every
      > odd number.
      > Alternatively, you could use modulo:
      > !($num % 2)
      >
      > IMO, the most odd thing about this is the single "&" between the two
      > statements; I would use "&&" or "and" here. ;)[/color]

      Why would you do that? '&&' and '&' are completely different operators,
      and give different results when evaluated in boolean context on two numbers.

      Tim

      Comment

      • milahu

        #4
        Re: how does this work

        Tim Martin wrote:[color=blue]
        > milahu wrote:[color=green]
        > > IMO, the most odd thing about this is the single "&" between the two
        > > statements; I would use "&&" or "and" here. ;)[/color]
        >
        > Why would you do that? '&&' and '&' are completely different operators,
        > and give different results when evaluated in boolean context on two numbers.[/color]

        Because I prefer comparing boolean values with logical instead of byte
        operators.

        Comment

        • Tim Martin

          #5
          Re: how does this work

          milahu wrote:[color=blue]
          > Tim Martin wrote:[color=green]
          >> milahu wrote:[color=darkred]
          >>> IMO, the most odd thing about this is the single "&" between the two
          >>> statements; I would use "&&" or "and" here. ;)[/color]
          >> Why would you do that? '&&' and '&' are completely different operators,
          >> and give different results when evaluated in boolean context on two numbers.[/color]
          >
          > Because I prefer comparing boolean values with logical instead of byte
          > operators.
          >[/color]

          Sorry, I misread you. I thought you were referring to replacing the
          second '&', not the first one. I didn't even spot that the two boolean
          expressions were being combined with a bitwise operator (which I agree
          is wrong).

          For reference, the function in question was:

          function is_even($num){
          return (is_numeric($nu m)&(!($num&1))) ;
          }


          Tim

          Comment

          • windandwaves

            #6
            Re: how does this work

            windandwaves wrote:
            Thanks folk

            It makes more sense now. I gather that the single ampersand test for a
            particular bit in a byte, while the double ampersand is the AND operator.

            Thanks a million.

            Nicolaas


            Comment

            • Steve

              #7
              Re: how does this work

              On Fri, 21 Apr 2006 11:20:27 +0100, Tim Martin wrote:
              [color=blue]
              > milahu wrote:[color=green]
              >> Tim Martin wrote:[color=darkred]
              >>> milahu wrote:
              >>>> IMO, the most odd thing about this is the single "&" between the two
              >>>> statements; I would use "&&" or "and" here. ;)
              >>> Why would you do that? '&&' and '&' are completely different operators,
              >>> and give different results when evaluated in boolean context on two numbers.[/color]
              >>
              >> Because I prefer comparing boolean values with logical instead of byte
              >> operators.
              >>[/color]
              >
              > Sorry, I misread you. I thought you were referring to replacing the
              > second '&', not the first one. I didn't even spot that the two boolean
              > expressions were being combined with a bitwise operator (which I agree
              > is wrong).
              >
              > For reference, the function in question was:
              >
              > function is_even($num){
              > return (is_numeric($nu m)&(!($num&1))) ;
              > }
              >
              >
              > Tim[/color]

              Why is it wrong? you're anding 0 or 1 with 0 or 1, and that'll return 1
              only if both are 1. It might even be faster - probably not though.

              Comment

              • Tim Martin

                #8
                Re: how does this work

                Steve wrote:[color=blue]
                > On Fri, 21 Apr 2006 11:20:27 +0100, Tim Martin wrote:[color=green]
                >> Sorry, I misread you. I thought you were referring to replacing the
                >> second '&', not the first one. I didn't even spot that the two boolean
                >> expressions were being combined with a bitwise operator (which I agree
                >> is wrong).
                >>
                >> For reference, the function in question was:
                >>
                >> function is_even($num){
                >> return (is_numeric($nu m)&(!($num&1))) ;
                >> }[/color]
                >
                > Why is it wrong? you're anding 0 or 1 with 0 or 1, and that'll return 1
                > only if both are 1. It might even be faster - probably not though.[/color]

                It's wrong in the sense of being a bad habit, not in the sense of
                returning the wrong result. Doing this sort of thing regularly can lead
                to brittle code that breaks due to seemingly innocuous changes.

                Tim

                Comment

                Working...