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
  • 4 What type is var? – the busybee Commented Mar 25 at 6:29
  • This might answer the question: Why is 0 < -0x80000000? – Lundin Commented Mar 25 at 7:39
Add a comment  | 

1 Answer 1

Reset to default 5

Is 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 Fs 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