Type Punning

Noun · Development

Definitions

  1. Reinterpreting the raw bit pattern of a value as a different type without performing any conversion — for example, reading an IEEE 754 float's bits as an integer via a union or `memcpy` in C. This is useful for bit-level tricks (like the fast inverse square root hack) but violates strict aliasing rules if done via pointer casts, potentially invoking undefined behavior.

    In plain English: A low-level trick where you look at the raw bits of one type of data and pretend they represent a completely different type.

    Example: "The fast inverse square root in Quake III uses type punning through a pointer cast to read a float's bits as an int — technically undefined behavior, but it worked on every compiler of the era."

Related Terms