从“ unsigned int”转换为“ unsigned char”可能会改变其值

时间:2018-10-11 14:01:01

标签: gcc gcc-warning

我很茫然,无法理解此警告(我已经阅读了几个类似的问题/答案)。警告示例:

basexx.c:181:15: warning: conversion to ?unsigned char? from ?unsigned int? may alter its value [-Wconversion]
    *op   = (t >> 24)      ;
               ^

移位unsigned int的结果是否应该是unsigned int? 无符号字符在转换操作中来自哪里?

如果它说:

basexx.c:181:15: warning: conversion to ?unsigned char? from ?unsigned int? may alter its value [-Wconversion]
    *op   = (t >> 24)      ;
          ^

这可能是可以理解的,只是对于较小的班次不会出现警告。

给出:

unsigned char * op;
unsigned int    t;

这些发出警告(任何24或更大的移位):

*op = (t >> 24) & 255;
*op = (t >> 24)      ;
*op = (t >> 25) & 255;
*op = (t >> 25)      ;

但这些不(不超过23个):

*op = (t >> 23) & 255;
*op = (t >> 23)      ;

0 个答案:

没有答案