We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › What is ">>" and how to read it.
Page Index Toggle Pages: 1
What is ">>" and how to read it. (Read 445 times)
What is ">>" and how to read it.
Mar 12th, 2008, 12:29am
 
Hi all,

I am having trouble looking for references and an explanation on this symbol ">>". And how would you read/understand this line of code:

Code:
// Extract the red, green, and blue components from current pixel
int currR = (currColor >> 16) & 0xFF; // Like red(), but faster
int currG = (currColor >> 8) & 0xFF;
int currB = currColor & 0xFF;


All I know is that it extracts RBG colours but I don't know how this part work: Code:
>> 16) & 0xFF; 



Thanks : )

***Thank you for the help JohnG. I finally understand now Cheesy
Re: What is ">>" and how to read i
Reply #1 - Mar 12th, 2008, 1:00am
 
">> 16" means "shift right 16 bits".

All integers are internally binary, that is a bunch of 1s and 0s.

for instance 203 in binary is 11001011. What the >> operator says is that we want to shuffle the 1s and 0s to the right by however many places.

So 203 >> 2 means 11001011 >> 2 which is 00110010 which is 50 in decimal.

Now where this comes in handy is when you use a single int to contain multiple values, in this case a colour.

Processing stores all colours in a format known as ARGB, or Alpha, Red, Green, Blue.

So you have one 32 bit number (that's 32 1s and 0s), but it actually contains four 8bit numbers, that's 8bits for each of alpha/red/green/blue.

If we're just interested in the red part, we need to extract it from the rest of the values contained within the ARGB int.

The quick (in terms of processing power) way to get the value is to shift all the bits in the number right 16 places (that's 2 * 8) which makes the blue and green values drop off the bottom, leaving us with just the alpha and red.

Now to get rid of the Alpha part we use "&" which means "And" at the binary level. 0xFF is a hexadaecimal representaton of an 8bit number which is made up of all 1s and no 0s. It might be clearer to write it as 0x00FF since we're using it on a (now) 16 bit value (that's 8 for alpha, 8 for red). When we do this "And" with the value it effectively just sets the alpha part of the alpha/red number to 0, leaving us with just the red part that we wanted in the first place.
Re: What is ">>" and how to read i
Reply #2 - Mar 13th, 2008, 12:15am
 
I've been wondering about the ">>"  as well.

JohnG, that's one amazingly simple and clear description you've delivered there. Awesome!
Re: What is ">>" and how to read i
Reply #3 - Apr 25th, 2008, 2:15am
 
Hi,
That is an excellent description John, I was wondering how one might reverse the process? I have an rgb that I want to turn back into a pixel value.

Thanks a lot!
Alan

EDIT: upon thinking about this, do you just do it using the color() function?

color(r,g,b)?
Re: What is ">>" and how to read i
Reply #4 - Apr 25th, 2008, 9:13am
 
You can just use color, or there's << to do the reverse of >>

Code:
int alpha=255;
int r=128;
int g=64;
int b=255;

color c=(alpha<<24)+(r<<16)+(g<<8)+b;
Page Index Toggle Pages: 1