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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:27:36+00:00 2026-06-17T14:27:36+00:00

I have a byte I’m using for bitflags. I know that one and only

  • 0

I have a byte I’m using for bitflags. I know that one and only one bit in the byte is set at any give time.

Ex: unsigned char b = 0x20; //(00100000) 6th most bit set

I currently use the following loop to determine which bit is set:

int getSetBitLocation(unsigned char b) {
  int i=0;
  while( !((b >> i++) & 0x01) ) { ; }
  return i;
}

How do I most efficiently determine the position of the set bit? Can I do this without iteration?

  • 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-17T14:27:38+00:00Added an answer on June 17, 2026 at 2:27 pm

    Can I do this without iteration?

    It is indeed possible.

    How do I most efficiently determine the position of the set bit?

    You can try this algorithm. It splits the char in half to search for the top bit, shifting to the low half each time:

    int getTopSetBit(unsigned char b) {
      int res = 0;
      if(b>15){
        b = b >> 4;
        res = res + 4;
      }
      if(b>3){
        b = b >> 2;
        res = res + 2;
      }
    
      //thanks @JasonD
      return res + (b>>1);
    }
    

    It uses two comparisons (three for uint16s, four for uint32s…). and it might be faster than your loop. It is definitely not shorter.


    Based on the idea by Anton Kovalenko (hashed lookup) and the comment by 6502 (division is slow), I also suggest this implementation (8-bit => 3-bit hash using a de-Bruijn sequence)

    int[] lookup = {7, 0, 5, 1, 6, 4, 3, 2};
    
    int getBitPosition(unsigned char b) {
      // return lookup[(b | (b>>1) | (b>>2) | (b>>4)) & 0x7];
      return lookup[((b * 0x1D) >> 4) & 0x7];
    }
    

    or (larger LUT, but uses just three terms instead of four)

    int[] lookup = {0xFF, 0, 1, 4, 2, 0xFF, 5, 0xFF, 7, 3, 0xFF, 0xFF, 6, 0xFF, 0xFF, 0xFF};
    
    int getBitPosition(unsigned char b) {
      return lookup[(b | (b>>3) | (b>>4)) & 0xF];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a byte[] that i obtained using Object ArrayList<Obj> Can anyone tell me
I have a byte I am using to store bit flags. I have 8
I have a byte (unsigned char) array. How to draw it using QPainter?
I have byte ... Dim endframe As Byte now i know that i can
I am making C# windows application.In that application i have one byte array containing
I have 3 byte arrays in C# that I need to combine into one.
I have a byte I am using to store bit flags. I need to
I have a byte stream that may be UTF-8 data or it may be
I have two byte arrays in C# using .NET 3.0. What is the most
I have byte to binary string function, std::string byte_to_binary(unsigned char byte) { int x

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.