Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Bitwise operations

by SuicideJunkie (Vicar)
on Jan 08, 2014 at 20:05 UTC ( [id://1069857]=note: print w/replies, xml ) Need Help??


in reply to Bitwise operations

Easiest way to see what it does is to do it with lots of print statements.

use strict; use warnings; my $val = 0; p('Zero'); $val |= 1; p('Or-ed 1'); $val <<= 1; p('shift 1'); $val |= 0; p('Or-ed 0'); $val <<= 1; p('shift 1'); $val |= 1; p('Or-ed 1'); $val <<= 1; p('shift 1'); $val |= 0; p('Or-ed 0'); #Prints in binary with lowest bit to the right side. sub p { printf '%20s : %4d => ', shift, $val; print reverse split '', unpack'b*', pack'c', $val; print "\n"; }
C:\>perl test.pl Zero : 0 => 00000000 Or-ed 1 : 1 => 00000001 shift 1 : 2 => 00000010 Or-ed 0 : 2 => 00000010 shift 1 : 4 => 00000100 Or-ed 1 : 5 => 00000101 shift 1 : 10 => 00001010 Or-ed 0 : 10 => 00001010

Replies are listed 'Best First'.
Re^2: Bitwise operations
by toolic (Bishop) on Jan 08, 2014 at 20:22 UTC
    See also: %b
    #Prints in binary with lowest bit to the right side. sub p { printf "%20s : %4d => %08b\n", shift, $val, $val; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-20 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found