在以下代码中,对8位无符号整数执行按位运算。
/**
*
* @param fileName
* @return file extension
* example file.fastq.gz => fastq.gz
*/
private String extractFileExtension(String fileName) {
String type = "undefined";
if (FilenameUtils.indexOfExtension(fileName) != -1) {
String fileBaseName = FilenameUtils.getBaseName(fileName);
int indexOfExtension = -1;
while (fileBaseName.contains(".")) {
indexOfExtension = FilenameUtils.indexOfExtension(fileBaseName);
fileBaseName = FilenameUtils.getBaseName(fileBaseName);
}
type = fileName.substring(indexOfExtension + 1, fileName.length());
}
return type;
}
在此示例中,uint8_t i = 10;
uint8_t j = i>>2;
被提升为i
,并且值被分配给8位unsigned int。 int转换回8位unsigned int是否安全?
答案 0 :(得分:2)
没有
在算术表达式中,如果可能,较小的类型将转换为int
,否则转换为unsigned int
(请参阅6.3.1.1p2)。
实际上,
uint8_t i = 10;
puts(_Generic(i>>2, uint8_t: "u8", int: "int", unsigned int: "uint"));
应该输出" int",应该
uint8_t i = 10, j = 2;
puts(_Generic(i>>j, uint8_t: "u8", int: "int", unsigned int: "uint"));
(在前一个示例中,i
也会升级为int
,因为2
的类型为int
,因为usual arithmetic conversions会适用。)< / p>