我正在编写一些代码来测试位字段中的位,我写了这样的:
return (unsigned)(m_category | category) > 0
Run Code Online (Sandbox Code Playgroud)
我虽然这不会编译,因为我没有给出整数类型,但令我惊讶的是它确实编译了。所以我想知道,这是做什么的?是未定义的吗?
unsigned是相同的unsigned int。
转换会将有符号整数转换为无符号整数,如下所示:
https://en.cppreference.com/w/cpp/language/implicit_conversion#Integral_conversions
Run Code Online (Sandbox Code Playgroud)If the destination type is unsigned, the resulting value is the smallest unsigned value equal to the source value modulo 2n where n is the number of bits used to represent the destination type. That is, depending on whether the destination type is wider or narrower, signed integers are sign-extended[footnote 1] or truncated and unsigned integers are zero-extended or truncated respectively.
| 归档时间: |
|
| 查看次数: |
158 次 |
| 最近记录: |