How to convert a double variable to binary data (bits)? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to convert a double variable to binary data (bits)?

reivaj7999reivaj7999 Posts: 25Member
[blue]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[/blue]

Comments

  • IDKIDK Posts: 1,784Member
    : [blue]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[/blue]
    :
    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.

    [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]

  • reivaj7999reivaj7999 Posts: 25Member
    [b][red]This message was edited by reivaj7999 at 2005-8-12 9:49:24[/red][/b][hr]
    : 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.
    :
    : [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]
    [blue]Thank you Niklas,
    But how can I accomplish this?
    for instance I have this code...
    [code]
    double doubleValue = some_double_value;
    // I get the address of doubleValue
    double *doubleValueRef = &doubleValue;
    [/code]
    ...then, how can I make the access?
    Regards Reivaj[/blue]
  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : [blue]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[/blue]
    :

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

    [code]double dbl = 49958634.876359;
    unsigned long* ulp = reinterpret_cast(&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;
    [/code]

    Which outputs on my machine:
    [code]0100000110000111110100100111011101010111000000101100100010000010[/code]

    There may be some endian issues depending on your computer. Also, maybe somebody knows of an online converter to validate that.
    [/green]
  • reivaj7999reivaj7999 Posts: 25Member
    : [green]What errors/warnings does your compiler report in regards to the [italic]bitset[/italic]? The constructors for [italic]bitset[/italic] don't accept a [italic]double[/italic] as an argument so you have to be a bit creative when initializing a [italic]bitset[/italic] with the contents of a [italic]double[/italic]. If you had to use a [italic]bitset[/italic] and assuming 8 byte [italic]doubles[/italic] and 4 byte [italic]longs[/italic], I think this should work:
    :
    : [code]double dbl = 49958634.876359;
    : unsigned long* ulp = reinterpret_cast(&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;
    : [/code]
    :
    : Which outputs on my machine:
    : [code]0100000110000111110100100111011101010111000000101100100010000010[/code]
    :
    : There may be some endian issues depending on your computer. Also, maybe somebody knows of an online converter to validate that.
    : [/green]
    [blue]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...
    [code]#include<iostream>
    #include
    #include

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

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

    mostSignificative <<= 32;
    mostSignificative |= lessSignificative;

    cout << mostSignificative << endl;
    }[/code]
    ...and this are the compiler messages:
    Info :Compiling C:TrabajoServicio socialServicio est.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[/blue]
  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : : [green]What errors/warnings does your compiler report in regards to the [italic]bitset[/italic]? The constructors for [italic]bitset[/italic] don't accept a [italic]double[/italic] as an argument so you have to be a bit creative when initializing a [italic]bitset[/italic] with the contents of a [italic]double[/italic]. If you had to use a [italic]bitset[/italic] and assuming 8 byte [italic]doubles[/italic] and 4 byte [italic]longs[/italic], I think this should work:
    : :
    : : [code]double dbl = 49958634.876359;
    : : unsigned long* ulp = reinterpret_cast(&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;
    : : [/code]
    : :
    : : Which outputs on my machine:
    : : [code]0100000110000111110100100111011101010111000000101100100010000010[/code]
    : :
    : : There may be some endian issues depending on your computer. Also, maybe somebody knows of an online converter to validate that.
    : : [/green]
    : [blue]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...
    : [code]#include<iostream>
    : #include
    : #include
    :
    : void main()
    : {
    : double value = M_PI;
    : unsigned* valueRef = reinterpret_cast(&value);
    :
    : bitset<64> lessSignificative(*valueRef);
    : bitset<64> mostSignificative(*(++valueRef));
    :
    : mostSignificative <<= 32;
    : mostSignificative |= lessSignificative;
    :
    : cout << mostSignificative << endl;
    : }[/code]
    : ...and this are the compiler messages:
    : Info :Compiling C:TrabajoServicio socialServicio est.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[/blue]
    :

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

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

    [code]using namespace std;[/code]

    ...or alternately change these lines:

    [code]bitset<64> lessSignificative(*valueRef);
    bitset<64> mostSignificative(*(++valueRef));[/code]

    to this:

    [code]std::bitset<64> lessSignificative(*valueRef);
    std::bitset<64> mostSignificative(*(++valueRef));[/code]

    2. [italic]main[/italic] should always return an [italic]int[/italic].

    3. This code has some issues:

    [code][red]unsigned[/red]* valueRef = reinterpret_cast<[red]unsigned[/red]*>(&value);[/code]

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

    So, in total you should have this:
    [code]#include
    #include
    #include
    [blue]using namespace std;[/blue]

    [blue]int[/blue] main()
    {
    double value = M_PI;
    unsigned [blue]long[/blue]* valueRef = reinterpret_cast(&value);

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

    mostSignificative <<= 32;
    mostSignificative |= lessSignificative;

    cout << mostSignificative << endl;

    [blue]return 0;[/blue]
    }[/code]
    [/green]
  • IDKIDK Posts: 1,784Member
    [b][red]This message was edited by IDK at 2005-8-12 11:55:9[/red][/b][hr]
    [b][red]This message was edited by IDK at 2005-8-12 11:54:22[/red][/b][hr]
    : [blue]Thank you Niklas,
    : But how can I accomplish this?
    : for instance I have this code...
    : [code]
    : double doubleValue = some_double_value;
    : // I get the address of doubleValue
    : double *doubleValueRef = &doubleValue;
    : [/code]
    : ...then, how can I make the access?
    : Regards Reivaj[/blue]
    :
    Like this:
    [code]
    double doubleValue = some_double_value;
    byte *doubleAddress = &doubleValue;
    // at the red arrow [red]^[/red] I want to get the address of doubleValue to doubleaddress
    //make binary...
    for(int i = 0; i 0; p++) {
    cout << (Math.Pow(2,p) & &doubleAddress)/Math.Pow(2,p);
    } //right at the red arrow [red]^[/red] I want to get the value that's stored at the address doubleAddress.
    doubleAddress++;
    }
    [/code]
    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...

    [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]







Sign In or Register to comment.