title: Bit manipulation of int in java, print information contained within int
body: <p>In the code below (in the else statement) I am trying to get the value for the red blue and green parts of the given pixel. I am not sure how to get the value from the int. This code is from another stack post <a href="https://stackoverflow.com/questions/6524196/java-get-pixel-array-from-image">Java - get pixel array from image</a> I am trying to modify it to tell me if it has found a pixel of a specific color (I know the RGB of this color and want to compare each pixel). </p>
  
  <p>How can I get each R, G, and B in the 0-255 value range?</p>
  
  <pre><code>private static int[][] convertBImageToArr(BufferedImage image) 
      {
          final byte[] pixels;
          DataBuffer rasterData = image.getRaster().getDataBuffer();
          DataBufferByte rasterByteData = (DataBufferByte)rasterData;
          pixels = rasterByteData.getData();
          final int width = image.getWidth();
          final int height = image.getHeight();
          final boolean hasAlphaChannel = image.getAlphaRaster() != null;
  
          int[][] result = new int[height][width];
          if (hasAlphaChannel) 
          {
              final int pixelLength = 4;
              for(int pixel = 0, row = 0, col = 0; pixel &lt; pixels.length; pixel += pixelLength) 
              {
                  int argb = 0;
                  argb += (((int) pixels[pixel] &amp; 0xff) &lt;&lt; 24); // alpha
                  argb += ((int) pixels[pixel + 1] &amp; 0xff); // blue
                  argb += (((int) pixels[pixel + 2] &amp; 0xff) &lt;&lt; 8); // green
                  argb += (((int) pixels[pixel + 3] &amp; 0xff) &lt;&lt; 16); // red
  
                  result[row][col] = argb;
                  col++;
                  if (col == width) 
                  {
                      col = 0;
                      row++;
                  }
              }
          } 
          else 
          {
              final int pixelLength = 3;
              for (int pixel = 0, row = 0, col = 0; pixel &lt; pixels.length; pixel += pixelLength) 
              {
                 int argb = 0;
                 argb += -16777216; // 255 alpha
                 argb += ((int) pixels[pixel] &amp; 0xff); // blue
                 argb += (((int) pixels[pixel + 1] &amp; 0xff) &lt;&lt; 8); // green
                 argb += (((int) pixels[pixel + 2] &amp; 0xff) &lt;&lt; 16); // red
  
                 if(row == 11 &amp;&amp; col == 11)
                 {
                  System.out.println("B:" + ((int) pixels[pixel] &amp; 0xff));
                  System.out.println("G:" + (((int) pixels[pixel + 1] &amp; 0xff) &lt;&lt; 8));
                  System.out.println("R:" + (((int) pixels[pixel + 2] &amp; 0xff) &lt;&lt; 16));
                 }
  
                 result[row][col] = argb;
                 col++;
                 if (col == width) 
                 {
                    col = 0;
                    row++;
                 }
              }
          }
          return result;
     }
  </code></pre>