"Wrapping"? Why is it not called "trimming"?

Started by
5 comments, last by SiCrane 10 years, 2 months ago

Why is type conversion in C(akin languages)from a higher to a lower precision type not called "trimming" instead of "wrapping"?

It makes more sense, since in little endian systems the least significant byte(s) is(are) trimmed while the rest(most significant)byte(s) is(are) cut off.

In fact, the wrapping takes place in bit shifting operations.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.
Advertisement
Let's say you have 16-bit shorts and 32-bit ints. If you cast an unsigned int to an unsigned short,

for 0 you get 0,
for 1 you get 1,
...
for 65535 you get 65535,
for 65536 you get 0,
for 65537 you get 1,
...

In that sense, the numbers wrap around at 2^16. That's how I understand it.

C is not only for little endian machines. C actually specifies undefined results when trying to store a value larger than a variable can fit. So char c = 1000 leaves c in an undefined state on machines where char is 8 bits.

The practical effect of most implementations is that the value wraps in the exact same way that integer overflows occur. This is only a side effect of implementation details though, and is not guaranteed behavior in C. The frequency with which projects rely on this behavior does suggest it is unlikely to change wherever it exists today, but it's still just an implementation detail.

And I hope trimming never catches on to explain this process. Trimming implies a far more invasive process in my mind (trimming trailing/leading whitespaces for example).


The practical effect of most implementations is that the value wraps in the exact same way that integer overflows occur. This is only a side effect of implementation details though, and is not guaranteed behavior in C. The frequency with which projects rely on this behavior does suggest it is unlikely to change wherever it exists today, but it's still just an implementation detail.

It is actually guaranteed to behave this way for unsigned types. For signed types, overflow is UB, though on any system you'll encounter today it will probably either wrap around (2's complement) or saturate (most DSPs?).

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Actually, I've rarely heard of an integer conversion to a smaller type as "wrapping". Much more common is referring it to as a "narrowing" conversion.

I don't think I've ever heard it referred to as 'wrapping'; 'cast' or 'convert' sure but never 'wrapping'.

The only time I've heard wrapping used is in cases of, well, wrapping such as an unsigned int at max wrapping around to zero when being added to and the like.

It is actually guaranteed to behave this way for unsigned types. For signed types, overflow is UB, though on any system you'll encounter today it will probably either wrap around (2's complement) or saturate (most DSPs?).

That's not quite right. Specific behavior depends on language. In C++ with unsigned integer destination types, the result is always the least integer modulo congruent to the source integer modulo 2^n where n is the number of bits in the destination type. For a signed integer type, if the source integer value can be represented by the destination type, the value is unchanged. However, if not the result is implementation defined, not undefined.

C acts a lot like C++, other than weird wording in the standard; however, if the destination is a signed type and the original value cannot be represented in the destination type, the result can be an implementation defined value or an implementation defined signal can be raised.

C# on the other hand, for narrowing conversions that cannot be represented in the destination type, the result depends on whether or not overflow detection is enabled. If so, a narrowing conversion for an integer value that cannot be represented in the destination type will throw a System.OverflowException. If overflow checking isn't on, the result always consists of discarding the most significant bits of the source.

This topic is closed to new replies.

Advertisement