简体   繁体   English

为什么按位取反运算符“〜”强制转换为int? (从“ int”转换为“ unsigned char”可能会改变其值)

[英]Why does bit-wise negate operator “~” cast to int? (conversion to ‘unsigned char’ from ‘int’ may alter its value)

GCC 4.9.1 reports "warning: conversion to 'unsigned char' from 'int' may alter its value [-Wconversion]" with the following code GCC 4.9.1报告“警告:从'int'转换为'unsigned char'可能会更改其值[-Wconversion]”,其代码如下

#include <cstdlib>

int main( int , char*[] ) {
  unsigned char *dest = new unsigned char[16];
  const unsigned char *src = new unsigned char[16];
  for( size_t i = 0; i != 16; ++i) {
    dest[i] = ~(src[i]);
  }
  return 0;
}

Obviously, both src and dest are both pointers to an array of unsigned char and I only want the latter to be the bit-wise negation of the former. 显然, srcdest都是指向unsigned char数组的指针,我只希望后者成为前者的按位取反。 For some strange reason the ~ operator seems to return an int and thus triggering the warning. 由于某些奇怪的原因, ~运算符似乎返回一个int ,从而触发警告。 Why? 为什么? Is this intended behaviour? 这是预期的行为吗?

Of course, I know I could use a static_cast<unsigned char>() to prevent the warning, but I feel that something else is wrong and the warning should not be there in the first place. 当然,我知道我可以使用static_cast<unsigned char>()来阻止警告,但是我觉得还有其他问题,并且警告不应该放在第一位。

I guess the literal answer is because the Standard says so. 我猜字面上的答案是因为标准这么说。 From [expr.unary.op]: 来自[expr.unary.op]:

The operand of ~ shall have integral or unscoped enumeration type; ~的操作数应具有整数或无范围的枚举类型; the result is the one's complement of its operand. 结果是一个操作数的补码。 Integral promotions are performed. 进行整体促销。 The type of the result is the type of the promoted operand. 结果的类型是提升操作数的类型。

which, according to [conv.prom] is: 根据[conv.prom],它是:

A prvalue of an integer type other than bool , char16_t , char32_t , or wchar_t whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; 如果int可以表示源类型的所有值,则整数转换等级(4.13)小于int等级的bool类型, boolchar16_tchar32_twchar_t的整数类型的prvalue可以转换为int类型的prvalue。 ; otherwise, the source prvalue can be converted to a prvalue of type unsigned int . 否则,可以将源prvalue转换为unsigned int类型的prvalue。

And int has higher rank than unsigned char . 并且int等级高于unsigned char So yes, it's intended behavior, and an explicit static_cast squelches the warning. 因此,是的,这是预期的行为,并且显式static_cast抑制了警告。

all operands in expressions are promoted to at least int s, because int should represent 'natural' integral type for given architecture. 表达式中的所有操作数至少都提升为int ,因为int应该表示给定体系结构的“自然”整数类型。

So this warning is proper - type on right side of assignment will be int (for some people it might indicate place for optimization*), static_cast is a good solution (I would add mask for 0xFF to it, just to be sure and properly state my intent). 因此,此警告是正确的-赋值右侧的类型将为int (对于某些人来说,它可能指示优化的位置*), static_cast是一个很好的解决方案(我将向其添加0xFF掩码,只是为了确保正确地声明状态)我的意图)。

*) for example: one could negate 4 bytes at once, making better use of CPU. *)例如:一个可以一次取反4个字节,从而更好地利用CPU。 Some compilers might do it by them selfs. 一些编译器可能会自己完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:从“ int”转换为“ unsigned char”可能会改变其值 - warning: conversion to 'unsigned char' from 'int' may alter its value 转换为std :: array <unsigned char, 1ul> :: int中的value_type可能会改变其值 - Conversion to std::array<unsigned char, 1ul>::value_type from int may alter its value 如何解决此警告:从“ int”转换为“ unsigned char”可能会更改其值 - How to fix this warning : conversion to 'unsigned char' from 'int' may alter its value 从int转换为char可能会改变其值 - Conversion from int to char may alter its value 警告:从&#39;long int&#39;转换为&#39;double&#39;可能会改变其值 - warning: conversion to 'double' from 'long int' may alter its value 条件运算符:从“int”转换为“unsigned char”,可能丢失数据 - Conditional operator: conversion from 'int ' to 'unsigned char ', possible loss of data 与int和移位执行按位与 - performing bit-wise AND with int and shifting g ++警告:从int转换为uint16_t可能会改变其值 - g++ warning: conversion to uint16_t from int may alter its value 在memset()中将int值转换为unsigned char如何工作? - How does the conversion of int value to unsigned char in memset() work? 缩小从 int 到 unsigned char 的转换 - Narrowing conversion from int to unsigned char
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM