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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:21:18+00:00 2026-06-18T02:21:18+00:00

This is what I need to do: int lg(int v) { int r =

  • 0

This is what I need to do:

int lg(int v)
{
    int r = 0;
    while (v >>= 1) // unroll for more speed...
    {
        r++;
    }
}

I found the above solution at: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog

This works, butI need to do it without loops, control structures, or constants bigger than 0xFF (255), which has proven to be very hard for me to find. I’ve been trying to figure something out using conditionals in the form

( x ? y : z ) = (((~(!!x) + 1)) & y) | ((~(~(!!x) + 1)) & z)

but I can’t get it to work. Thanks for your time.

  • 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-18T02:21:19+00:00Added an answer on June 18, 2026 at 2:21 am

    Without any control structure, not even the ?: operator, you can simulate your own algo

    int r = 0;
    
    x >>= 1;
    r += (x != 0);
    x >>= 1;
    r += (x != 0);
    ...
    

    provided that, in C,

    • x is assumed to be positive (otherwise having int x=-1; for instance x >>= 1 n times is always != 0
    • a condition like x != 0 returns 0 (false) or 1 (*true)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert this for loop to a do while: for (int i
I'm getting TypeError: coercing to Unicode: need string or buffer, int found This is
I need to get UserCarId(int) from this query and assign to int type variable.
I need to have only one button in showConfirmDialog . I tried this: int
I'm in a need to optimize this really tiny, but pesky function. unsigned umod(int
I can do loop with more then one condition like this: for (int i
This need may sound a little convoluted, and if so, then I am open
I have this need to place my app in the share/send context menu so
So I have this need to import (currently) about 15k products. Each product has
I need this urgently in my Django site, but because of the time constraint,

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.