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

The Archive Base Latest Questions

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

I am currently trying to write an algorithm that determines how many bits are

  • 0

I am currently trying to write an algorithm that determines how many bits are necessary to represent a number x. My implementation will be in c.
There are a few catches though, I am restricted to pretty much just the bitwise operators {~, &, ^, |, +, <<, >>}. Also, I cannot use any type of control flow (if, while, for).
My original approach was to examine the number in binary from left to right, and look for where there is an occurrence of the first ‘1’. I am not sure how to approach this given the restrictions I have.
The number I am working with can be considered an unsigned integer. So 00110 would require only 3 bits.

I am wondering if there is a much easier/cleaner way to do this and I am missing it?
Or if someone can give a few hints?

Basically, I was trying to implement this without the while loop:

 int result = 0;
  while (x >>= 1) {
    result += 1;
  }
  return result;
  • 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:43:48+00:00Added an answer on May 28, 2026 at 7:43 pm

    http://www-graphics.stanford.edu/~seander/bithacks.html#IntegerLog

    Shows how to do it without control flow.

    unsigned int v;          // 32-bit value to find the log2 of 
    register unsigned int r; // result of log2(v) will go here
    register unsigned int shift;
    
    r =     (v > 0xFFFF) << 4; v >>= r;
    shift = (v > 0xFF  ) << 3; v >>= shift; r |= shift;
    shift = (v > 0xF   ) << 2; v >>= shift; r |= shift;
    shift = (v > 0x3   ) << 1; v >>= shift; r |= shift;
                                            r |= (v >> 1);
    r++;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to write an addin for PowerPoint that whenever any PowerPoint
I am currently trying to write a very simple app that sends an object
I'm currently trying to write a simple script that looks in a folder, and
I am currently trying to write a program that alphabetizes strings. I have one
I wrote a simple coin change algorithm that currently finds the minimum number of
I'm trying to write some Python code that will traverse each directory in the
I'm currently trying to write a program in VB.NET which fluidly changes the DWM
I am currently trying to write functional tests for a charging form which gets
Currently I'm trying to write my first Python library and I've encountered the following
currently I am trying to write to an 20x4 LCD Dot-Matrix Module using Perle's

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.