in reply to Re: Bitwise operators
in thread Bitwise operators

Would the following help you?  It's my attempt to provide a pictorial representation of what's happening:
use strict; use warnings; my $value = 51; # Accumulate binary digits ("bits") my @bits = ( ); foreach my $place (0 .. 7) { my $nextbit = mask_off_bit($value, $place); push @bits, $nextbit; } # Display each bit printf "\nNumber %3d in binary is: ", $value; map { print "$_" } reverse @bits; print "\n"; # Test the results against good ol' printf printf " Which is the same as: %08b\n", $value; sub mask_off_bit { my ($num, $place) = @_; my $mask = 1 << $place; # Same as 2 ** $place my $mstr = " " x (8 - $place) . "^"; # Graphic representati +on printf " Num %3d: 0b%08b\n", $num, $num; # Number in decimal & +binary my $bit = ($mask == ($num & $mask))? 1: 0; # Next bit is one or z +ero printf "Mask %3d: %s => bit %d\n", $mask, $mstr, $bit; return $bit; }

The above code is masking off each bit, using a mask of 1, 2, 4, 8, etc. in the line:

my $bit = ($mask == ($num & $mask))? 1: 0


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^3: Bitwise operators
by thevoid (Scribe) on Dec 25, 2006 at 19:48 UTC
    Thanks so much for taking the time to do that, but it's a bit advanced for me - there's a lot there I haven't covered and don't recognise : )