The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

what does that exactly mean?

Posted by Matt Gerrans on October 10, 2001 at 5:24 PM

What this means is that the 32 bits of information in the pixel integer is diveded evenly into four chunks of 8 bits, like so:
aaaaaaaarrrrrrrrggggggggbbbbbbbb

The >> operator shifts the bits to the right by the specified amount (the number on the right side). The "& 0xff" is a mask (the & is a bitwise and operator), and it blanks (zeros) out everything except the lowest 8 bits. So the effect of the combined operation of shifting right by 8 (or 16, or 24) bits and then masking is to look at a specific chunk of 8 bits within the 32-bit field.

So if we did it in the opposite order that you specified, for illustative purposes, it would be something like this:


int p = _pixels[index];

// Look at the lower 8 bits:
// No shift, so:
// aaaaaaaarrrrrrrrggggggggbbbbbbbb -> aaaaaaaarrrrrrrrggggggggbbbbbbbb
// Result of masking:
// aaaaaaaarrrrrrrrggggggggbbbbbbbb -> 000000000000000000000000bbbbbbbb
_blue = (p) & 0xff;

// Look next group 8 bits:
// After shift right 8:
// aaaaaaaarrrrrrrrggggggggbbbbbbbb -> ????????aaaaaaaarrrrrrrrgggggggg
// Result of masking:
// ????????aaaaaaaarrrrrrrrgggggggg -> 000000000000000000000000gggggggg
_green = (p >> 8) & 0xff;

// Look next group 8 bits:
// After shift right 16:
// aaaaaaaarrrrrrrrggggggggbbbbbbbb -> ????????????????aaaaaaaarrrrrrrr
// Result of masking:
// ????????????????aaaaaaaarrrrrrrr -> 000000000000000000000000rrrrrrrr
_red = (p >> 16) & 0xff;

// Look last group 8 bits:
// After shift right 24:
// aaaaaaaarrrrrrrrggggggggbbbbbbbb -> ????????????????????????aaaaaaaa
// Result of masking:
// ?????????????????????????aaaaaaaa -> 00000000000000000000000aaaaaaaa
_alpha = (p >> 24) & 0xff;

> hi!
> Is there anyone who can explain me what this exactly means...
> (i know the result...but cant understand how it comes to it)
> using the pixelgrabber class i grab a pixel...
> and extract the colors....hmm...i copied this from an example...and dont understand what the operators mean and what for is everything
> int p=_pixels[index];
> _alpha = (p >> 24) & 0xff;
> _red = (p >> 16) & 0xff;
> _green = (p >> 8) & 0xff;
> _blue = (p ) & 0xff;
> i get the right values for the colors... but how??
> what does the '>>' mean and what for is '&', how do they exactly work?
> thx
> -ilya






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us