C未运营商为什么我得到一个警告 [英] C not operator why do I get a warning

查看:243
本文介绍了C未运营商为什么我得到一个警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我错这个code

Whats wrong with this code

typedef unsigned char datum; /* Set the data bus width to 8 bits. */

    datum pattern;
    datum antipattern;

    antipattern = ~pattern;


Remark[Pa091]: operator operates on value promoted to int (with possibly unexpected result) C:\filepath...\file.c 386 

编译器是IAR EWARM
为什么要两个char变量需要转换为int。为什么要抱怨标志的改变,当一切都被宣布为无符号。

Compiler is IAR EWARM why should two char variables need to be converted to an int. why should it complain about a change of sign when everything is declared as unsigned.

任何想法转换为使用摆脱这种警告?

Any idea what cast to use to get rid of this warning?

推荐答案

C的规则要求 unsigned char型的操作数转换为 INT (除了不正当C实现)。

The rules of C require that unsigned char operands be converted to int (except in perverse C implementations).

一旦操作数是 INT ,它被签署,而运营商可能会给你意想不到的效果,因为有符号整数和位重新presentations语义不被完全C.指定的编译器警告有益你这个问题。

Once the operand is an int, it is signed, and the ~ operator may give you unexpected results, because the semantics for signed integers and their bit representations are not fully specified by C. The compiler is helpfully warning you about this.

您应该使用反模式=〜(无符号整数)模式; 。随着 unsigned int类型,你保证该值是简单的二元psented重新$ P $。

You should use antipattern = ~ (unsigned int) pattern;. With unsigned int, you are guaranteed that the value is represented with simple binary.

这篇关于C未运营商为什么我得到一个警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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