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 6770017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:15:44+00:00 2026-05-26T15:15:44+00:00

Im not familiar with bitwise operations. This is an image manipulation algorithm. What is

  • 0

Im not familiar with bitwise operations. This is an image manipulation algorithm. What is happening?

void binarize50(int pixels[]) {
        for(int i = 0; i < pixels.length; i++) {
            int gray = ((pixels[i] & 0xff) + ((pixels[i] & 0xff00) >> 8) + ((pixels[i] & 0xff0000) >> 16)) / 3;
            pixels[i] = gray < 128 ? 0xff000000 : 0xffffffff;
        }
}

I found out that (pixels[i] & 0xff) just “normalizes” somehow and let the pixel/byte become a positive value between 0..255.

What does (pixels[i] & 0xff00 >> 8) do?
What is 0xff00?
And why if grey < 128 the pixel becomes 0xff000000 or else 0xffffffff?

  • 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-05-26T15:15:44+00:00Added an answer on May 26, 2026 at 3:15 pm

    The & operation with the 0xff and the like applies a bitmask. It’s a bitwise and operation. Since an int in Java is 32-bit, you can read 0xff as 00000000_00000000_00000000_11111111, 0xff00 as 00000000_00000000_11111111_00000000 and so on. They just omit the leading zeroes.

    So if you do pixels[i] & 0xff, what’s happening is that you get an int of which the last 8 bits are the same as in pixels[i] with the remaining ones set to zero.

    The >> operator is the right shift. It will shift the bit pattern to the right over the number of bits indicated. If b contained 00110011_11001100_00010110_01001100 and you did b >> 8, you’d end up with 00000000_00110011_11001100_00010110. The last 8 bits “dropped off”, while to the left zeroes have been shifted in. I don’t remember if this operation will shift in 1’s to the left if the leading bit was 1, so maybe someone can confirm or deny that.

    Knowing this, let’s take a look at this line:

    int gray = ((pixels[i] & 0xff) + ((pixels[i] & 0xff00) >> 8) + ((pixels[i] & 0xff0000) >> 16)) / 3;
    

    What happens here is that we make an int as follows (not exact code execution order, just for illustration):

    • pixels[i] is masked so only the last 8 bits are retained, the rest become 0.
    • pixels[i] is masked so only the bits 8 – 15 (counting right to left, starting from 0) are retained, the rest become 0. That result is then shifted 8 bits to the right. If we started with 00001111_00001111_10101010_00110011, this would result in 00000000_00000000_00000000_10101010.
    • pixels[i] is masked so only the bits 16 – 23 are retained, then shifted 16 bits to the right.
    • the results of the above three operations are added and…
    • that result is divided by 3.

    So what does this achieve? Well, what it basically comes down to is that the first 8 bits of pixels[i] are ignored, and the following 3 sections of 8 bits are each interpreted as a single value between 0 and 255 of which the average is taken.

    Then that result is checked against 128. If it’s lower, pixels[i] i set to 0xff000000, otherwise it’s set to 0xffffffff.

    This is typical bit twiddling you’d encounter in operations on colors coded as ints. This is probably the argb scheme, where the first 8 bits of an int are the alpha (transparency), the next 8 bits are red, the next 8 are green and the last 8 bits are blue. Or a variant thereof. 0xff000000 would be completely opaque black, while 0xffffffff is completely opaque white.

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

Sidebar

Related Questions

I am not familiar with this website but I am desperately seeking help with
I am not familiar with this, and can use a kick start. I am
I'm not familiar with this programming language,in PHP it's getcwd() .
I have this question because I am not familiar latest generation of MS VS
I'm not familiar with bitwise operators, but I have seem them used to store
I'm not familiar with time operations in javascript. I tried new Date(); which gives
To preface this, I'm not familiar with OLAP at all, so if the terminology
I don't usually work with this type of data layer so I'm not familiar
I believe there is a way to do this, but I'm not familiar with
Not familiar with C, please correct me on any mistakes. here's some code: void

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.