use strict; use warnings; my $infile = 'yourinputfilename'; my $outfile = 'youroutputfilename'; open(my $fhin, '<', $infile) or die "error: open '$infile': $!"; binmode($fhin); open(my $fhout, '>', $outfile) or die "error: open '$outfile': $!"; binmode($fhout); local $/ = \2; # make <> operator read two bytes at a time while (my $TileData = <$fhin>) { if (length($TileData) != 2) { warn "oops, file is of uneven length, last byte='$TileData'\n"; last; } # do your thing with bitwise stuff here... # my $NewTileData = ... print {$fhout} $NewTileData; }