当 int 被转换为 short 并被截断时,新值是如何确定的? [英] When an int is cast to a short and truncated, how is the new value determined?

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

问题描述

有人可以澄清在 C 中将整数转换为 short 时会发生什么吗?我使用的是 Raspberry Pi,所以我知道 int 是 32 位,因此 short 必须是 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 代码为例:

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 标准,当你将整数转换为有符号类型,并且值在目标类型的范围之外时,结果是执行-定义.(或者可以引发实现定义的信号,但我不知道有任何编译器会这样做.)

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 位,short 是 16 位,转换值 0x1248642 可能会产生一个看起来像 0x8642 的位模式.并且假设有符号类型的补码表示(几乎在所有系统上都使用),高位是符号位,因此结果的数值将为-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;

这还涉及一个隐式转换,从shortint.由于保证int的范围至少覆盖short的整个范围,所以值不变.(因为,在你的例子中,sx 的值恰好是负数,这种表示的变化很可能涉及符号扩展,传播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;

如果您想将一个 32 位的数量推入一个 16 位的变量中,那么您应该做的第一件事就是决定如果该值不合适,您希望您的代码如何运行.一旦确定了这一点,您就可以弄清楚如何编写符合您要求的 C 代码.有时截断恰好是您想要的,在这种情况下,您的任务会很容易,尤其是在您使用无符号类型时.有时超出范围的值是一个错误,在这种情况下,您需要检查它并决定如何处理错误.有时您可能希望值​​饱和,而不是截断,因此您需要编写代码来做到这一点.

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 被转换为 short 并被截断时,新值是如何确定的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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