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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:44:59+00:00 2026-06-09T00:44:59+00:00

Say I have a floating point number. I would like to extract the positions

  • 0

Say I have a floating point number. I would like to extract the positions of all the ones digits in the number’s base 2 representation.

For example, 10.25 = 2^-2 + 2^1 + 2^3, so its base-2 ones positions are {-2, 1, 3}.

Once I have the list of base-2 powers of a number n, the following should always return true (in pseudocode).

sum = 0
for power in powers:
    sum += 2.0 ** power
return n == sum

However, it is somewhat difficult to perform bit logic on floats in C and C++, and even more difficult to be portable.

How would one implement this in either of the languages with a small number of CPU instructions?

  • 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-09T00:45:01+00:00Added an answer on June 9, 2026 at 12:45 am

    Give up on portability, assume IEEE float and 32-bit int.

    // Doesn't check for NaN or denormalized.
    // Left as an exercise for the reader.
    void pbits(float x)
    {
        union {
            float f;
            unsigned i;
        } u;
        int sign, mantissa, exponent, i;
        u.f = x;
        sign = u.i >> 31;
        exponent = ((u.i >> 23) & 255) - 127;
        mantissa = (u.i & ((1 << 23) - 1)) | (1 << 23);
        for (i = 0; i < 24; ++i) {
            if (mantissa & (1 << (23 - i)))
                printf("2^%d\n", exponent - i);
        }
    }
    

    This will print out the powers of two that sum to the given floating point number. For example,

    $ ./a.out 156
    2^7
    2^4
    2^3
    2^2
    $ ./a.out 0.3333333333333333333333333
    2^-2
    2^-4
    2^-6
    2^-8
    2^-10
    2^-12
    2^-14
    2^-16
    2^-18
    2^-20
    2^-22
    2^-24
    2^-25
    

    You can see how 1/3 is rounded up, which is not intuitive since we would always round it down in decimal, no matter how many decimal places we use.

    Footnote: Don’t do the following:

    float x = ...;
    unsigned i = *(unsigned *) &x; // no
    

    The trick with the union is far less likely to generate warnings or confuse the compiler.

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

Sidebar

Related Questions

Given an arbitrary floating point number, say -0.13 , suppose we have an algorithm
Say you have 100000000 32-bit floating point values in an array, and each of
Let's say I have some code that does some floating point arithmetic and stores
Let's say I have three 32-bit floating point values, a , b , and
Let's say I have an array of floating point numbers, in sorted (let's say
If I have an extremely long floating point number in Ruby such as: x
For example, I have an NSDecimal myDecimal. Lets say it represents something like -1234567.89
Firstly, I would like to say that I have tested if my link to
Say we have deep hashes like: b = {1 => {2 => {} },
Say i have a few fields like the following: abd738927 jaksm234234 hfk342 ndma0834 jon99322

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.