title: How to convert RGB to RGBA using bit-manipulation
body: <p>I have been trying to figure out this problem for a day or so, and I'm a bit confused with what's going on.</p>
  
  <p>Essentially I have this method in order to convert a BGR Integer to RGB and create a Color out of it</p>
  
  <pre><code>      Color c2 = new Color((BGRColorNumber &amp; 0xFF),
      ((BGRColorNumber &gt;&gt; 8) &amp; 0xFF),
      ((BGRColorNumber &gt;&gt; 16) &amp; 0xFF));
  </code></pre>
  
  <p>The color codes can be found here <a href="http://www.endprod.com/colors/" rel="nofollow">http://www.endprod.com/colors/</a></p>
  
  <p>This works perfectly, but I am now trying to do this as an RGBA integer instead of RGB.   I'm a bit confused exactly how Alpha fits into an RGB integer.  It seems that if you have just RGB or BGR or whatever you can just make alpha 1.0/255 it seems, or is there more to it than that?</p>
  
  <p>Here is the code I have been working with</p>
  
  <pre><code>public class test 
  {
  
  public static void main (String[] args)
  
  {
  
      test t = new test();
  
  
      System.out.println(t.asIntRGBA(4823790));
  
  }
  
      public int asIntRGBA(int BGRColorNumber)
  {
  
  
  
      int rgba = 
                (((int) (BGRColorNumber         &amp; 0xFF) &amp; 0xFF) &lt;&lt; 16)
              | (((int) ((BGRColorNumber &gt;&gt; 8)  &amp; 0xFF) &amp; 0xFF) &lt;&lt; 8 )
              | (((int) (BGRColorNumber &gt;&gt; 16)  &amp; 0xFF) &amp; 0xFF)
              | (((int) ((BGRColorNumber &gt;&gt; 24) &amp; 0xFF) &amp; 0xFF) &lt;&lt; 24);
  
      Color c = new Color(rgba);
  
    Color c2 = new Color((BGRColorNumber &amp; 0xFF),
      ((BGRColorNumber &gt;&gt; 8) &amp; 0xFF),
      ((BGRColorNumber &gt;&gt; 16) &amp; 0xFF));
  
  
    Color c3 = new Color(15637065);
    System.out.println((((int) ((BGRColorNumber &gt;&gt; 24) &amp; 0xFF) &amp; 0xFF) &lt;&lt; 24) + " : " + c.getAlpha());
      System.out.println(String.format("#%02x%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()));
      System.out.println(String.format("#%02x%02x%02x%02x", c2.getRed(), c2.getGreen(), c2.getBlue(), c2.getAlpha()));
        System.out.println(String.format("#%02x%02x%02x%02x", c3.getRed(), c3.getGreen(), c3.getBlue(), c3.getAlpha()));
      return (rgba);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is my output</p>
  
  <p>0 : 255</p>
  
  <p>"#ee9a49ff"</p>
  
  <p>"#ee9a49ff"</p>
  
  <p>"#ee9a49ff"</p>
  
  <p>15637065</p>
  
  <p>So it works if I convert it to a color, and then output RGBA, but I figured why waste an Object creation and then getting the values when I can just do the colorInteger and Bitwise shift operation itself?</p>
  
  <p>When doing the operation on the Alpha channel, it gives me 00/0, but when using Color.getAlpha(); I'm given ff/255....?</p>
  
  <p>I also am not 100% sure what the &lt;&lt; # is for, but it seems that you place the values of >> # into &lt;&lt; #.  So if I'm getting for blue that's in the red spot, and putting it into the blue spot, the same goes for the red.</p>
  
  <p>In this case it seems that Alpha is in the highest position of 24, so I'm assuming this is initially ABGR????</p>
  
  <p>When I switch to</p>
  
  <pre><code>        int rgba = 
                (((int) (BGRColorNumber         &amp; 0xFF) &amp; 0xFF) &lt;&lt; 24)
              | (((int) ((BGRColorNumber &gt;&gt; 8)  &amp; 0xFF) &amp; 0xFF) &lt;&lt; 16)
              | (((int) ((BGRColorNumber &gt;&gt; 16)  &amp; 0xFF) &amp; 0xFF) &lt;&lt; 8)
              | (((int)  (BGRColorNumber &gt;&gt; 24) &amp; 0xFF) &amp; 0xFF);
  
  System.out.println(String.format("#%02x%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()));
  </code></pre>
  
  <p>my output for this is "#9a4900ff"
  instead of            "#ee9a49ff"</p>
  
  <p>However if I do</p>
  
  <pre><code>System.out.println(String.format("#%02x%02x%02x%02x",  ((rgba &gt;&gt; 24) &amp; 0xFF),  ((rgba &gt;&gt; 16) &amp; 0xFF),  ((rgba &gt;&gt; 8) &amp; 0xFF), ((rgba &gt;&gt; 0) &amp; 0xFF)));
  </code></pre>
  
  <p>I get "#ee9a4900"  which is basically normal, except for the 00 bug with the Alpha, except it's bugging out trf now....???????  00 instead of ee and Alpha is ff now instead of 00?</p>
  
  <p>So I'm confused what exactly I'm doing wrong...? 
  Any advice is appreciated!</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p>EDIT:  I realized that maybe it is because there is only RGB places from 0 to 16, and 24 itself doesn't exist which is why I'm getting 00 for anything that I'm shifting into the 24 with "&lt;&lt;24?"</p>
  
  <p>Maybe I'll just put a 255/ff into the Alpha spot of 24 and call it a day.  If the value is being 00, then I just need to make it not be that :).</p>
  
  <p>I still do not understand why Color.getAlpha() returned ff, and not 00 though...?</p>
  
  <hr>
  
  <p>EDIT 2:</p>
  
  <p>So I ended up figuring out the issue.</p>
  
  <p>Essentially I didn't have anything in position 24, and only 0-23 was being used up.  This meant I was given a 0 for the value of the last 8 24-31.</p>
  
  <p>I also realized the reason the color was giving me ff/255 for Alpha, was because I had not told the Color I had an Alpha value, thus it was giving it the default of ff/255.</p>
  
  <pre><code>By doing Color c = new Color (#, true);
  </code></pre>
  
  <p>I was able to turn on the Alpha value.</p>
  
  <p>Here is how I solved my problem.</p>
  
  <pre><code>public class test 
  </code></pre>
  
  <p>{</p>
  
  <pre><code>public static void main (String[] args)
  {
  
      test t = new test();
  
  
      System.out.println(t.asIntRGBA(4823790));
  
  }
  
      public int asIntRGBA(int BGRColorNumber)
  {
  
  
  
      int rgba = 
                (((int) (BGRColorNumber         &amp; 0xFF) &amp; 0xFF) &lt;&lt; 24)
              | (((int) ((BGRColorNumber &gt;&gt; 8)  &amp; 0xFF) &amp; 0xFF) &lt;&lt; 16 )
              | (((int) ((BGRColorNumber &gt;&gt; 16)  &amp; 0xFF) &amp; 0xFF) &lt;&lt; 8)
              | ((int) (252 &amp; 0xFF) &amp; 0xFF);
  
      Color c = new Color(rgba, true);
     // Color c3 = Color.TRANSLUCENT;
  
    Color c2 = new Color((BGRColorNumber &amp; 0xFF),
      ((BGRColorNumber &gt;&gt; 8) &amp; 0xFF),
      ((BGRColorNumber &gt;&gt; 16) &amp; 0xFF), 252);
  
  
    Color c3 = new Color(-51471799, true);
    System.out.println(((rgba &gt;&gt; 24) &amp; 0xFF) + " | " +  c.getRed() +  " | " + c.getGreen() + " | " + c.getBlue() + " | " + c.getAlpha());
    //System.out.println(();
      System.out.println(String.format("#%02x%02x%02x%02x",((rgba &gt;&gt; 24) &amp; 0xFF), ((rgba &gt;&gt; 16) &amp; 0xFF), ((rgba &gt;&gt; 8) &amp; 0xFF), ((rgba) &amp; 0xFF)));
      System.out.println(String.format("#%02x%02x%02x%02x", c2.getRed(), c2.getGreen(), c2.getBlue(), c2.getAlpha()));
        System.out.println(String.format("#%02x%02x%02x%02x", c3.getRed(), c3.getGreen(), c3.getBlue(), c3.getAlpha()));
      return (rgba);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I changed getting a value with >> 24 to just putting a value (in this case 252), to test.  Technically they will all be 255.</p>
  
  <p>This gives me the correct values and corresponds to the correct Hex value according to the above page..</p>
  
  <p>However I am having an issue with </p>
  
  <pre><code>  System.out.println(String.format("#%02x%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()));
  </code></pre>
  
  <p>being correct</p>
  
  <p>Correct output:</p>
  
  <p>"#ee9a49fc"</p>
  
  <p>Color's output:</p>
  
  <p>"#9a49fcee"</p>
  
  <p>if I take the int value I got from rgba "-291878404"  and plugged it into Color c3, I get the same values as Color c.  I'm not sure what is wrong, but I am having an issue getting everything to be the same.</p>
  
  <p>rgba</p>