Bitwise Operations

Bitwise Operations: A Comprehensive Guide to Efficient Bit Manipulation and Beyond

Bitwise operations are fundamental and efficient processor instructions that apply logical operators to data in a unique way that may not be familiar to most people. This article explores bitwise operations, their applications, and their advantages over logical operations.

Categories of Bitwise Operations

Bitwise operations mainly fall into two categories:

  1. Data Chopping/Sewing: Consider a floating-point value, which consists of three parts packed into a binary value. Accessing individual parts requires some “chopping” of the binary representation.
  2. SIMD (Single Instruction, Multiple Data) on Bitfields: In this category, the same operation is applied to each bit of a bitfield. For example, Conway’s Game of Life is a classic example of SIMD on bitfields.

The challenge with bitwise operations lies in understanding when and how to use them effectively, which often requires expertise in algorithm design.

Cost of Operations

Bitwise operations offer a constant cost regardless of the data width. In contrast, some other operations have varying costs based on the width of the operands. Here’s a summary of the cost of various operations for 32-bit and 64-bit operands:

  • Integer Division and Integer Modulo: 16 + housekeeping cycles for 32-bit operands and 32 + housekeeping cycles for 64-bit operands. The cost is 1 clock cycle per 2 bits, doubling when the data width doubles.
  • Integer Multiplication: 5 + housekeeping cycles for 32-bit operands and 6 + housekeeping cycles for 64-bit operands. When the data width doubles, it takes one more clock cycle.
  • Addition and Subtraction: These operations have a constant cost of 1 for any data width.
  • Bitwise operations (AND, OR, XOR, Shift, etc.): They also have a constant cost of 1 for any data width.

The “big O” notation used here represents the complexity in terms of the size of data in bits.

Bitwise Functions

Bitwise operators (AND, OR, XOR) are similar to their logical counterparts but operate on individual bits in a more efficient way. While logical operators perform operations independently on each bit, bitwise operations can perform up to 64 simultaneous logical operations without requiring multithreading.

  • Bitwise AND (A & B): It combines two binary values, retaining only the bits that are set in both A and B.
  • Bitwise OR (A | B): It combines two binary values, setting any bit that is set in either A or B.
  • Bitwise XOR (A ^ B): It combines two binary values, setting bits that are set in either A or B, but not both.

Bitwise operations have a constant cost of O(n) = 1, while logical operations have a cost of O(n) = n.

Bitwise Not

The bitwise NOT operator (~) is a unary operator that inverts the bits of a binary value. It is a constant cost operation with O(n) = 1. When applied to individual bits, logical NOT (!) is equivalent in terms of results.

Bitwise Shift Left and Right

Shift operators (<< and >>) move bits to the left or right by a specified number of positions. These operations are akin to multiplying or dividing by a power of 2. They have a constant cost of O(n) = 1, regardless of data width.

Odd / Even

Determining if an integer is odd or even can be efficiently done using bitwise operations. For binary numbers, if the least significant bit (unit bit) is 0, the number is even; if it’s 1, the number is odd. Bitwise solutions have a constant cost of O(n) = 1, while mathematical solutions involving division and modulo have a cost of O(n) = n.

Multiplication / Division by 2, 4, 8

Bitwise operations shine when performing multiplication or division by powers of 2 (2, 4, 8). For computers, these operations are analogous to human multiplication and division by 10, 100, 1000. Bitwise solutions have a constant cost of O(n) = 1, whereas mathematical solutions (division, modulo) have a cost of O(n) = n for division and O(n) = log(n) for multiplication.

Longest Run of 1’s

Bitwise operations are highly efficient when finding the longest run of 1’s in a binary integer. Unlike classical bit-by-bit solutions that involve division and modulo, bitwise solutions can work in O(n) = n time complexity, where n is the number of bits.

Gray Code

Gray code is a binary encoding scheme with applications in various devices like CNC machines, robotic arms, and 3D printers. It ensures that only one bit flips between consecutive values, preventing ambiguities during position sensing. Bitwise operations are used to encode and decode Gray code efficiently.

Floating Point Encoding

Floating-point numbers consist of multiple parts packed into an integer. Bitwise operations are employed to access specific parts efficiently, either through bitfield structures or direct bitwise manipulation.

Handling Huge Arrays of Bits

Bitwise operations are invaluable when handling vast arrays of bits efficiently, such as compression algorithms (e.g., Huffman coding), error detection and correction (e.g., Hamming codes), and encryption (e.g., XOR-based one-time pads).

Memory-Mapped Registers

In embedded systems, bitwise operations are used extensively to access and manipulate memory-mapped registers, which control hardware peripherals like GPIO pins, timers, and UART communication.

Bitwise operations are essential tools in computer programming and low-level systems programming. They offer constant-time complexity, making them highly efficient for various tasks, including bitwise manipulation, bitfield operations, bit testing, and binary encoding/decoding. Understanding when and how to use bitwise operations effectively is essential for optimizing algorithms and programs. While they might seem arcane or low-level, they remain relevant and powerful tools in modern software development.