std::bitset
doesn't have a constructor accepting a double
. When you pass double
, you instead invoke the constructor that accepts unsigned long long
and the double
is first implicitly converted to unsigned long long
.
Problem is, unsigned long long
cannot represent infinity, and the behaviour of the program is consequently undefined. Standard says:
[conv.fpint]
A prvalue of a floating-point type can be converted to a prvalue of an integer type.
The conversion truncates; that is, the fractional part is discarded.
The behavior is undefined if the truncated value cannot be represented in the destination type.
To see the bits, you can first "bitcast" the floating point to an integer type, and then pass that integer to bitset:
auto int_b = std::bit_cast<std::uint64_t>(b);
std::bitset<64> x(int_b);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…