pashanoid has asked for the wisdom of the Perl Monks concerning the following question:
I've never dealt with bits and bytes, so I'm unable to figure out the first 5 bits and last 3 bits of say 129... supposed to get 16 and 3 or 2 or something... Thank you!
I sort of came up with this, but it's not nice:
#!/usr/bin/perl # $num = 129; $str = unpack("B32", pack("N", $num)); $str =~ s/^0+(?=\d)//; # otherwise you'll get leading zeros print "$str\n"; $fragment = substr $str, 0, 5; #first 5 bytes print "$fragment\n"; $num1=unpack("N", pack("B32", substr("0" x 32 . $fragment, -32))); print "num1=$num1\n"; $fragment = substr $str, 5, 3; #first 5 bytes print "$fragment\n"; $num2=unpack("N", pack("B32", substr("0" x 32 . $fragment, -32))); print "num2=$num2\n";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: first 5 bits and last 3 bits
by moritz (Cardinal) on Jul 26, 2011 at 12:39 UTC | |
by pashanoid (Scribe) on Jul 26, 2011 at 12:55 UTC | |
by cdarke (Prior) on Jul 26, 2011 at 13:08 UTC | |
Re: first 5 bits and last 3 bits
by fisher (Priest) on Jul 26, 2011 at 12:38 UTC | |
by DrHyde (Prior) on Jul 27, 2011 at 09:26 UTC | |
Re: first 5 bits and last 3 bits
by ikegami (Patriarch) on Jul 26, 2011 at 18:55 UTC | |
by pashanoid (Scribe) on Jul 27, 2011 at 06:15 UTC | |
Re: first 5 bits and last 3 bits
by jpl (Monk) on Jul 26, 2011 at 20:32 UTC | |
by ikegami (Patriarch) on Jul 26, 2011 at 21:30 UTC |
Back to
Seekers of Perl Wisdom