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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:19:23+00:00 2026-06-15T16:19:23+00:00

I have a function which checks, whether a bit in an int is set

  • 0

I have a function which checks, whether a bit in an int is set or not.
But I think there will be a much faster implementation, since this one is linear and can’t be the most efficient one, although I know the int should be between 1 and 1024.

 public static int getBitPos(final int n) {
        if (Integer.bitCount(n) != 1)
            return Constants.UNDEFINED;
        else {
            for (int i = 0; i < Integer.MAX_VALUE; ++i) {
                if (testBit(n, i))
                    return i;
            }
        }
        return Constants.UNDEFINED;
    }

Where testBit is the following standard function:

public static boolean testBit(final int n, final int pos) {
    int mask = 1 << pos;
    return (n & mask) == mask;
}

But there mast be a faster way, isn’t there? If I have the value 17 and I want to know if the 4th bit (n = 8) is set? There should be a faster way to check whether the bit for n=8 is set…

Hope you can help me…

EDIT 1:
Thanks for the support. The comments and answers brought me to my mistake. I was setting the values wrongly, which made it more complicated than needed. I was never good at bit shifting.
I set the value like this, if I wanted the second bit to be set:

value = 2;

If I wanted the 4th bit to be set too, I added the value according to the 4th bit:

value += 8;

So value was 10, and the 2nd and 4th bit were set. So I saved the numbers in my class, instead of the bit-positions (8 as value, instead of 4 for the 4th bit, …).
After changing this, I could get rid of my unnecessary function, which was way over the top!
Thanks for all help!

  • 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-15T16:19:24+00:00Added an answer on June 15, 2026 at 4:19 pm

    Your code always returns the lowest bit that is 1, if there is only one. You can achieve the same by doing this:

    int foo = whatever;
    int lowestSetBit = Integer.numberOfTrailingZeros(foo) + 1;
    

    Your code would be

    public static int getBitPos(final int n) {
        if (Integer.bitCount(n) == 1)
            return Integer.numberOfTrailingZeros(n) + 1;
        return Constants.UNDEFINED;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written following function which checks whether start_date field is not empty and
I have an XSLT function which checks whether the sent parameter is in YYYYMMDD
In a YII work, I have an init() function which checks whether the session
I have a function like this which checks whether the field is empty or
I have the following function which I use to check whether or not an
I have a function which checks the passed value and returns false or true,
I have a function which I have to loop it a few times but
I have 3 ajax call in one function and checkAjaxCompletion which checks each ajax
I have a javascript function which supposed to check whether a task is completed.
We have a function in PHP, in_array , which checks if the given value

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.