C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to convert a double variable to binary data (bits)? Posted by reivaj7999 on 12 Aug 2005 at 8:32 AM
Hi everyone, how do you do?

I am having this problem, I do not know if it is possible, but I am trying to convert a double variable to binary data(bits).

I tried with the bitset container, but it seems that my compiler does not recognize the bitset.

Any ideas of how I can do this, of course if it is possible?

Thanks guys!
Regards Reivaj

Report
Re: How to convert a double variable to binary data (bits)? Posted by IDK on 12 Aug 2005 at 9:16 AM
: Hi everyone, how do you do?
:
: I am having this problem, I do not know if it is possible, but I am trying to convert a double variable to binary data(bits).
:
: I tried with the bitset container, but it seems that my compiler does not recognize the bitset.
:
: Any ideas of how I can do this, of course if it is possible?
:
: Thanks guys!
: Regards Reivaj

:
Floats are stored a little diferently in memmory than usual data...
Try to get the adress of where the double is stored and then make a byte
access it with the double's address.

Now make it binary.

Niklas Ulvinge aka IDK

Report
Re: How to convert a double variable to binary data (bits)? Posted by reivaj7999 on 12 Aug 2005 at 9:36 AM
This message was edited by reivaj7999 at 2005-8-12 9:49:24

: Floats are stored a little diferently in memmory than usual data...
: Try to get the adress of where the double is stored and then make a byte
: access it with the double's address.
:
: Now make it binary.
:
: Niklas Ulvinge aka IDK
Thank you Niklas,
But how can I accomplish this?
for instance I have this code...
double doubleValue = some_double_value;
// I get the address of doubleValue
double *doubleValueRef = &doubleValue;

...then, how can I make the access?
Regards Reivaj

Report
Re: How to convert a double variable to binary data (bits)? Posted by IDK on 12 Aug 2005 at 11:52 AM
This message was edited by IDK at 2005-8-12 11:55:9

This message was edited by IDK at 2005-8-12 11:54:22

: Thank you Niklas,
: But how can I accomplish this?
: for instance I have this code...
:
: double doubleValue = some_double_value;
: // I get the address of doubleValue
: double *doubleValueRef = &doubleValue;
: 

: ...then, how can I make the access?
: Regards Reivaj

:
Like this:
double doubleValue = some_double_value;
byte *doubleAddress = &doubleValue;
//   at the red arrow ^ I want to get the address of doubleValue to doubleaddress
//make binary...
for(int i = 0; i<sizeof(double); i++) {
  for(int p = 7; p > 0; p++) {
    cout << (Math.Pow(2,p) & &doubleAddress)/Math.Pow(2,p);
  } //right at the red arrow ^ I want to get the value that's stored at the address doubleAddress.
  doubleAddress++;
}

This should be platform independent and you wont need any headers,
except math.h.
I'm not sure of how addresses are working in C/C++... That's why I wrote it in english first.

I hope you got how I've denscibed it here. I usually programming in
assembly and there it's all clear, but here I don't know...

Niklas Ulvinge aka IDK







Report
Re: How to convert a double variable to binary data (bits)? Posted by HK_MP5KPDW on 12 Aug 2005 at 10:29 AM
: Hi everyone, how do you do?
:
: I am having this problem, I do not know if it is possible, but I am trying to convert a double variable to binary data(bits).
:
: I tried with the bitset container, but it seems that my compiler does not recognize the bitset.
:
: Any ideas of how I can do this, of course if it is possible?
:
: Thanks guys!
: Regards Reivaj

:

What errors/warnings does your compiler report in regards to the bitset? The constructors for bitset don't accept a double as an argument so you have to be a bit creative when initializing a bitset with the contents of a double. If you had to use a bitset and assuming 8 byte doubles and 4 byte longs, I think this should work:

double dbl = 49958634.876359;
unsigned long* ulp = reinterpret_cast<unsigned long*>(&dbl);
bitset<64> bits1(*ulp);
bitset<64> bits2(*(++ulp));
bits2 <<= 32;    // Shift values left
bits2 |= bits1;  // Combine this part with other part
cout << bits2 << endl;


Which outputs on my machine:
0100000110000111110100100111011101010111000000101100100010000010


