当一个int被转换为短和截断,如何在新的价值决定的? [英] When an int is cast to a short and truncated, how is the new value determined?

查看:187
本文介绍了当一个int被转换为短和截断,如何在新的价值决定的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能澄清当整数转换为会发生什么用C?我使用的树莓派,所以我知道,一个 INT 是32位的,因此一个必须为16位。

Can someone clarify what happens when an integer is cast to a short in C? I'm using Raspberry Pi, so I'm aware that an int is 32 bits, and therefore a short must be 16 bits.

比方说我用下面的C code,例如:

Let's say I use the following C code for example:

int x = 0x1248642;
short sx = (short)x;
int y = sx;

我得到的 X 将被截断,但有人可以解释究竟如何?是否使用变化?究竟是如何从32位截断为16位数字?

I get that x would be truncated, but can someone explain how exactly? Are shifts used? How exactly is a number truncated from 32 bits to 16 bits?

推荐答案

根据ISO C标准,当你转换一个整数为有符号的类型,值是目标类型的范围之外,其结果是实现-defined。 (或者实现定义的信号可以提高,但我不知道这样做的编译器。)

According to the ISO C standard, when you convert an integer to a signed type, and the value is outside the range of the target type, the result is implementation-defined. (Or an implementation-defined signal can be raised, but I don't know of any compilers that do this.)

在实践中,最常见的行为是高阶位被丢弃。因此,假如 INT 是32位和为16位,转换价值 0x1248642 可能会产生看起来像 0x8642 的位模式。并假设一个二进制补码重新presentation为签名类型(其被用在几乎所有的系统),所述高阶位是符号位,所以结果的数字值将是 - 31166

In practice, the most common behavior is that the high-order bits are discarded. So assuming int is 32 bits and short is 16 bits, converting the value 0x1248642 will probably yield a bit pattern that looks like 0x8642. And assuming a two's-complement representation for signed types (which is used on almost all systems), the high-order bit is the sign bit, so the numeric value of the result will be -31166.

int y   =   sx;

这也涉及到一个隐式转换,从 INT 。由于 INT 的范围内保证至少覆盖的整个范围内的短,该值不变。 (因为在你的榜样,价值 SX 恰好是负数,再presentation的这种变化可能涉及到的符号扩展的,传播 1 符号位到结果的全部16个高位。)

This also involves an implicit conversion, from short to int. Since the range of int is guaranteed to cover at least the entire range of short, the value is unchanged. (Since, in your example, the value of sx happens to be negative, this change of representation is likely to involve sign extension, propagating the 1 sign bit to all 16 high-order bits of the result.)

正如我指出的,通过语言标准要求的所有这些细节。如果你真的想截断值更窄的类型,它可能是最好使用无符号类型(具有语言指定环绕行为),也许明确掩蔽操作,如:

As I indicated, none of these details are required by the language standard. If you really want to truncate values to a narrower type, it's probably best to use unsigned types (which have language-specified wraparound behavior) and perhaps explicit masking operations, like this:

unsigned int x = 0x1248642;
unsigned short sx = x & 0xFFFF;

如果您有要推到一个16位变量32位的数量,你应该做的第一件事就是决定如何你想你的code的行为如果该值不适合。一旦你决定,你可以弄清楚如何写C code,你想要做什么。有时截断恰好是你想要的,在这种情况下,你的任务是一件容易的事,特别是如果你使用的是无符号类型。有时,超出范围的值是一个错误,在这种情况下,你需要检查它,并决定如何处理错误。有时你可能想要的值饱和,而不是截断,所以你需要写code做到这一点。

If you have a 32-bit quantity that you want to shove into a 16-bit variable, the first thing you should do is decide how you want your code to behave if the value doesn't fit. Once you've decided that, you can figure out how to write C code that does what you want. Sometimes truncation happens to be what you want, in which case your task is going to be easy, especially if you're using unsigned types. Sometimes an out-of-range value is an error, in which case you need to check for it and decide how to handle the error. Sometimes you might want the value to saturate, rather than truncate, so you'll need to write code to do that.

了解转换用C如何工作是很重要的,但如果你的启动的与这个问题你也许会从错误的方向接近你的问题。

Knowing how conversions work in C is important, but if you start with that question you just might be approaching your problem from the wrong direction.

这篇关于当一个int被转换为短和截断,如何在新的价值决定的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