SoFunction
Updated on 2025-04-12

Understand C language assignment truncation in one article

Truncate meaning

In C, when a wide range of integers (such as 16-bit short or int16_t) is assigned to a narrower range of integers (such as 8-bit char or int8_t), the so-called "truncation" occurs if the original value exceeds the representation range of the target type. This means that the excess is discarded, leaving only the part that the target type can represent.

Note that truncation refers to the truncation stored in memory for complementary code. The storage of numerical values ​​is stored in memory in complement.

Cutoff example

Taking the unsigned 16-bit integer assigning value to the unsigned integer 8-bit as an example, the range of the 8-bit integer is 0-255, and the range of the 16-bit integer is 0-65536. To see the effect of truncation, you can choose a number greater than 255 (that is, the maximum value of an 8-bit unsigned integer) but less than 65536 (this maximum value of a 16-bit unsigned integer). For example, 300 of type uint16_t is assigned to type uint8_t. Select 300 (binary denoted as 00001 0010 1100), then after truncation, only the low 8-bit 0010 1100 will be retained, which corresponds to the 44 in the decimal.

For example, 130 of int16_t is assigned to the int8_t type. The int8_t type can only represent integers between -128 and 127. int16_t ranges from -32768 to 32767. The two's complement representation of 130 (in int16_t) is 0000 0000 1000 0010. However, when it is assigned to int8_t, only the lower 8 bits are reserved, i.e. 1000 0010.

Since the int8_t type is a signed type, the first bit 1 is regarded as a signed bit and is a negative number. According to the two's complement rule, to calculate the actual value of this number, you need to inverse it first (excluding the highest bit) and then add 1, which can be obtained as -126.

An easier way is to ignore the sign bit (i.e. the highest bit), and the remaining 7 bits are 000 0010, which is a binary representation of 2. However, since the whole number is negative, in the two's complement system, this is done by adding this positive number starting from -128 (1000 0000), i.e. -126. In addition, if the added value exceeds 127 (int8_t gets the maximum value), the final value can be obtained from the loop. For example, the added value is 128, then it will eventually be -128, because the next value of 127 is -128.

#include <>

int main(void)
{
    uint16_t wideVar = 300; // unsigned short
    uint8_t narrowVar = (uint8_t)wideVar; // unsigned char
    printf("%u, %u\n", wideVar, narrowVar); // 300, 44

    int16_t wideVar2 = 130;  // signed short
    int8_t narrowVar2 = (int8_t)wideVar2; // signed char
    printf("%d, %d\n", wideVar2, narrowVar2); // 130, -126

    return 0;
}

This is the end of this article about the implementation of C language assignment truncation. For more related C language assignment truncation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!