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$..$/
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.