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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:47:48+00:00 2026-05-14T22:47:48+00:00

When interviewing new candidates, we usually ask them to write a piece of C

  • 0

When interviewing new candidates, we usually ask them to write a piece of C code to count the number of bits with value 1 in a given byte variable (e.g. the byte 3 has two 1-bits). I know all the common answers, such as right shifting eight times, or indexing constant table of 256 precomputed results.

But, is there a smarter way without using the precomputed table? What is the shortest combination of byte operations (AND, OR, XOR, +, -, binary negation, left and right shift) which computes the number of 1-bits?

  • 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-14T22:47:49+00:00Added an answer on May 14, 2026 at 10:47 pm

    There are at least two faster solutions, with different performance characteristics:

    1. Subtract one and AND the new and old values. Repeat until zero. Count the number of iterations. Complexity: O(B), where B is the number of one-bits.

      int bits(unsigned n)
      {
          for (int i = 0; n; ++i)
          {
              n &= n - 1;
          }
          return i;
      }
      
    2. Add pairs of bits then groups of four, then groups of eight, till you reach the word size. There’s a trick that lets you add all groups at each level in a single pass. Complexity: O(log(N)), where N is the total number of bits.

      int bits(uint32 n)
      {
          n = (n & 0x55555555) + ((n >>  1) & 0x55555555);
          n = (n & 0x33333333) + ((n >>  2) & 0x33333333);
          n = (n & 0x0f0f0f0f) + ((n >>  4) & 0x0f0f0f0f);
          n = (n & 0x00ff00ff) + ((n >>  8) & 0x00ff00ff);
          n = (n & 0x0000ffff) + (n >> 16);
          return n;
      }
      

      This version is a bit naive. If you think about it a bit, you can avoid some of the operations.

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

Sidebar

Related Questions

While interviewing for a company, I was asked to submit several code samples that
I am interviewing candidates for a position developing an application which relies heavily on
I am interviewing candidates for a role that centres around user experience. The issue
I am new to the area of web development and currently interviewing companies, the
We are interviewing new developers looking for someone with some hibernate experience. We are
I plan on interviewing Flex candidates in the near future and I'm looking for
I was interviewing a guy for a mid-level software engineering position yesterday, and he
I've been interviewing a lot of .NET programmers lately and I haven't met one
I was asking the following question during interviewing in a company working on cloud
I'm currently reading Coders at Work, and I'm at the chapter interviewing Brendan Eich.

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.