Ad
  • Custom User Avatar

    When your C/C++ program behaves differently when you add/remove printing statements, it almost certainly means that it contains undefined behavior. I put your code in a static analyzer and it turns out that it contains undefined behavior when called with negative numbers.
    Here is the culprit line:

    int const t = 1U << ((*(unsigned int *)&f >> 23) - 0x7f);
    

    In C/C++, the bitshift operators << and >> are only well defined when 0 <= shift < width of the type being shifted, with the width defined as sizeof(type) * 8, i.e. the number of bits in the type. Using these operators with a shift outside of this range is undefined behavior, and this happens in your code for negative numbers, the shift will be around 260.

    (Also, *(unsigned int *)&f violates the rules of a well-behaved C/C++ program. If you want to reinterpret the bits of a type into another type, you are supposed to use std::memcpy() / std::bit_cast() since C++20).

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution