Understanding "(sizeof(t) & (sizeof(t) - 1)) != 0" from va_arg macro

Viewed 43

When looking into code for the va_arg macro (with MSVC), I came across this if else section.

((sizeof(t) > sizeof(__int64) || (sizeof(t) & (sizeof(t) - 1)) != 0) ? ... : ...

While I understand the meaning of the first part, based on my understanding of binary and sizeof, the second part seems to always be false. Is there a datatype that would satisfy the conditions of this subexpression:

(sizeof(t) & (sizeof(t) - 1)) != 0
1 Answers

This condition:

(sizeof(t) & (sizeof(t) - 1)) != 0

Will be true if sizeof(t) is not a power of 2. While this will be true for most if not all basic types, it could be the case for aggregate types such an array or struct.

If the size is a power of 2, subtracting 1 from that value will result in a value that has no bits in common with the size, so a bitwise AND of those value would be 0. If the size is not a power of 2, the highest bit set will remain unchanged when subtracting 1, so a bitwise AND of those values will result in a non-zero value.

Related