  # iterate bit 0 to bit 7
     $bit_plane[$i] = $layer &amp; $mask;               # mask image
     $mask = $mask &lt;&lt; 1;                            # shift mask to bit 1
     if($type eq &quot;lsb&quot;) {                                  
       $bit_plane[$i] = $bit_plane[$i] &gt;&gt; $i;       # shift to LSB
     }
     elsif($type eq &quot;msb&quot;) { 
       $bit_plane[$i] = $bit_plane[$i] &lt;&lt; (7 - $i); # shift to MSB
     }
   }
   return @bit_plane;                               # return 