There may be some endian issues depending on your computer. Also, maybe somebody knows of an online converter to validate that.

Report
Re: How to convert a double variable to binary data (bits)? Posted by reivaj7999 on 12 Aug 2005 at 11:10 AM
: What errors/warnings does your compiler report in regards to the bitset? The constructors for bitset don't accept a double as an argument so you have to be a bit creative when initializing a bitset with the contents of a double. If you had to use a bitset and assuming 8 byte doubles and 4 byte longs, I think this should work:
:
:
double dbl = 49958634.876359;
: unsigned long* ulp = reinterpret_cast<unsigned long*>(&dbl);
: bitset<64> bits1(*ulp);
: bitset<64> bits2(*(++ulp));
: bits2 <<= 32;    // Shift values left
: bits2 |= bits1;  // Combine this part with other part
: cout << bits2 << endl;
: 

:
: Which outputs on my machine:
:
0100000110000111110100100111011101010111000000101100100010000010

:
: There may be some endian issues depending on your computer. Also, maybe somebody knows of an online converter to validate that.
:

Thank you, I am working with Borland C++ 5.02 in a AMD AthlonXP 2600+ 2.09GHz and 1.0GB RAM, HD Freespace of 8.0GB, and I am having issues with bitset, here is my code...
#include<iostream>
#include<math>
#include<bitset>

void main()
{
	double value = M_PI;
   unsigned* valueRef = reinterpret_cast<unsigned*>(&value);

   bitset<64> lessSignificative(*valueRef);
   bitset<64> mostSignificative(*(++valueRef));

   mostSignificative <<= 32;
   mostSignificative |= lessSignificative;

   cout << mostSignificative << endl;
}

...and this are the compiler messages:
Info :Compiling C:\Trabajo\Servicio social\Servicio\test.cpp
Warn : string.h(549,3):Functions containing for are not expanded inline
Warn : string.h(557,3):Functions containing while are not expanded inline
Warn : string.h(563,3):Functions containing for are not expanded inline
Warn : string.h(575,3):Functions containing for are not expanded inline
Warn : string.cc(686,32):Comparing signed and unsigned values
Warn : BITSET.h(221,3):Functions containing for are not expanded inline
Warn : BITSET.h(267,3):Functions containing for are not expanded inline
Warn : BITSET.h(273,3):Functions containing for are not expanded inline
Warn : BITSET.h(279,3):Functions containing for are not expanded inline
Warn : BITSET.h(292,3):Functions containing for are not expanded inline
Warn : BITSET.h(305,3):Functions containing for are not expanded inline
Warn : BITSET.h(311,3):Functions containing for are not expanded inline
Warn : BITSET.h(340,3):Functions containing for are not expanded inline
Warn : BITSET.h(380,3):Functions containing for are not expanded inline
Warn : BITSET.h(399,3):Functions containing for are not expanded inline
Warn : BITSET.h(407,3):Functions containing for are not expanded inline
Warn : string.cc(658,2):Cannot create pre-compiled header: code in header
Error: test.cpp(10,11):Undefined symbol 'bitset'
Error: test.cpp(10,14):Ambiguous operators need parentheses
Error: test.cpp(10,33):Call to undefined function 'lessSignificative'
Error: test.cpp(11,14):Ambiguous operators need parentheses
Error: test.cpp(11,33):Call to undefined function 'mostSignificative'
Error: test.cpp(13,25):Illegal use of pointer
Error: test.cpp(14,24):Illegal use of pointer
Warn : test.cpp(17,2):'valueRef' is assigned a value that is never used
Is anything wrong with my code? Please Help!!!
Regards Reivaj

Report
Re: How to convert a double variable to binary data (bits)? Posted by HK_MP5KPDW on 12 Aug 2005 at 11:37 AM
: : What errors/warnings does your compiler report in regards to the bitset? The constructors for bitset don't accept a double as an argument so you have to be a bit creative when initializing a bitset with the contents of a double. If you had to use a bitset and assuming 8 byte doubles and 4 byte longs, I think this should work:
: :
: :
double dbl = 49958634.876359;
: : unsigned long* ulp = reinterpret_cast<unsigned long*>(&dbl);
: : bitset<64> bits1(*ulp);
: : bitset<64> bits2(*(++ulp));
: : bits2 <<= 32;    // Shift values left
: : bits2 |= bits1;  // Combine this part with other part
: : cout << bits2 << endl;
: : 

