Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8262553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:47:32+00:00 2026-06-08T03:47:32+00:00

The PHP imagecolorat() function can be used to get the RGB values of an

  • 0

The PHP imagecolorat() function can be used to get the RGB values of an image pixel, as demonstrated in the documentation:

$im = imagecreatefrompng("php.png");
$rgb = imagecolorat($im, 10, 15);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

I don’t understand the last three lines at all; I know that they return the correct values, but I cannot figure out how the >> and & operators work together to do so. Could someone please explain?

For reference, when $rgb = 9750526, the RGB values turn out to be(148,199,254).

Thanks for any responses.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:47:35+00:00Added an answer on June 8, 2026 at 3:47 am

    & and >> are two different bitwise operators (Bitwise operators in PHP). They work with the bit representation of the values, which gives us a few useful (and fast!) possibilities.

    >> is right shift. If you right shift a value, you’re moving all the bits to the right in the representation. If you have “11100” and right shift it by two places, you’re left with “111” (the bits shifted off on the right end disappears). The number on the right side of >> indicates the number of bits to shift.

    & is bitwise and. If you have two values represented as 111000 and 101011, and decide to bitwise and them, you’ll end up with 101000 (the bits where the same bits in both values are set will be 1, otherwise 0).

    We now have what we need to make sense of the code above. $rgb contains an integer value (the value is not interesting, but the bit pattern is), which represents 24 bits (32 bits for RGBA, but we’ll ignore that here). The value consists of 8 bits for red, 8 bits for green and 8 bits for blue. We’re however not interested in what number those 24 bits represent together, but what the separate R, G and B values are.

    Our value represent the following bits for the colors:

    rrrrrrrr gggggggg bbbbbbbb 
    

    If we shift this bitmask with >> 16, we’ll get:

                      rrrrrrrr
    

    This is what >> 16 does, and leaves us with just the bits representing the red color. As we’ve now moved the bits all the way down, the value represents a number in [0, 255]. We then add & 255 (which here is written as 0xFF in hexadecimal notation) to remove any stray bits above the 8 we’re interested in. We’ll get to how that happens now, when we look at how we get the G value:

    Our value still bravely represents the different colors:

    rrrrrrrr gggggggg bbbbbbbb 
    

    If we shift this bitmask 8 places with >> 8, we’ll get:

             rrrrrrrr gggggggg
    

    But wait! This is not just the bits that represents g, but also the bits that represent r! This is not the value we’re after (as we just want g’s). We were lucky in the previous example since we didn’t have any bits set above r, but this time we’ve really got our work cut out for us. But we know about &, and it’s time to see it actually to it’s stuff.

    We now have:

             rrrrrrrr gggggggg
    

    And then we apply & 255 (0xFF in your code), which is representing in a bit value as:

             00000000 11111111
    

    Since & only keeps the bits which are set in both operands, we’ll end up with just the g values:

                      gggggggg
    

    The bits that represented red in the shifted value are now 0, and we’re left with only the bits representing the green of the original color. As they’ve been shifter into the same 8 bits that represent [0, 255], we’ve got our value yet again.

    The last value is easier, as it’s already located where we want it: in the bits representing [0, 255]. We can just do the & operation here, and get out on the other side:

    rrrrrrrr gggggggg bbbbbbbb & 
    00000000 00000000 11111111
    

    Which ends up with:

                      bbbbbbbb
    

    And we’ve got our blue value as well.

    Hope that explains it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get rgb color info from every pixel using imagecolorat() and I'm
I'm trying to get average of RGB color of Image in php. by gd
How can I check for a pixel pattern in PHP? I mean I wanna
My code so far (it creates an overlay to a youtube thumbnail): <?php header(Content-type:image/png);
I have created a captcha image making program with PHP <?php function word($n) {
How can i find in my image the darkest pixel, which occurence in image
PHP's mkdir returns false on failure. Failure can be: Folder already exists Some other
PHP: I have made up a function that returns an array. I would like
PHP 5.2.15 I am trying to replace {date[F]} with the date function. I have
I'd like to change the hue of an image using the PHP image processing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.