1 2 3 4
sub reverseBitsIn8BitChar { my $c = shift; return 0xFF & ((($c * 0x0802 & 0x22110) | ($c * 0x8020 & 0x88440)) * 0x10101 >> 16); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#! /usr/bin/env perl use strict; use warnings; use 5.010.000; #> sub routines #> -------------------------------------------------------------------------- sub hexstring_bitwise_reverted { # see output of "perldoc -f convert"; or see manually in perlfaq4 return sprintf "0x%02X", oct( "0b" . reverse sprintf( "%08b", oct(shift) ) ); # | | | | | | # | | | | | fetch first argument (a hex string 0xAB) # | | | | convert it to decimal # | | | create bitstring of decimal # | | invert the order of the bits in bitstring # | convert reverted bitstring back to decimal # create a hexstring again to return } #> main program #> -------------------------------------------------------------------------- my $hexstring = "0xFC"; say join "\n", $hexstring, hexstring_bitwise_reverted( $hexstring );