Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Bit operation

by toren (Novice)
on Jan 12, 2011 at 14:00 UTC ( #881892=perlquestion: print w/replies, xml ) Need Help??
toren has asked for the wisdom of the Perl Monks concerning the following question:

I have an attribute (32 bits-long), that each bit responsible to specific functionality. Perl script I'm writing should turn on 4th bit, but save previous definitions of other bits.

I use in my program:
Sub BitOperationOnAttr{ my $a=""; MyGetFunc( $a); $a |= 0x00000008; MySetFunc( $a); }

** MyGetFunc/ MySetFunc my own fuctions that know read/fix value.


1. if usage of $a |= 0x00000008; is right ?

2. how extract hex value by Regular Expression from string I have : For example:

"Attribute: Somestring: value (8 long (0x8))"

Thanks in advance

Replies are listed 'Best First'.
Re: Bit operation
by toolic (Bishop) on Jan 12, 2011 at 14:20 UTC
    1. if usage of $a |= 0x00000008; is right ?
    Yes. All you have to do is try it:
    use strict; use warnings; my $val = 0xffff_fff0; printf "%x\n", $val; $val |= 0x0000_0008; printf "%x\n", $val; __END__ fffffff0 fffffff8
    Side note: don't use $a as a variable name as it has special significance in Perl (sort).
    2. how extract hex value by Regular Expression from string I have : For example:

    You can use the [[:xdigit:]] character class to extract hex values. See perlre.

      Thanks, On my system its look like
      my $val = 0xffff_fff0; printf "%x\n", $val; $val |= 0x0000_0008; printf "%x\n", $val;
      fffffff0 8
        What version of perl are you using?
        perl -v This is perl, v5.8.8 built for MSWin32-x86-multi-thread
        Please double check. Are you sure that's exactly the code you ran? If so, please provide the output of perl -V. (That's an uppercase "V".)
Re: Bit operation
by Anonyrnous Monk (Hermit) on Jan 12, 2011 at 14:09 UTC




    my ($hex) = $string =~ /\b(0x[0-9a-fA-F]+)\b/;

    (then do hex($hex) if you want the value as a number)

Re: Bit operation
by raybies (Chaplain) on Jan 12, 2011 at 14:14 UTC

    you're writing C style perl, but for #1 the |= operand should work fine. Though you don't need all those leading zeroes, unless you think it helps you read the function better, you could just do  $a |= 0x8;

    You can't change the value of $a by using it as a function parameter in MyGetFunc. You'll want to either return that as a value, or pass in a reference to the variable, and access it directly. ex.  $a = MyGetFunc() would change the value of $a, but passing it to a function would not, unless you passed a reference to $a.

    Not sure I understand the whole question, what are you asking about regarding a regex? Are you extracting some string? And are they seperated by attribute:string: value? If so , then you may just need to use split on the colon delimiter and store the values separately.) (then use the perl function "hex" to convert a string of hexadecimal digits to a hex integer value)

      perl -e ' sub dom { $_[0]=2;} $x=1; dom($x); print $x,"\n";' #prints 2

      Although it is not best practice to do so you can change parameters of subroutines

        Fine. I stand corrected... and I'm pretty sure the guy asking about how to use a bitwise or will find this practice very useful. Buwahahaha. :)
      You can't change the value of $a by using it as a function parameter in MyGetFunc.

      Sure you can, perl is perl :)

      sub lunch { $_[0]++ } my $foo = 1; warn $foo; lunch($foo); warn $foo; __END__ 1 at - line 3. 2 at - line 5.
      search for alias in perlsub

        I assume what raybies was referring to was that unless $a is within a scope that encloses the subroutine (which it is in your case but may not necessarily be in the OP's case) then it *can* be modified without having to return it.

        However, in my experience, having been bitten way too many times by not paying attention to scopes and mis-using "global variables", in my humble opinion, it is much, much better to be careful and discipline oneself to think of it more along the lines as raybies posed it by keeping things more encapsulated and planning for the variable to be local to the subroutine and then explicitly returning values that you want to change...or to at least be clearer, through the use of references as some of the other responders suggested, that you are doing the change.

        But that is just my opinion and certainly your comment is, to the best of my knowledge, right.

        ack Albuquerque, NM
Re: Bit operation
by sundialsvc4 (Abbot) on Jan 12, 2011 at 14:40 UTC

    Yes, the correct way to manipulate bits is:

    • To turn a bit on, use or mask.
    • To turn a bit off, use and (not mask).
    • To determine if a bit is on or off, use and mask (in an if expression) and test for zero.
    • You cannot both set/reset and test a bit in a single operation (in a high-level language such as Perl).   Remember that race conditions can occur in multi-threaded applications, because the operation is not “atomic.”

    You do not have to provide leading zeros.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://881892]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2017-02-22 22:25 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (338 votes). Check out past polls.