0

I have a double variable. Is it possible to output every bit of this variable using char*(char pointer) ? So, char pointer would be

char* p = (char*)&myVar;

at the beginning and then it will be incremented to go through all the bits.

For example:

// double requires 8 bytes in the memory. 
double x = 8;

Should output something like:

00000000 00001010 00000000 00000000 00000000 00000000 00000000 00000000
Aremyst
  • 1,480
  • 2
  • 19
  • 33
  • Yes, it is possible. The way things print would be implementation-defined, though - for example, `8.0` could end up looking like `00000000 00000000 00000000 00000000 00000000 00000000 00001010 00000000`. – Sergey Kalinichenko Aug 13 '14 at 16:25
  • To be honest, Paul, the "duplicate" question shows solution using bitset and not a pointer. – Aremyst Aug 13 '14 at 16:32
  • @JoeSmith So read the `std::bitset` into your `char` array... – scohe001 Aug 13 '14 at 16:34
  • Never underestimate the simplicity of [a lookup table](http://ideone.com/iTosDI), easily generated with a half-dozen lines of code. Maybe overkill, but hard to argue with the triviality. – WhozCraig Aug 13 '14 at 18:18
  • @WhozCraig, hm, that's pretty fun way of doing it, thank you! – Aremyst Aug 14 '14 at 12:49

0 Answers0