Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Convert bitwise value to its value

by gibsonca (Beadle)
on May 16, 2012 at 20:15 UTC ( #970912=perlquestion: print w/replies, xml ) Need Help??
gibsonca has asked for the wisdom of the Perl Monks concerning the following question:

After I read in a string from a file, how do I get the numeric value?

(1 << 18) | (0x00 << 12) | (0x00 << 6) | 0x00

I believe the correct value should be 0x40000. Tired of guessing my way thru...

Replies are listed 'Best First'.
Re: Convert bitwise value to its value
by sauoq (Abbot) on May 16, 2012 at 21:48 UTC

    And if you don't trust the input, you can use the same technique more Safely like so:

    #!/usr/bin/perl use warnings; use strict; use Safe; my @ops = qw( padany leaveeval lineseq bit_or left_shift const left_shift right_shift bit_and bit_xor bit_or negate i_negate not complement); my $safe = Safe->new(); $safe->permit_only(@ops); my $result = $safe->reval( '(1 << 18) | (0x00 << 12) | (0x00 << 6) | 0 +x00' ); printf "0x%x\n", $result;

    "My two cents aren't worth a dime.";
Re: Convert bitwise value to its value
by mbethke (Hermit) on May 16, 2012 at 20:24 UTC
    If you trust the input to contain nothing evil, eval will do:
    $s = "(1 << 18) | (0x00 << 12) | (0x00 << 6) | 0x00"; printf "0x%x\n", eval $s;
Re: Convert bitwise value to its value
by AnomalousMonk (Chancellor) on May 17, 2012 at 03:40 UTC

    ... and there's also a programmers calculator, such as the app supplied standard with Windoze. I assume there is something similar in *nix.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://970912]
Approved by Corion
[Corion]: Meh ... Minion really doesn't want to run under the fork() emulation on Windows (which I understand), but has no other way either (which I find inconvenient)
[Corion]: Other than that, it seems really polished

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2018-06-21 14:59 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.