t_mask = $mask;
       for(my $i=0;$i&lt;=7;$i++) {                     # Iterate Mask
         my $val = $col-&gt;[$h];                         # Value
         $bit_plane[$i][$w][$h] = $val &amp; $act_mask;    # Binary AND: Value &amp; Mask
         $act_mask = $mask &lt;&lt; $i;                      # Shift Mask
         if($type eq &quot;lsb&quot;) { 
           $bit_plane[$i][$w][$h] = $bit_plane[$i][$w][$h] &gt;&gt; $i; 
         }
         elsif($type eq &quot;msb&quot;) { 
           $bit_plane[$i][