Here's a short program that does this with byte-at-a-time arithmetic. You can make it do word-at-a-time with a little playing, which should be faster.
#!/usr/bin/perl -w
use strict;
use constant WORDBYTES => 1;
use constant WORDBITS => WORDBYTES*8;
use constant WORDNYBBLES => WORDBYTES*2;
use constant WORDMASK => (2**WORDBITS)-1;
sub left_shift_hex_string
{
my($bits,$str,$add)=@_;
my $val = hex(substr($str,0,WORDNYBBLES));
my $carryout = $val >> (2**WORDBYTES-$bits);
if (length($str) > WORDNYBBLES)
{
my($carryin,$res)=left_shift_hex_string($bits,substr($str,WORDNYBB
+LES),$add);
return ($carryout,sprintf "%x%s", ($val<<$bits | $carryin) & WORDM
+ASK, $res);
}
else
{
warn "val=$val, bits=$bits, add=$add\n";
return($carryout,sprintf "%x", ($val<<$bits|$add)&WORDMASK);
}
}
my($co,$val)=left_shift_hex_string(1,"00ffffff",0);
print "val=$val\n";
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.