How to use '&' and '>>' on a double number

May 14, 2013 at 5:25pm
Hey,
I am working on a project where I need to retrive a double number and store 8 bits of the number in one field and the other 16 bits in another field. the code below gives me an error.

1
2
3
  lata= lat>>8;
  latb = (lat & 0xff);


The error states that & and >> are illegal for double. With this in mind, can I use these on a double. If not what can I do to achieve what I am trying to do? Any help will be greatly appreciated.

May 14, 2013 at 6:08pm
floating point numbers have intricate inner structure, so there is no sense in shift and bitwise and operators for them.

If you can tell me what do you want I might be able to help you

To illustrate:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<cassert>
#include<iomanip>

int main() {
    double d = 3.1415;
    assert(sizeof(d) == sizeof(unsigned long long));
    /*Make sure that our conversions are correct*/
    unsigned long long l = *reinterpret_cast<unsigned long long*>(&d);
    std::cout << std::fixed << std::setprecision(20) 
              << (*reinterpret_cast<double*>(&l) ) << std::endl;
    /*Shifting value as if it was integer */
    l = *reinterpret_cast<unsigned long long*>(&d) >> 1;
    std::cout << std::fixed << std::setprecision(20)
              << (*reinterpret_cast<double*>(&l) ) << std::endl;
}
Result:
3.14150000000000018119
0.00000000000000000000

And that what happens if I use -3.1415:
-3.14150000000000018119
34468122235899929731268127252972631307719631954602084229973957663296898131825128231025492077425653771839651515076174809397381002673182873761219166865457152.00000000000000000000
Last edited on May 14, 2013 at 6:33pm
Topic archived. No new replies allowed.