Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Bitwise & not working correctly in CGI script

by Jenda (Abbot)
on Jun 13, 2012 at 15:35 UTC ( #975999=note: print w/ replies, xml ) Need Help??


in reply to Bitwise & not working correctly in CGI script

print "19 & 16 = ", (19 & 16), "\n"; print "'19' & '16' = ", ('19' & '16'), "\n"; print "'19' & 16 = ", ('19' & 16), "\n"; print "19 & '16' = ", (19 & '16'), "\n";

The thing is that Perl did not convert the string (keys of a hash are ALWAYS strings, even if you used numeric literals when filling it in.) to a number and instead it evaluated a bitwise AND on the characters of the strings.

Jenda
Enoch was right!
Enjoy the last years of Rome.


Comment on Re: Bitwise & not working correctly in CGI script
Download Code
Re^2: Bitwise & not working correctly in CGI script
by Anonymous Monk on Jun 14, 2012 at 06:28 UTC

    What Jenda is trying to say, what perlnumber (and kennethk ) say, is that you should numify one of your arguments, so write

    #~ if( 0+$flag & $job_type ){ #~ if( $flag & 0+$job_type ){ #~ if( 0+$flag & 0+$job_type ){ if( int $flag & int $job_type ){ $bu = $bj{$flag} ; cluck("business_unit: $bu"); }
Re^2: Bitwise & not working correctly in CGI script
by solegaonkar (Beadle) on Jun 15, 2012 at 08:28 UTC
    Wow! That was right! The problem got solved when I numified both operands of & by adding 0. Thanks for your help!!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://975999]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-07-10 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (217 votes), past polls