GaryHull
GaryHull

Reputation: 59

Awk and() operator

I have a code in awk like this:

val = and($1, 0x03FFFFFF) + $2

I wrote the Perl version like this:

$val = ($1 && 0x03FFFFFF) + $2;

Are this lines the same?

Upvotes: 3

Views: 2044

Answers (1)

DVK
DVK

Reputation: 129383

Not quite - you want a &, not &&:

$val = ($1 & 0x03FFFFFF) + $2; 

Please note that you are correct in that the parenthesis are needed due to precedence


In more detail:

  • Your awk expression uses and() which is defined in gawk manual's ch. "8.1.6 Bit-Manipulation Functions of gawk" thusly:

    and(v1, v2) ======= Returns the bitwise AND of the values provided by v1 and v2.

  • Therefore, in Perl you want a bitwise "and" ; not a logical one, which according to perldoc perlop is a single ampersand: "&"

Upvotes: 6

Related Questions