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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:01:55+00:00 2026-05-16T11:01:55+00:00

I have a 64-bit unsigned integer with exactly 1 bit set. I’d like to

  • 0

I have a 64-bit unsigned integer with exactly 1 bit set. I’d like to assign a value to each of the possible 64 values (in this case, the odd primes, so 0x1 corresponds to 3, 0x2 corresponds to 5, …, 0x8000000000000000 corresponds to 313).

It seems like the best way would be to convert 1 → 0, 2 → 1, 4 → 2, 8 → 3, …, 263 → 63 and look up the values in an array. But even if that’s so, I’m not sure what the fastest way to get at the binary exponent is. And there may be more efficient ways, still.

This operation will be used 1014 to 1016 times, so performance is a serious issue.

  • 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-16T11:01:56+00:00Added an answer on May 16, 2026 at 11:01 am

    If performance is a serious issue, then you should use intrinsics/builtins to use CPU specific instructions, such as the ones found here for GCC:

    http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Other-Builtins.html

    • Built-in function int __builtin_ffs(unsigned int x).

      Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.

    • Built-in function int __builtin_clz(unsigned int x).

      Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.

    • Built-in function int __builtin_ctz(unsigned int x).

      Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined.

    Things like this are the core of many O(1) algorithms, such as kernel schedulers which need to find the first non-empty queue signified by an array of bits.

    Note: I’ve listed the unsigned int versions, but GCC has unsigned long long versions, as well.

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

Sidebar

Related Questions

Say we have an 8-bit unsigned integer n ( UINT8_MAX=255 ); what is the
I have a byte[4] which contains a 32-bit unsigned integer (in big endian order)
I have values that are 64-bit unsigned ints, and I need to store them
You have an ASCII string representing a 128-bit unsigned integer number n, i.e. 0
I have a code pattern which translates one integer to another. Just like this:
I have an unsigned 16-bit integer in big endian byte order: 0x01f1 but they
I have a class whose only data member is a 32 bit unsigned integer.
I have an image file that is a grayscale 8 bit unsigned integer raw
I have a database table which contains an unsigned integer field to store the
I have a raw image where each pixel corresponds to a 16 bits unsigned

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.