: :
: : Which outputs on my machine:
: :
0100000110000111110100100111011101010111000000101100100010000010

: :
: : There may be some endian issues depending on your computer. Also, maybe somebody knows of an online converter to validate that.
: :

: Thank you, I am working with Borland C++ 5.02 in a AMD AthlonXP 2600+ 2.09GHz and 1.0GB RAM, HD Freespace of 8.0GB, and I am having issues with bitset, here is my code...
:
#include<iostream>
: #include<math>
: #include<bitset>
: 
: void main()
: {
: 	double value = M_PI;
:    unsigned* valueRef = reinterpret_cast<unsigned*>(&value);
: 
:    bitset<64> lessSignificative(*valueRef);
:    bitset<64> mostSignificative(*(++valueRef));
: 
:    mostSignificative <<= 32;
:    mostSignificative |= lessSignificative;
: 
:    cout << mostSignificative << endl;
: }

: ...and this are the compiler messages:
: Info :Compiling C:\Trabajo\Servicio social\Servicio\test.cpp
: Warn : string.h(549,3):Functions containing for are not expanded inline
: Warn : string.h(557,3):Functions containing while are not expanded inline
: Warn : string.h(563,3):Functions containing for are not expanded inline
: Warn : string.h(575,3):Functions containing for are not expanded inline
: Warn : string.cc(686,32):Comparing signed and unsigned values
: Warn : BITSET.h(221,3):Functions containing for are not expanded inline
: Warn : BITSET.h(267,3):Functions containing for are not expanded inline
: Warn : BITSET.h(273,3):Functions containing for are not expanded inline
: Warn : BITSET.h(279,3):Functions containing for are not expanded inline
: Warn : BITSET.h(292,3):Functions containing for are not expanded inline
: Warn : BITSET.h(305,3):Functions containing for are not expanded inline
: Warn : BITSET.h(311,3):Functions containing for are not expanded inline
: Warn : BITSET.h(340,3):Functions containing for are not expanded inline
: Warn : BITSET.h(380,3):Functions containing for are not expanded inline
: Warn : BITSET.h(399,3):Functions containing for are not expanded inline
: Warn : BITSET.h(407,3):Functions containing for are not expanded inline
: Warn : string.cc(658,2):Cannot create pre-compiled header: code in header
: Error: test.cpp(10,11):Undefined symbol 'bitset'
: Error: test.cpp(10,14):Ambiguous operators need parentheses
: Error: test.cpp(10,33):Call to undefined function 'lessSignificative'
: Error: test.cpp(11,14):Ambiguous operators need parentheses
: Error: test.cpp(11,33):Call to undefined function 'mostSignificative'
: Error: test.cpp(13,25):Illegal use of pointer
: Error: test.cpp(14,24):Illegal use of pointer
: Warn : test.cpp(17,2):'valueRef' is assigned a value that is never used
: Is anything wrong with my code? Please Help!!!
: Regards Reivaj

:

Yes, you've got some problems with the code you posted.

1. You probably need to put the following line after all of your #include statements.

using namespace std;


...or alternately change these lines:

bitset<64> lessSignificative(*valueRef);
bitset<64> mostSignificative(*(++valueRef));


to this:

std::bitset<64> lessSignificative(*valueRef);
std::bitset<64> mostSignificative(*(++valueRef));


2. main should always return an int.

3. This code has some issues:

unsigned* valueRef = reinterpret_cast<unsigned*>(&value);


Those both need to be unsigned long, just plain unsigned isn't going to work (unless your default unsigned int is 4 bytes, the same as an unsigned long?).

So, in total you should have this:
#include <iostream>
#include <math>
#include <bitset>
using namespace std;
 
int main()
{
    double value = M_PI;
    unsigned long* valueRef = reinterpret_cast<unsigned long*>(&value);
 
    bitset<64> lessSignificative(*valueRef);
    bitset<64> mostSignificative(*(++valueRef));
 
    mostSignificative <<= 32;
    mostSignificative |= lessSignificative;
 
    cout << mostSignificative << endl;

    return 0;
}





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.