diff --git a/include/fmt/format.h b/include/fmt/format.h index 70b14c21..c6c22557 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2127,7 +2127,7 @@ FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg 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(out, static_cast(abs_value), specs); } diff --git a/test/format-test.cc b/test/format-test.cc index e40a5b94..5842890b 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -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('x')), "x"); - EXPECT_EQ(fmt::format("{:c}", int(-136)), "x"); + EXPECT_EQ(fmt::format("{:c}", -136), "x"); } TEST(format_test, format_bin) {