Bits and Integer Representation

  1. Writing an Integer in Base Two

    A binary numeral adds powers of two at positions whose bits are one.

    Undergraduate
  2. Reading Hexadecimal Bytes

    One hexadecimal digit describes four bits, so two such digits can write the value of one byte.

    Undergraduate
  3. The Range of an Unsigned Bit Field

    Find the smallest and largest unsigned integers a field of a given bit width can hold.

    Undergraduate
  4. 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.

    Undergraduate
  5. 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.

    Undergraduate
  6. 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.

    Undergraduate
  7. 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.

    Undergraduate
  8. 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.

    Undergraduate
  9. 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.

    Undergraduate
  10. 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.

    Undergraduate
  11. 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.

    Undergraduate
  12. 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.

    Undergraduate
  13. Toggling Bits with Bitwise XOR

    Bitwise exclusive or produces one where the operands differ. A one in the mask inverts the selected source bit.

    Undergraduate
  14. 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.

    Undergraduate
  15. 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.

    Undergraduate
  16. 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.

    Undergraduate
  17. 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.

    Undergraduate
  18. Packing Two Fields into a Byte

    Disjoint bit ranges can store several small unsigned fields. Their width constraints must be checked before combining them.

    Undergraduate
  19. 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.

    Undergraduate
  20. Multi-Byte Values and Byte Order

    Byte order specifies which byte of a multibyte value appears first in a stored sequence.

    Undergraduate
  21. 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.

    Undergraduate
  22. 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.

    Undergraduate
  23. 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.

    Undergraduate
  24. Counting Trailing Zero Bits

    Trailing zero count gives the number of consecutive zero bits starting at the least significant end of a nonzero word.

    Undergraduate
  25. One-Hot State Encoding

    A one-hot representation identifies one state by setting exactly one of a fixed number of bits.

    Undergraduate
  26. Set Operations with Bitsets

    Once each element has a stable bit position, bitwise operations can compute union, intersection, and membership changes directly.

    Undergraduate
  27. 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.

    Undergraduate
  28. 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.

    Undergraduate
  29. 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.

    Undergraduate
  30. Formatting Binary Data for Inspection

    A byte dump should make offsets, widths, and byte boundaries visible so readers can tell where a representation changes.

    Undergraduate