-32768没有装修成一个16位有符号值 [英] -32768 not fitting into a 16 bit signed value

查看:101
本文介绍了-32768没有装修成一个16位有符号值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PCLint v 9.00h

I am using PCLint v 9.00h

在我的code,我有以下(其中S16是一个符号1​​6位):

In my code I have the following (where S16 is a signed 16 bit):

S16 temperatureResult = -32768;

,其中除我的大脑已经停止工作是可以适应这种类型的最小值

Which unless my brain has stopped working is the smallest value that can fit into this type

但我得到的皮棉错误。违反MISRA 2004年所需规则10.1,整数隐式转换为较小的类型

But I am getting a lint error of "Violates MISRA 2004 Required Rule 10.1, Implicit conversion of integer to smaller type"

如果我将值更改为-​​32767它工作正常。

If I change the value to -32767 it works fine.

我失去了一些东西明显?

Am I missing something obvious?

推荐答案

这并不一定是不适合。很可能它适合。 (你真的检查吗?)

It doesn't necessarily "not fit". Quite possibly it fits. (Did you actually check it?)


  • 如果您的平台使用32位(或更大) INT ,那么所有的算术EX pressions在32位评估 INT 键入和警告只是告诉你,你是一个 INT 值转换为较小的类型。 PCLint根本没有刻意去检查实际值是否适合目标类型。

  • If your platform uses 32-bit (or larger) int, then all arithmetic expressions are evaluated in 32-bit int type and the warning simply tells you that you are converting an int value to a smaller type. PCLint simply didn't bother to check whether the actual value fits into the destination type.

您也许能够燮preSS这一警告具有显式类型转换

You might be able to suppress this warning with an explicit type cast

S16 temperatureResult = (S16) -32768;


  • 如果您的平台使用16位 INT ,那么一个稍微不同的问题可能是这里涉及。在C语言 -32768 不是一个原子常量。 -32768 实际上是由一个一元的前pression - 运营商应用到一个正的常数 32768 32768 是一个积极的常量,不适合一个16位的,由于这个原因,编译器使用更大的类型(32位长整型大概)重新present 32768 。因此, -32768 在一个较大型的领域进行评估,并最终为较大型的值。 PCLind决定要提醒你的隐式切换到更大的类型。 (请参见(-2147483648> 0)返回用C ++真实了解详细信息)

  • If your platform uses 16-bit int, then a slightly different issue might be involved here. In C language -32768 is not an atomic constant. -32768 is actually an expression consisting of an unary - operator applied to a positive constant 32768. 32768 is a positive constant that does not fit into a 16-bit type, for which reason the compiler uses a larger type (32-bit long int probably) to represent 32768. Consequently, -32768 is evaluated in the domain of a larger type and also ends up as a value of larger type. PCLind decided to warn you about the implicit switch to a larger type. (See (-2147483648> 0) returns true in C++? for more details.)

    如果这是这里发生了什么,然后避免你可以使用显式类型转换的警告

    If that's what's happening here, then to avoid the warning you can use an explicit type cast

    S16 temperatureResult = (S16) -32768;
    

    ,或者,可以恩preSS初始化为

    or, alternatively, you can express the initialization as

    S16 temperatureResult = -32767 - 1;
    

    在后一种情况下,编译器应该能够16位的领域内评估不断前pression INT 键入

    In the latter case the compiler should be able to evaluate the constant expression within the domain of 16-bit int type.

    这篇关于-32768没有装修成一个16位有符号值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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