Skip to content

Instantly share code, notes, and snippets.

@degski
Created February 5, 2019 15:20
Show Gist options
  • Save degski/52884dae253e7b30474cf37db2c67372 to your computer and use it in GitHub Desktop.
Save degski/52884dae253e7b30474cf37db2c67372 to your computer and use it in GitHub Desktop.
floats as bits print structure
struct float_as_bits {
float_as_bits ( const float & v_ ) : value { v_ } { };
float_as_bits ( float && v_ ) : value { std::move ( v_ ) } { };
template<typename Stream>
[[ maybe_unused ]] friend Stream & operator << ( Stream & out_, const float_as_bits & v_ ) noexcept {
std::uint32_t v;
std::memcpy ( &v, &v_.value, 4 );
int c = 0;
std::uint32_t i = std::uint32_t { 1 } << 31;
while ( i ) {
putchar ( int ( ( v & i ) > 0 ) + int ( 48 ) );
if ( 0 == c or 8 == c ) {
putchar ( 32 );
}
i >>= 1;
++c;
}
return out_;
}
float value;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment