Questions about manipulating binary numbers like that are often best solved by looking them over in binary. Using a little bit of perl:
#!/usr/bin/perl use strict; use warnings; my @memory = (0, 0, 5, 59); print "Original values:\n"; printf "% 3u : %08b\n", $_, $_ for @memory; $memory[1] = $memory[1] << 7; $memory[2] = $memory[2] << 14; $memory[3] = $memory[3] << 21; print "\nAfter shifting the values\n"; printf "% 10u : %032b\n", $_, $_ for @memory; my $t = 0; $t += $_ for @memory; print "\nThe sum:\n"; printf "% 10u : %032b\n", $t, $t;
Running it gives a hopefully familiar number: ;^D
Original values: 0 : 00000000 0 : 00000000 5 : 00000101 59 : 00111011 After shifting the values 0 : 00000000000000000000000000000000 0 : 00000000000000000000000000000000 81920 : 00000000000000010100000000000000 123731968 : 00000111011000000000000000000000 The sum: 123813888 : 00000111011000010100000000000000
Update: I just read the thread, and noticed that in your code, you use the same construct several times. Additionally, you mention that you only want seven bits. So I'd suggest writing a subroutine that masks off the high bit of your bytes and packs the number for you. Something like the following, but with a better name:
sub plonk { my ($b0, $b1, $b2, $b3) = @_; return $b0 & 0x7f + ($b1 & 0x7f) << 7 + ($b2 & 0x7f) << 14 + ($b3 & 0x7f) << 21; }
...roboticus
When your only tool is a hammer, all problems look like your thumb.
In reply to Re^5: ID3v2 TAG Footer Reading goes wrong
by roboticus
in thread ID3v2 TAG Footer Reading goes wrong
by thanos1983
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |