-5

Possible Duplicate:
Good tutorials on Bitwise operations in Java
Are the &, |, ^ bitwise operators or logical operators?

Code as below:

int rgb[] = new int[] {     
(argb >> 16) & 0xff, //red     
(argb >>  8) & 0xff, //green     
(argb      ) & 0xff  //blue 
}; 

I saw a code which is getting RGB value from a INT value, the operations are as above, but I never see such operation before, I want to find more information about what are they and what can they do, The symbol >> and & and 0xff.

So, what do you all address them in Java?

Community
  • 1
  • 1
Sam YC
  • 10,725
  • 19
  • 102
  • 158
  • 2
    http://en.wikipedia.org/wiki/Bitwise_operation - `>>` is right shift, `&` is bitwise AND. `0xff` is `255`. – Mysticial Aug 14 '12 at 03:00
  • 1
    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html – Matt Ball Aug 14 '12 at 03:01
  • Right shift, AND, and literal value. – Hot Licks Aug 14 '12 at 03:02
  • 1
    Down vote- question doesnt show minimal effort to find an answer. – mazaneicha Aug 14 '12 at 03:03
  • And you should have learned these operations in the first 15 minutes of studying the language. – Hot Licks Aug 14 '12 at 03:03
  • possible duplicate of [Are the &, |, ^ bitwise operators or logical operators?](http://stackoverflow.com/questions/11597978/are-the-bitwise-operators-or-logical-operators) . Or this one is better operator-in-java (http://stackoverflow.com/questions/4001813/operator-in-java) – CoolBeans Aug 14 '12 at 03:03
  • 2
    This is such an RTFM question, I've voted to close – Bohemian Aug 14 '12 at 03:04
  • 6
    I dislike people writing read the faqing faq or rtfm. Manuals are getting too heavy nowadays and it certainly helps a beginner to just give him/her the simple dang answer. A kid trying to learn programming may not even know that it is called bit shift and look up wikepedia for it. Just patronise them, OK. No good being trigger happy voting questions down or closing them. – Blessed Geek Aug 14 '12 at 03:26
  • All the manuals are online now. They've never been lighter or easier to read. Agree that this is not a stupid question, though. – mob Aug 14 '12 at 04:31

2 Answers2

5

Further explanation of the bit arithmetic. Let's assume the int bits look as follows:

00000000 10101010 01010101 11011011
unused   red      green    blue

To get the int value for blue, you want all bits other than the lower 8 turned off. The easy way to do this is with a bitwise and. 0xff can also be written as 0x000000ff, which will set the upper 24 bits to 0, and only turn on the bits in the lower 8 which are already set to 1.

argb & 0xff ==> 00000000 00000000 00000000 11011011
                                           blue

argb >> 8 ==> 00000000 00000000 10101010 01010101 
                                red      green
(argb >> 8) & 0xff ==> 00000000 00000000 00000000 01010101 
                                                  green

argb >> 16 ==> 00000000 00000000 00000000 10101010
                                          red
(argb >> 16) & 0xff ==> 00000000 00000000 00000000 10101010
                                                   red

even thought the & 0xff on the red value seems unecessary, there's no guarantee the caller might not have set some of the high order unused bits, so you want to be safe and turn all of them off.

Matt
  • 11,523
  • 2
  • 23
  • 33
2
int rgb[] = new int[] {     
(argb >> 16) & 0xff, //red     
(argb >>  8) & 0xff, //green     
(argb      ) & 0xff  //blue 
};

>>: Arithmetic right shift. Also known as signed shift.

&: Bitwise AND

0x....: Hex numbers.

But what is it doing?

(argb >> 16) & 0xff right shifts the value of argb 16 bits, and then performs a bitwise AND operation to capture only 8 bits. This is essentially getting the byte from bits 16-23

Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
  • hi, why after shifting, the RGB value is still INT but not byte? Since 0-255 can fit into one byte right? – Sam YC Aug 14 '12 at 03:13
  • 1
    @GMsoF: This code creates an array of 3 integers. Each color value is ANDed with `0xff` (which is only 8 bits). In this example each RGB value is a 32-bit integer that was constructed from only 8 bits. If you constructed as a byte array, the possible range of values is 0-255 for each channel. You are right, it would have made more sense to place the result into a byte array, considering `0xff` implies only a byte. – Chris Dargis Aug 14 '12 at 03:16
  • So the RGB values stored in INT is reversed? like B->G->R->Alpha? that is why Blue does not need to be shifted? – Sam YC Aug 14 '12 at 03:30
  • 2
    @GMsoF: No, that is not true. Blue isn't being shifted because the low byte (bits 0 - 7) is being considered the blue value. The green value (bits 8 - 15) is the next byte above, with of course red in bits (16 - 23). Bits 24 - 31 are ignored in this example. – Chris Dargis Aug 14 '12 at 03:33