From 74c2d6cfe7993985635d81d87b5c3ed6036b1ff9 Mon Sep 17 00:00:00 2001 From: David Xchel Morales Hurtado Date: Sat, 11 Jul 2026 10:19:06 -0600 Subject: [PATCH] Changed two's complement method Now using logic operators instead of bit shifting --- include/fmt/format.h | 2 +- test/format-test.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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) {