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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:26:13+00:00 2026-05-28T19:26:13+00:00

Given an unsigned int, I have to implement the following operations : Count the

  • 0

Given an unsigned int, I have to implement the following operations :

  1. Count the number of bits set to 1
  2. Find the index of the left-most 1 bit
  3. Find the index of the righ-most 1 bit

(the operation should not be architecture dependents).

I’ve done this using bitwise shift, but I have to iterate through almost all the bits(es.32) .
For example, counting 1’s:

unsigned int number= ...;
while(number != 0){
    if ((number & 0x01) != 0)
        ++count;
    number >>=1;
}

The others operation are similar.

So my question is: is there any faster way to do that?

  • 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-28T19:26:14+00:00Added an answer on May 28, 2026 at 7:26 pm

    If you want the fastest way, you will need to use non-portable methods.

    Windows/MSVC:

    • _BitScanForward()
    • _BitScanReverse()
    • __popcnt()

    GCC:

    • __builtin_ffs()
    • __builtin_ctz()
    • __builtin_clz()
    • __builtin_popcount()

    These typically map directly to native hardware instructions. So it doesn’t get much faster than these.

    But since there’s no C/C++ functionality for them, they’re only accessible via compiler intrinsics.

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

Sidebar

Related Questions

I have this buffer given: unsigned char *buffer; int buffer_length; This is how I
Given: unsigned int a, b, c, d; I want: d = a * b
Given an integer typedef: typedef unsigned int TYPE; or typedef unsigned long TYPE; I
Given std::vector<CMyClass> objects; CMyClass list[MAX_OBJECT_COUNT]; Is it wise to do this? for(unsigned int i
Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
I was given the assignment to create a program that performs basic set operations.
Given an unsigned int A (32 bit), and another unsigned int B, where B's
In my makefile I have the following target, which compiles text/HTML resources into unsigned
I have seen and used C++ code like the following: int myFourcc = 'ABCD';
Given that signed and unsigned ints use the same registers, etc., and just interpret

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.