#include #include #include using namespace std; template void printBits(T type); unsigned powerOf2(int exp); int main() { printBits('A'); printBits(static_cast(12)); printBits(static_cast(32000)); printBits(25); printBits(255); printBits(1234); printBits(123456); printBits(12345678); printBits(1234567890); cout << endl; int big = INT_MAX; printBits(big); big++; printBits(big); cout << endl; unsigned int ubig = INT_MAX; printBits(ubig); ubig++; printBits(ubig); cout << endl; printBits(123.456f); printBits(123.456); } template void printBits(T t) { cout << setw(8) << t << " = "; unsigned mask; unsigned char* ptr; for (size_t i = 0; i < sizeof(T); i++) { // Advance ptr each byte of the argument ptr = reinterpret_cast(&t) + i; // Print the contents of the byte for (int i = 7; i >= 0; --i) { mask = powerOf2(i); cout << (*ptr & mask ? 1 : 0); } cout << " "; } cout << endl; } unsigned powerOf2(int exp) { unsigned value = 1; for (auto i = 0; i < exp; ++i) { value *= 2; } return value; }