Skip to content

Instantly share code, notes, and snippets.

@cjhanks
Created September 1, 2016 17:28
Show Gist options
  • Save cjhanks/2beee8b87b72e3f2525903a0898c561d to your computer and use it in GitHub Desktop.
Save cjhanks/2beee8b87b72e3f2525903a0898c561d to your computer and use it in GitHub Desktop.
NonCompliantCpp (I think)
#include <iostream>
template <typename Scalar>
void
print_bits(Scalar s)
{
static_assert(sizeof(Scalar) == 4, "NO no no ");
uint32_t* k = (uint32_t*)&s;
for (size_t n = 0; n < 8 * sizeof(Scalar); ++n) {
std::cerr << ((*k & (1 << n)) ? "1" : "0");
}
std::cerr << "\n";
}
int
main()
{
do {
int i;
i = -1;
print_bits(i);
i = +1;
print_bits(i);
i = +0;
print_bits(i);
i = -0;
print_bits(i);
} while (0);
std::cerr << "------------------------\n";
do {
float f;
f = -1.0;
print_bits(f);
f = +1.0;
print_bits(f);
f = +0.0;
print_bits(f);
f = -0.0;
print_bits(f);
} while (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment