in reply to Re: Testing a number for oddness
in thread Testing a number for oddness

On constants, the AND may be getting optimized away, and in checking I find that it is...
# perl -MO=Deparse,-p -we "print 15 & 1" print(1); -e syntax OK # perl -MO=Deparse,-p -we "print 16 & 1" print(0); -e syntax OK

--
$you = new YOU;
honk() if $you->love(perl)

Replies are listed 'Best First'.
Re: Re: Re: Testing a number for oddness
by chipmunk (Parson) on Jan 23, 2001 at 09:40 UTC
    It turns out that almost all operators in Perl get optimized away in this manner. 'Constant folding', as it's called, is a very common optimization.
    % perl -MO=Deparse print 15 + 1, 15 - 1, 15 * 1, 15 % 1, 15 & 1, 15 ^ 1, 15 << 1, !15, 15 || $x, 15 . 1, 15 < 1, 15 <=> 1, 15 ? $x : $y; __END__ print 16, 14, 15, 0, 1, 14, 30, 0, 15, '151', 0, 1, $x;
    In fact, the only operator I've found that could have this optimization but doesn't is x.
Re: Re: Re: Testing a number for oddness
by runrig (Abbot) on Jan 23, 2001 at 08:35 UTC
    That's why I set '$i' to a constant (my $i=15), then AND'ed it. I checked, and THAT won't get optimized away.