in reply to Re: calculating the 3 MSBs from an integer
in thread calculating the 3 MSBs from an integer
I think I have understood this correctly but please put me right if I am barking up the wrong tree
the return automagically maps this to the decimal for the three remaining bits. Was use integer necessary for this or does it improve efficiency ?$_[0] & 0xff # A bitmask to gets the last (8 bit) byte from the intege +r. # It has exactly the same result as $_[0] % 256 >> 5 # bitshift the result of the above so all but # the 3 MSB drop off the end.
Here is a one liner to dump what is going on
and some of the output for the interesting regionsperl -le'print join "\t", $_, (unpack "B*", pack "I*", $_),($_ & 0xFF) + >> 5 for (0..256)'
0 00000000000000000000000000000000 0 1 00000000000000000000000000000001 0 2 00000000000000000000000000000010 0 . . 30 00000000000000000000000000011110 0 31 00000000000000000000000000011111 0 32 00000000000000000000000000100000 1 33 00000000000000000000000000100001 1 . . 222 00000000000000000000000011011110 6 223 00000000000000000000000011011111 6 224 00000000000000000000000011100000 7 225 00000000000000000000000011100001 7 . . 255 00000000000000000000000011111111 7 256 00000000000000000000000100000000 0
Cheers,
R.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: calculating the 3 MSBs from an integer
by borisz (Canon) on Feb 17, 2005 at 14:13 UTC | |
|
Re^3: calculating the 3 MSBs from an integer
by insaniac (Friar) on Feb 17, 2005 at 14:40 UTC | |
by Random_Walk (Prior) on Feb 17, 2005 at 15:27 UTC | |
by insaniac (Friar) on Feb 17, 2005 at 21:14 UTC | |
by Random_Walk (Prior) on Feb 17, 2005 at 22:20 UTC | |
by insaniac (Friar) on Feb 18, 2005 at 06:43 UTC |