← All posts
November 21, 2026 · 3 min read

Implicit conversions in C++, byte by byte

Implicit conversions in C++ change the bytes: int 7 becomes 401C000000000000 as a double, char + char is an int, and int 300 narrows to char 44.

What do implicit conversions in C++ do to the bytes in memory? When you assign an int to a double, add two chars, or store an int in a char, the compiler converts the value for you, without a cast. The type changes, and so do the bytes. Here are the real ones.

This is the written version of Understanding C++ #9. Everything below comes from one program, compiled with g++ -std=c++20 -Wall -O2 (GCC 13) on x86-64 Linux, where int is 4 bytes, double is 8 and char is a signed 8-bit byte. The program prints each value in hex, top byte first; in memory, x86-64 stores the lowest byte first.

What happens to the bytes when an int becomes a double?

int i = 7; then double d = i;. The sizes print 4 8: four bytes become eight. But the conversion does not copy the four bytes and pad them with zeros. An int stores 7 as a plain binary number, 00000007. A double stores a sign bit, an 11-bit exponent and a 52-bit mantissa (IEEE-754), so the same 7 has to be encoded again from scratch: 401C000000000000.

int i = 7 as 00 00 00 07 in 4 bytes, and double d = i as 40 1C 00 00 00 00 00 00 in 8 bytes

variable type bytes hex, top byte first
i int 4 00000007
d double 8 401C000000000000

Not one bit is in the same place. The low byte of i is 07; the low byte of d is 00. The value is exactly the same, though: a double has 53 bits of precision, more than the 32 bits of an int, so every int converts to a double without loss. To read the double's bits, the program uses std::bit_cast (C++20), which copies the 8 bytes into an unsigned long unchanged.

Why is char + char an int?

char a = 100, b = 100; are one byte each, 64 in hex. Before arithmetic, C++ applies the integral promotions: operands narrower than int, such as char and short, are converted to int first. So a + b is computed as an int, and sizeof(a + b) prints 4, not 1.

char a = b = 100 is one byte, 64; a + b is a 4-byte int holding 00 00 00 C8, which is 200

The sum prints 200. A signed char here holds at most 127 (CHAR_MAX), so the sum would not fit in a char, but it was never a char: it is an int holding 000000C8. There is no overflow. The narrowing only happens if you store the result back into a char.

What does narrowing do to the bytes?

int big = 300; then char c = big;. In hex, 300 is 0000012C. A char has room for one byte, so the conversion keeps the low byte, 2C, and the other three are dropped. 2C is 44, and c prints 44. Since C++20 this result is defined by the standard: converting to a signed integer type gives the value congruent modulo 2 to the power of its width, which here means keeping the low 8 bits. Before C++20 it was implementation-defined, and GCC already did the same.

int big = 300 is 00 00 01 2C; char c = big keeps only the low byte 2C, which is 44

Brace initialization does not allow this silently. char e{big}; is a narrowing conversion, which the standard makes ill-formed in a list initialization. GCC 13 reports it as a warning (narrowing conversion of 'big' from 'int' to 'char' [-Wnarrowing]) when big is a variable, and as an error for the constant char e{300};. Either way, braces make the compiler tell you.

What is the difference, in one line?

int to double rebuilds the bits into a new encoding of the same value. int to char drops the high bytes and keeps what was in the low one.

int to double rebuilds the bits, int to char drops bytes, with the whole program convert.cpp below

The program and its output

#include <bit>
#include <cstdio>
int main() {
    int i = 7;
    double d = i;
    std::printf("%zu %zu\n", sizeof i, sizeof d);
    std::printf("%08X\n", i);
    auto u = std::bit_cast<unsigned long>(d);
    std::printf("%016lX\n", u);
    char a = 100, b = 100;
    std::printf("%zu %d\n", sizeof(a + b), a + b);
    int big = 300;
    char c = big;
    std::printf("%08X %d\n", big, c);
}
4 8
00000007
401C000000000000
4 200
0000012C 44

Line by line: 4 8 are the sizes of i and d; 00000007 is i; 401C000000000000 is the bit pattern of d; 4 200 are sizeof(a + b) and the sum; 0000012C 44 are big in hex and c after the narrowing.

Try it

Store 256 in a char: what prints? Write 256 in hex first and look at its low byte, then run it on your own compiler.