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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:54:47+00:00 2026-05-23T07:54:47+00:00

I am copy the double value to the vector of unsigned char. It will

  • 0

I am copy the double value to the vector of unsigned char. It will be my buffer for socket, which should contain values of different types.
I wanted to test if the coping works correct. I wrote a small code.

vector<unsigned char> buffer(8);
double doubleValue_1 = 8.0;
memcpy(&buffer[0], &doubleValue_1, sizeof( doubleValue_1));

bitset<64>bitset_1(doubleValue_1);
cout << "doubleValue_1: " << bitset_1 << endl;

cout << "buffer: ";
for (int i = 0; i < buffer.size(); i++) {
    bitset<8>bitset_v(buffer.at(i));
    cout << bitset_v;
}

cout << endl << endl;

double doubleValue_2;
memcpy(&doubleValue_2, &buffer[0], sizeof(doubleValue_2));

bitset<64>bitset_2(doubleValue_2);
cout << "doubleValue_2: " << bitset_2 << endl;

And I got the output:

doubleValue_1: 0000000000000000000000000000000000000000000000000000000000001000
buffer:        0000000000000000000000000000000000000000000000000010000001000000
doubleValue_2: 0000000000000000000000000000000000000000000000000000000000001000

How you can see, the first doubleValue_1 represented 8.0. Than I copied this value to vector. And the output is NOT equal to the previous one. But when I coped bytes from vector to double back, I got in doubleValue_2 the same value as in doubleValue_1.
My question is, why the output of vector is different than of double? Maybe I print out the vector in wrong way. Could you suggest me how I can do it correctly?
Thank you a lot.

  • 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-23T07:54:47+00:00Added an answer on May 23, 2026 at 7:54 am

    bitset<64>bitset_1(doubleValue_1); is casting your double to an unsigned long (because that is what the bitset<64> constructor takes.)

    Similarly for bitset<64>bitset_2(doubleValue_2);.

    Neither one is actually winding up with the bit representation for a double.

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

Sidebar

Related Questions

I am using vector<char> to send and receive data via socket. In this vector
Array.Copy and Buffer.BlockCopy both do the same thing, but BlockCopy is aimed at fast
I have a class Step derived from std::vector<unsigned int> . I need to overload
Take a simple class with the big 3 (constructor, copy constructor, destructor): #include <vector>
i am using reflection to copy values of fields from object of Class A
I've a data structure as follows typedef vector<double> v_t; typedef set<int> s_t; typedef map<s_t,
in the following snippet I wish the function accept a double pointer(2D array) which
Possible Duplicate: Why should the copy constructor accept its parameter by reference in C++?
I made a function for an object called copy() that should just return an
If have a union that currently foresees some simple data types: union Value {

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.