Changed two's complement method

Now using logic operators instead of bit shifting
This commit is contained in:
David Xchel Morales Hurtado 2026-07-11 10:19:06 -06:00
parent a612e331f3
commit 74c2d6cfe7
2 changed files with 2 additions and 2 deletions

View File

@ -2127,7 +2127,7 @@ FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg<T> arg,
// Handles negative cases on casts to smaller types using Two's complement
// One example is int{-104} to char which std::format uses as 152
if (sizeof(Char) < sizeof(abs_value) && (arg.prefix & 0xff) == '-')
abs_value = (1 << (8*sizeof(Char))) - abs_value;
abs_value = ~abs_value + 1;
return write_char<Char>(out, static_cast<Char>(abs_value), specs);
}

View File

@ -1334,7 +1334,7 @@ TEST(format_test, format_int) {
"invalid format specifier");
check_unknown_types(42, "bBdoxXnLc", "integer");
EXPECT_EQ(fmt::format("{:c}", static_cast<int>('x')), "x");
EXPECT_EQ(fmt::format("{:c}", int(-136)), "x");
EXPECT_EQ(fmt::format("{:c}", -136), "x");
}
TEST(format_test, format_bin) {