为什么>> 24会导致-Wconversion但>>> 23不会呢? [英] Why >>24 causes -Wconversion but >>23 doesn't?

查看:194
本文介绍了为什么>> 24会导致-Wconversion但>>> 23不会呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

#include <stdint.h>

unsigned char f(uint32_t RGBA)
{
  return (RGBA>>24) & 0xFF;
}

使用-Wconversion编译时,会引起警告:从'uint32_t {aka unsigned int}转换为'unsigned char'"可能会更改其值[-Wconversion]".如果我将shift值降低到23或更小,警告就会消失.

When compiled with -Wconversion it causes "warning: conversion to ‘unsigned char’ from ‘uint32_t {aka unsigned int}’ may alter its value [-Wconversion]". If I lower the shift value to 23 or less the warning disappears.

我已经浏览了C99标准,但我不明白这里会发生什么.如果删除&运算符,则始终会发出警告,并且警告可能会很好,因为表达式的结果(在整数提升之后)大于unsigned char.我唯一的想法是,仅因为gcc很聪明并且无论如何都会看到结果是8位的,才对较小的移位省略了警告,因为该标准并未将其特殊化.我在这里吗?

I've looked through the C99 standard and I don't understand what happens here. If I remove the & operator then the warning is always emitted and that is probably good, as the result of the expression (after integer promotions) is larger than unsigned char. My only idea is that the warning is omitted for smaller shifts only because gcc is smart and sees that the result is 8-bit anyway, as the standard doesn't make this a special case. Am I right here?

为什么偏移值很重要?那是GCC错误吗?叮当声似乎不会对任何移位值产生警告.

And why does the shift value matter? Is that a GCC bug? Clang seems to not produce the warning for any shift values.

我在64位Linux系统上使用GCC 5.3.1.

I'm using GCC 5.3.1 on a 64-bit Linux system.

推荐答案

如Shafik Yaghmour所述,这似乎是GCC中的错误:

As mentioned by Shafik Yaghmour, this appears to be a bug in GCC:

GCC错误40752:-Wconversion为不大于目标类型的操作数生成错误警告

它似乎自4.4.0版本以来就存在,该版本于2009-07-14首次报告,并且有5个重复项.根据错误报告中的评论,关于如何处理它似乎存在一些争论.

It appears to have been present since version 4.4.0, first reported on 2009-07-14, and has 5 duplicates. Based on the comments in the bug report, there seems to be some debate on how to handle it.

这篇关于为什么&gt;&gt; 24会导致-Wconversion但&gt;&gt;&gt; 23不会呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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