Semua buku

Profesional

Aplikasi Tentang Pelatih Masuk Mulai membaca

Computer Science · Glosarium

Apa itu Cast?

Definition 2.4 University Computer Science — Year 1 · Bab 2 — C: Types, Operators and Control Flow

A cast is an explicit conversion, written (type)expression. Converting a floating value to an integer type drops the fractional part (it truncates toward zero); converting an integer to a narrower unsigned type keeps it modulo 2N2^N.

Where the usual arithmetic conversions lead two operands. Narrow types are first promoted to int. Two signed or two unsigned operands meet at the greater rank, up their column (i). A signed operand goes to the unsigned operand’s type when that rank is equal or greater: right at the same rank, then up (ii). When the signed operand has the greater rank, the unsigned one moves to it along a dashed arrow if that type holds all its values, as unsigned into a 64-bit long; if not, both go to the unsigned type of the greater rank (iii). A floating operand makes the whole operation floating.
Where the usual arithmetic conversions lead two operands. Narrow types are first promoted to int. Two signed or two unsigned operands meet at the greater rank, up their column (i). A signed operand goes to the unsigned operand’s type when that rank is equal or greater: right at the same rank, then up (ii). When the signed operand has the greater rank, the unsigned one moves to it along a dashed arrow if that type holds all its values, as unsigned into a 64-bit long; if not, both go to the unsigned type of the greater rank (iii). A floating operand makes the whole operation floating.

Contoh

Example 2.5 (Why -1 < 1u is false)

In minus_one < one, both operands have rank int and one is unsigned, so by case (ii) minus_one is converted to unsigned: −1 mod 232=4294967295-1 \bmod 2^{32} = 4294967295, which is not less than 1. In big < one, big is a long, of greater rank, and on this machine long can hold every unsigned value: by case (iii) one is converted to long, and −1<1-1 < 1 is true. The same expression gives the opposite answer on a system where long has 32 bits: long then cannot hold 4294967295, so case (iii) sends both operands to unsigned long, and big becomes 4294967295 again.

Baca dalam konteks →