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

  • Home
  • SEARCH
  • 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 9214787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:03:05+00:00 2026-06-18T02:03:05+00:00

This is a code i am seeing for some time. i wonder how the

  • 0

This is a code i am seeing for some time.
i wonder how the code works.

public static final int MULTI = 1 << 1;
public static final int SINGLE = 1 << 2;
public static final int READ_ONLY = 1 << 3;
SWT.MULTI | SWT.SINGLE | SWT.READ_ONLY

i went deep into the class implementation of TableViewer(Composite parent, int style) in search for the answer but didnt find much.

I found this code, but didnt understand much

static int checkBits (int style, int int0, int int1, int int2, int int3, int int4, int int5) {
int mask = int0 | int1 | int2 | int3 | int4 | int5;
if ((style & mask) == 0) style |= int0;
if ((style & int0) != 0) style = (style & ~mask) | int0;
if ((style & int1) != 0) style = (style & ~mask) | int1;
if ((style & int2) != 0) style = (style & ~mask) | int2;
if ((style & int3) != 0) style = (style & ~mask) | int3;
if ((style & int4) != 0) style = (style & ~mask) | int4;
if ((style & int5) != 0) style = (style & ~mask) | int5;
return style;
}
  • 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-18T02:03:07+00:00Added an answer on June 18, 2026 at 2:03 am

    Packing Bits Together

    Let’s take a look at this code.

    public static final int MULTI = 1 << 1;
    public static final int SINGLE = 1 << 2;
    public static final int READ_ONLY = 1 << 3;
    

    The “<<” operator means shift the bits to the left. The number after the “<<” operator tells us how many bits to shift.

    So, another way of writing the code would be.

    public static final int MULTI = 2;
    public static final int SINGLE = 4;
    public static final int READ_ONLY = 8;
    

    By defining the flags this way, the values can be OR’d (“added”) together and saved in one byte or one integer.

    SWT.MULTI | SWT.SINGLE
    

    This is saying to OR the bits of the two values together. But what does that mean?

    In this case, since we’ve defined our values as a single bit, it’s the same as adding the values.

    MULTI = 2
    SINGLE = 4
    

    Therefore, the status value is 6 (2 + 4).

    Now remember, we’re not adding. Because we’re using bits, the effect is the same as if we’re adding.

    Unpacking Bits

    The second bit of code you’ve posted basically takes the 6 that we came up with, and splits it back into 2 and 4. It works by checking each bit, one at a time, and seeing if it’s present.

    Let’s take one line, and see what it’s doing.

    if ((style & int1) != 0) style = (style & ~mask) | int1;
    

    int1 represents one of the style bits. For the sake of this discussion, let’s say that it is MULTI with a value of 2.

    The first part (the if condition) tests for whether or not the bit is set in the style integer.

    The second part is a bit trickier. (Bit, get it. Very punny.)

    The value of mask is given in the code.

    int mask = int0 | int1 | int2 | int3 | int4 | int5;
    

    This is just all of the status values OR’d together. From our original example of MULTI, SINGLE, and READ_ONLY, that gives us a mask of x’0E’ or 14. Through the rest of this discussion, I’ll be using hexadecimal.

    Status Bit Present

    Now, back to the line we’re talking about.

    if ((style & int1) != 0) style = (style & ~mask) | int1;
    

    style & int1 is an AND operation. That means the bit has to be set in both style and int1. Style is x’06’ (2 + 4) from when we set it before. int1 is x’02’. When you AND x’06’ and x’02’, you get x’02’, which makes the value of the if true.

    ~mask converts the x’0E’ into x’F1′. In other words, all of the bits are flipped from 0 to 1 and 1 to 0.

    style & ~mask is an AND operation. When you AND x’06’ and x’F1′, you get x’00’. In other words, none of the bits match up.

    We said earlier that int1 is MULTI, with the value of 2 or x’02’. Now we do an OR operation, which we explained before. ORing x’00’ and x’02’ gives us x’02’, which is the MULTI value that we wanted to extract.

    Status Bit Not Present

    The other alternative (when the status bit is not present) leads to a slightly different result. We didn’t set READ_ONLY, so lets go through that calculation.

    READ_ONLY is x’08’. Style is x’06’ When you AND those values together in the if

    if ((style & int1) != 0) style = (style & ~mask) | int1;
    

    you get x’00’, which causes the value of the if to be false.

    Justification

    The original reason for putting several status values into one byte or word was to save memory. This was important 40 years ago when computer memory was limited. Now, it’s done for convenience in passing status around. Rather than passing 7 or 15 different status indicators from one method to another, you pass one status indicator.

    The tradeoff, as you have seen, is that it takes a bit of code to extract the status bits so you can use them.

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

Sidebar

Related Questions

I'm seeing this code in a project and I wonder if it is safe
I have been seeing code like this usually in the start of header files:
Kept on seeing this pattern in code, but couldn't find any reference to it
I have seen code like this (actually seeing another person type it up): catch
I'm trying to learn about this feature of javascript I keep seeing in code,
This code is the core of a much larger script that works great in
I remember, some time ago, seeing an example on MSDN of how you could
I have been setting up builds for quite some time now. To do this,
I have the following code in some app: int lowRange=50; int[] ageRangeIndividual = {6,
I would like to measure the system time it takes to execute some code.

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.