admin管理员组文章数量:1400559
When I need to get lower 32bit of a variable:
Is there any difference between var & 0xFFFFFFFFu
vs. var & 0xFFFFFFFF
?
Is there any risk if I do not add suffix "u" in any case?
When I need to get lower 32bit of a variable:
Is there any difference between var & 0xFFFFFFFFu
vs. var & 0xFFFFFFFF
?
Is there any risk if I do not add suffix "u" in any case?
Share Improve this question edited Mar 25 at 11:11 chux 155k16 gold badges151 silver badges301 bronze badges asked Mar 25 at 6:15 leotsingleotsing 2171 silver badge5 bronze badges 2 |1 Answer
Reset to default 5Is there any difference between var & 0xFFFFFFFFu vs. var & 0xFFFFFFFF?
Commonly, no functional difference.
0xFFFFFFFF
is a hexadecimal constant. Its type is determined by its value to be int
, unsigned
, long
, unsigned long
, long long
, unsigned long long
- the first type the value fits in. It is very commonly an unsigned type given integer widths of 16, 32, 64, etc. Only when this 32-bit value can be first a signed type before an unsigned one, (think 36-bit long
) will this constant be signed.
0xFFFFFFFFu
is a hexadecimal constant. Its type is determined by its value to be unsigned,
unsigned long
, unsigned long long
. It is always some unsigned type.
On common systems, var & 0xFFFFFFFFu
and var & 0xFFFFFFFF
will result in the same value and type. The type of the and result depends and is affected by var
- think unsigned long long
vs. long long
, vs. int
.
Is there any risk if I do not add suffix "u" in any case?
No, scant risk on common systems.
Best to follow your group's coding style here.
I would recommend appending the u
. Little downside to adding that u
and likely easier to review and maintain.
In general, &
and other logical operands are best as unsigned types. Signed types often involve extra analysis and incur risks.
As I do not like naked magic numbers, obliging a reviewer to count F
s nor casting, I recommend the following to get lower 32 bits of a variable:
var & UINT32_MAX;
本文标签: cAny difference or risk betweenquotvaramp0xFFFFFFFFuquot vs quotvaramp0xFFFFFFFFquotStack Overflow
版权声明:本文标题:c - Any difference or risk between"var&0xFFFFFFFFu" vs. "var&0xFFFFFFFF"? - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744214613a2595577.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var
? – the busybee Commented Mar 25 at 6:29