Skip to content

Instantly share code, notes, and snippets.

@T-Dark0
T-Dark0 / what_as_does.md
Last active July 18, 2023 08:26
What `as` does

as does a huge number of things. Probably too many. Here's a comprehensive list of everything it can do. Note that as conversions are transitive: If a as B as C compiles, then so does a as C, though it might not take the same "path" through intermediate conversions.

  • int <-> int
    • zero_extend: unsigned int -> bigger unsigned int. Pads the number with leading zeroes.
    • sign_extend: signed int -> bigger signed int. Pads the number with leading zeroes if positive, and leading ones if negative.
    • truncate: bigger int -> smaller int (regardless of signedness). Throws away the high bits of the number
    • reinterpret_sign: int -> int of the same size and opposite signedness. Does nothing to the bits.
  • int <-> float
    • cast_saturate_to_infinity: Yields the float closest to the specified integer. Yields infinity if the integer is out of bounds.
  • cast_nan_to_zero_saturating_when_out_of_bounds: Truncates the float and yields the corresponding integer.