Same Bits Three Ways
#include <bit>
#include <cstring>
float f = 1.0f;
unsigned a = *reinterpret_cast<unsigned*>(&f); // (A)
unsigned b;
std::memcpy(&b, &f, sizeof b); // (B)
unsigned c = std::bit_cast<unsigned>(f); // (C)
sizeof(float) == sizeof(unsigned) == 4. Which lines have defined behaviour?
Sign in to answer questions and track your progress
Sign In