Double numbers representation in c++

Not very long ago I did this question for float numbers and the answer received was great. The function I use in my program is this:

1
2
3
4
5
6
7
8
9
10
void FloatBits(float value,char bits[33]){
    void *bytes=(void*)&value;
    int *temp=(int*)bytes;
    bits[32]='\0';
    for(int i=31;i>=0;i--){
        if(*temp & 1) bits[i]='1';
        else bits[i]='0';
        *temp >>=1;
    }
}


I just modified this function so I can use it to get the representation of a double, but it doesn't work and I don't know why:


1
2
3
4
5
6
7
8
9
10
11
12
void DoubleBits(double value,char bits[8*sizeof(double)+1]){
    void *bytes=(void*)&value;
    int *temp=(int*)bytes;
    bits[8*sizeof(double)]='\0';
    for(int i=8*sizeof(double)-1;i>=0;i--){
        if(*temp & 1) bits[i]='1';
        else bits[i]='0';
        *temp >>=1;

    }
}


Thanks in advance
What is your function supposed to do?
Sorry for not being explicit. I use this function to get the bit representation of a number of a given type. In the first one, for example, if value is -675.78125, then the function save the number 11000100001010001111001000000000 on the bits variable. My problem is that I try to use the same function, modified like in the second function, to produce the bit representation of a double type, but this last function doesnt produce the bit representation as expected. And I dont understand why this is so.
That code does appear to be correct. Can you provide some simple examples where it's not working?
the second one is not working because double has 64 bits (2 times int) while float has 32 bits like the int you convert it to.

better work with bytes like so:
1
2
3
4
unsigned char *temp = (unsigned char *)&value;
...
if((temp[i / 8] & (1 << (i % 8)))
  ...
Thank you smac and Zaita, and thak you coder777. So, accordingly, please correct me if I'm wrong, the '>>' operator only moves the block of 32 bits to the left while the remaining 32 never change their position while doing *tem>>=1. So after the iteration of 32 times it will be only 0's. Coder777, I already did the program with your suggestion and it works perfectly.
Topic archived. No new replies allowed.