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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:56:05+00:00 2026-05-27T14:56:05+00:00

Given double x , and assuming that it lies in [0,1] . Assume for

  • 0

Given double x, and assuming that it lies in [0,1] . Assume for example that x=0.3
In binary, (keeping 10 digits after the decimal point), it is represented as

x=0.0100110011...

I want to write some C++ code which will extract the 10 digits shown after the decimal point. In other words I want to extract the integer (0100110011)_2.

Now I am quite new to bit shifting and the (naive) solution which I have for the problem is the following

int temp= (int) (x*(1<<10))

Then temp in binary will have the necesary 10 digits.

Is this a safe way to perform the above process? OR are there safer / more correct ways to do this?

Note: I don’t want the digits extracted in the form of a character array. I specifically want an integer (OR unsigned integer) for this. The reason for doing this is that in generation of octrees, points in space are given hash keys based on their position named as Morton Keys. These keys are usually stored as integers. After getting the integr keys for all the points they are then sorted. Theoretically these keys can be obtained by scaling the coordinates to [0,1], extracting the bits , and interleaving them.

  • 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-27T14:56:06+00:00Added an answer on May 27, 2026 at 2:56 pm

    Use memcpy to copy double into an array of 32-bit numbers, like this:

    unsigned int b[2]; // assume int is 32-bits
    memcpy(b, &x, 8);
    

    The most 10 significant binary digits are in b[0] or b[1], depending on whether your machine is big- or little-endian.

    EDIT: The same can be achieved by some casting instead of memcpy, but that would violate strict aliasing rules. An alternative is to use a union.

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

Sidebar

Related Questions

It is easy given a container to get the associated iterators, example: std::vector<double>::iterator i;
How can I convert a double-precision hexadecimal back into a double? For example, given
In C# I want a function that rounds a given double to a given
Given a struct definition that contains one double and three int variables (4 variables
Given certain multibyte character sets, am I correct in assuming that the following doesn't
Given Wikipedia's discussion of Double Factorial s, can anyone suggest where I might find
Given the following declaration: double x; What is the value of x ? Is
Given a double dimension matrix with all positive numbers how do I traverse the
I've got a weird linker issue. I have code that looks like so: double
I need a Perl regular expression to match a string. I'm assuming only double-quoted

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.