Bit operators

suggest change

The result of a shift depends on the width of the operand type, not on whether the machine is called 64-bit. int is often 32 bits on both 32-bit and 64-bit platforms. Before C++20, left-shifting a negative signed value is undefined behavior, so this is not a portable way to inspect width:

int a = ~0;
int b = a << 1;

On a platform where int is wider or narrower, a well-defined shift of an unsigned value of that width can differ. A small microcontroller may use a 16-bit int.

Endianness does not affect the bitwise operators themselves. A right shift (>>) moves bits toward the least significant bit, and XOR combines corresponding bits. Endianness matters only for how a multi-byte value is stored in memory, whether or not you use bitwise operators.

| - bitwise OR

^ - bitwise XOR (exclusive OR)

& - bitwise AND

<< - left shift

>> - right shift

~ - bitwise NOT (unary complement)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents