R
Robbie Hatley
I was struggling to come up with a way to discern the actual
bit patterns of the representations of C++ objects (esp.
objects of small built-in types), and I came up with the
following mess. But I'm wondering, is there an easier way
to do this? This seems so klunky.
#include <iostream>
#include <cmath>
template<typename T>
void Binary(T const & object)
{
size_t size = 8 * sizeof(object); // Size of object in bits.
unsigned long long int mask =
static_cast<unsigned long long int>
(pow(2.0, static_cast<double>(size - 1)) + 0.1);
unsigned long long int pattern =
*reinterpret_cast<const unsigned long long int*>(&object);
for( ; mask > 0 ; mask >>= 1)
{
if(pattern & mask) std::cout << "1";
else std::cout << "0";
}
std::cout << std::endl;
return;
}
--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
bit patterns of the representations of C++ objects (esp.
objects of small built-in types), and I came up with the
following mess. But I'm wondering, is there an easier way
to do this? This seems so klunky.
#include <iostream>
#include <cmath>
template<typename T>
void Binary(T const & object)
{
size_t size = 8 * sizeof(object); // Size of object in bits.
unsigned long long int mask =
static_cast<unsigned long long int>
(pow(2.0, static_cast<double>(size - 1)) + 0.1);
unsigned long long int pattern =
*reinterpret_cast<const unsigned long long int*>(&object);
for( ; mask > 0 ; mask >>= 1)
{
if(pattern & mask) std::cout << "1";
else std::cout << "0";
}
std::cout << std::endl;
return;
}
--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/