Aussie AI

C++ Bitwise Operators

  • Book Excerpt from "Generative AI in C++"
  • by David Spuler, Ph.D.

C++ Bitwise Operators

Here's a refresher on the C++ bitwise operators:

    x & y — binary bitwise-AND

    x | y — binary bitwise-OR

    x ^ y — binary bitwise-XOR

    x << y — binary left bitshift

    x >> y — binary right bitshift

    ~x — unary bitwise-complement

Binary literals. Also, a reminder that C++ also supports binary literal constants with a “0b” prefix, similar to the hexadecimal “0x” prefix. For example, to represent the constant 10 (ten), your C++ code can use:

    const int ten = 10;     // decimal
    const int ten = 0xA;    // hexadecimal
    const int ten = 012;    // octal
    const int ten = 0b1010; // binary

Bitwise badness: A few pitfalls in coding C++ bitwise operators should be mentioned:

  • Integer-only: the C++ bitwise operators do not work on floating-point data types.
  • Quiet overflow: if you do anything to overflow an integer type, nobody's going to tell you. For example, shifting the sign bit too far left with “1<<32” instead of “1<<31” will simply lose it. You might get a compiler warning, though.
  • Two is not better than one. The & operator is bitwise, but && is logical. Similarly, | and ||. It's the reverse for < and << or > and >>. Choose the wrong one and you might get a compiler warning, if the stars are aligned and the wind is blowing easterly.
  • Operator precedence is tricky and not what you'd expect (it's arguably broken, but rather too late to fix), so use lots of parentheses in bitwise expressions, and don't ignore C++ compilation warnings.
  • Bitwise operators are not always well-defined on negative values (e.g. bitwise right shift is officially “undefined behavior” on a negative), so it's best to use “unsigned” types as operands to bitwise operators. Note also that it's often useful to add the suffix letter “u” to integer constants (e.g. 10u, 0xAu or 0b1010u), when dealing with bitwise operations. This makes the constant of type “unsigned” and avoids various bitwise operator problems with signed numbers.

Bitwise operation algebraic properties: The interaction with zero is an important difference between the main operations:

  • Bitwise-AND with zero equals zero:   a & 0 == 0
  • Bitwise-OR with zero equals the other value:   a | 0 == a

The following inequalities for bitwise operators on non-negative integers can also be useful to know:

  • Bitwise-AND only clears bits and is <= each operand:   a & b <= a
  • Bitwise-OR only sets bits and is >= each operand:   a | b >= a
  • Bitwise-AND equals the larger value only for equal numbers.
  • Bitwise-OR equals the larger value only for subset bit patterns.

Addition versus bitwise operations: The relationship between the bitwise operators and the integer “+” operator can be useful to understand:

  • Bitwise-AND is <= the sum of its operands:   a & b <= a + b
  • Bitwise-AND equals addition only if both numbers are zero.
  • Bitwise-OR is >= the sum of its operands:   a | b >= a + b
  • Bitwise-OR equals addition only for disjoint bit sets or zeros.

Note that these relationships are for positive integer values. Bitwise operators need positivity in their daily lives, whereas addition is fine with lots of negativity.

 

Next:

Up: Table of Contents

Buy: Generative AI in C++: Coding Transformers and LLMs

Generative AI in C++ The new AI programming book by Aussie AI co-founders:
  • AI coding in C++
  • Transformer engine speedups
  • LLM models
  • Phone and desktop AI
  • Code examples
  • Research citations

Get your copy from Amazon: Generative AI in C++