Bits and Integer Representation
-
Writing an Integer in Base Two
A binary numeral adds powers of two at positions whose bits are one.
-
Reading Hexadecimal Bytes
One hexadecimal digit describes four bits, so two such digits can write the value of one byte.
-
The Range of an Unsigned Bit Field
Find the smallest and largest unsigned integers a field of a given bit width can hold.
-
Reading Two's Complement
Read a signed binary number using the negative weight of its high bit and the positive weights of the other positions.
-
Negating a Two's-Complement Bit Pattern
At a fixed width, complementing the bits and adding one computes the negative modulo the number of available patterns.
-
Sign and Magnitude in a Bit Pattern
Sign-and-magnitude encoding reserves a bit for the sign and uses the remaining bits as an unsigned magnitude.
-
Wraparound Counters and Their Range
A wrapping counter reduces each result modulo the number of possible states. The stored value alone cannot record every overflow.
-
Saturating Arithmetic at a Fixed Boundary
Saturating arithmetic clamps a result to the representable range. It gives a different result from wraparound when a mathematical result exceeds that range.
-
Checking Addition Before Storing a Narrow Result
A calculation can fit in its working representation and still exceed the field where it will be written.
-
Using Bitwise And to Read Selected Bits
Bitwise and retains a one only at positions that are one in both operands. A mask can isolate part of a field.
-
Using Bitwise Or to Set Flags
Bitwise or sets every position that is already one in either operand, so it can enable a flag while preserving other enabled flags.
-
Clearing Selected Bits of a Field
To clear a selected bit while keeping the others, take bitwise and with a mask that has zero at the selected position and ones at the retained positions.
-
Toggling Bits with Bitwise XOR
Bitwise exclusive or produces one where the operands differ. A one in the mask inverts the selected source bit.
-
Left Shift and Scaling by Powers of Two
Shifting a nonnegative binary integer left inserts lower zero bits and multiplies its value by a power of two when the result is representable.
-
Logical and Arithmetic Right Shifts
A logical shift inserts zeros at the high end of a fixed-width field. An arithmetic shift extends its sign bit in a two's-complement interpretation.
-
Rotating Bits Within a Fixed Width
A bit rotation moves bits around the ends of a field without discarding the bits carried past one side.
-
Extracting a Packed Bit Field
To read a field inside an unsigned packed value, shift its lowest position down to zero and isolate the bits in its width.
-
Packing Two Fields into a Byte
Disjoint bit ranges can store several small unsigned fields. Their width constraints must be checked before combining them.
-
Sign Extension When Widening a Field
Sign extension copies the original sign bit into the new high positions to preserve a two's-complement value.
-
Multi-Byte Values and Byte Order
Byte order specifies which byte of a multibyte value appears first in a stored sequence.
-
Reading an Unsigned Length Field
A binary reader must know a length field's width, byte order, and unit before using it to take the next part of a record.
-
Serializing Signed Integers with an Explicit Width
A binary serializer should choose signedness, width, and byte order together so another reader can recover the same value.
-
Counting the Set Bits in a Word
Population count adds one for every bit position holding one. Specify a finite width if a negative value is represented with sign extension.
-
Counting Trailing Zero Bits
Trailing zero count gives the number of consecutive zero bits starting at the least significant end of a nonzero word.
-
One-Hot State Encoding
A one-hot representation identifies one state by setting exactly one of a fixed number of bits.
-
Set Operations with Bitsets
Once each element has a stable bit position, bitwise operations can compute union, intersection, and membership changes directly.
-
Recognizing a Power of Two as a Bit Pattern
A positive power of two has exactly one set bit in binary. Other positive integers represent a sum of more than one power.
-
Carrying During Binary Addition
When a column's inputs and incoming carry sum to two or more, the output bit keeps the remainder by two and advances a carry.
-
Borrowing During Binary Subtraction
A binary subtraction column borrows from the next position when its available digit cannot cover the subtrahend and any outstanding borrow.
-
Formatting Binary Data for Inspection
A byte dump should make offsets, widths, and byte boundaries visible so readers can tell where a representation changes.