**Lecture 02: Data Representation** Anthony J. Clark # Built-In Types The most common types built-in to C (assuming x86-64) Type | Bytes (Typical) | Range ---------------------|-----------------|------- `unsigned char` | ≥ 1 (1) | $0 ≤ x ≤ (2^8 - 1) = 255$ `unsigned short` | ≥ 2 (2) | $0 ≤ x ≤ (2^{16} - 1) = 65,535$ `unsigned int` | ≥ 2 (4) | $0 ≤ x ≤ (2^{32} - 1) = 4,294,967,295$ `unsigned long` | ≥ 4 (8) | $0 ≤ x ≤ (2^{64} - 1) = 1.8e19$ `unsigned long long` | ≥ 8 (8) | $0 ≤ x ≤ (2^{64} - 1) = 1.8e19$ `char` | ≥ 1 (1) | $-128 = -2^7 ≤ x ≤ (2^7 - 1) = 127$ `short` | ≥ 2 (2) | $-32,768 = -2^{15} ≤ x ≤ (2^{15} - 1) = 32,767$ `int` | ≥ 2 (4) | $-2,147,483,648 = -2^{31} ≤ x ≤ (2^{31} - 1) = 2,147,483,647$ `long` | ≥ 4 (8) | $-9.2e18 = -2^{63} ≤ x ≤ (2^{63} - 1) = -9.2e18$ `long long` | ≥ 8 (8) | $-9.2e18 = -2^{63} ≤ x ≤ (2^{63} - 1) = -9.2e18$ `float` | 4 | $x ≤ 3.4e38$ `double` | 8 | $x ≤ 1.8e308$ `void *` | ? (?) | N/A - Draw stack for following program - Discuss size and alignment - [Base Convert: IEEE 754 Floating Point](https://baseconvert.com/ieee-754-floating-point) ~~~c #include int main() { unsigned char uc = 1; // 0xBBB3CFEF short s = -1; // 0xBBB3CFEC int i = 10; // 0xBBB3CFE8 float f = 1; // 0xBBB3CFE4 double d1 = -1; // 0xBBB3CFD8 double d2 = 1; // 0xBBB3CFD0 int *i_ptr = &i; // 0xBBB3CFC8 printf("0x%X\n", &uc); printf("0x%X\n", &s); printf("0x%X\n", &i); printf("0x%X\n", &f); printf("0x%X\n", &d1); printf("0x%X\n", &d2); printf("0x%X\n", &i_ptr); } ~~~ Address | Contents | Associated Variable --------|---------------------|-------------------- `F0` | `-------- --------` | `-------- --------` `E8` | `0000000A FFFF--01` | `iiiiiiii ssss--uc` `E0` | `-------- 3F800000` | `-------- ffffffff` `D8` | `BFF00000 00000000` | `d1d1d1d1 d1d1d1d1` `D0` | `3FF00000 00000000` | `d2d2d2d2 d2d2d2d2` `C8` | `******** ******E8` | `i_ptri_p tri_ptri` `C0` | `-------- --------` | `-------- --------` # Integers - Unsigned integers are "normal" binary - Signed integers using 2's complement (flip bits and add 1) + Alternative: Signed-magnitude * two values for zero * less efficient hardware for math---different hardware for addition vs subtraction + Alternative: 1's complement (bitwise NOT) * addition plus carry gets added to result * two values for zero *Focus on Sign-Mag and 2's* Binary | Unsigned | Sign-Mag | 1's | 2's -------|----------|----------|-----|---- 000 | 0 | 0 | 0 | 0 001 | 1 | 1 | 1 | 1 010 | 2 | 2 | 2 | 2 011 | 3 | 3 | 3 | 3 100 | 4 | -0 | -3 | -4 101 | 5 | -1 | -2 | -3 110 | 6 | -2 | -1 | -2 111 | 7 | -3 | -0 | -1 ~~~text 1 + -1 = 0 Sign-Magnitude 001 ( 1) + 101 (-1) ----- 110 (-2) <-- incorrect 001 - 101 ----- 100 (-0) <-- correct 2's Complement 001 ( 1) + 111 (-1) _____ 000 ( 0) <-- correct ~~~ ## Binary Operations - Bitwise: `&`, `|`, `~`, `^` - Logical: `&&`, `||`, `!` - Shifting: `>>`, `<<` # Real Values - Real values have hardware support for IEE 754 standard (32 and 64 bit) + Limited precision (cannot compare floats) + Denomalized values when exponent is all zeros or ones * Represent special values (infinity, NaN, subnormal around zero) + Alternative: Fractional binary numbers 1001.101 = 9.625 * Can only represent powers of 2 * Limited range of values - Sign-Window-Offset interpretation + Sign-bit + Window (leading digit is between two consecutive power of two) + Offset (precision inside given window) # Casting and Converting - Conversion from signed to other + When a signed integer is converted to an integer or a floating-point type, if the original value is representable in the result type, the value is unchanged. + When a signed integer is converted to an integer of greater size, the value is sign-extended. + When converted to an integer of smaller size, the high-order bits are truncated. + When converting a signed integer to a floating-point type, if the original value isn't representable exactly in the result type, the result is the next higher or lower representable value. + When converting from signed to unsigned of the same size, the bits are preserved but the interpretation changes. - Conversion from unsigned to other + When an unsigned integer is converted to an integer or floating-point type, if the original value is representable in the result type, the value is unchanged. + When converting an unsigned integer to an integer of greater size, the value is zero-extended. + When converting to an integer of smaller size, the high-order bits are truncated. The result is interpreted using the result type, as shown in this example. + When converting an unsigned integer to a floating-point type, if the original value can't be represented exactly in the result type, the result is the next higher or lower representable value. + When converting from unsigned to signed of the same size, the bits are preserved but the interpretation changes. - Conversion from floating-point to other + A floating-point value that's converted to another floating-point type undergoes no change in value if the original value is representable exactly in the result type. If the original value is numeric but isn't representable exactly, the result is either the next greater or next lower representable value. + A floating-point value that is converted to an integral type is first truncated by discarding any fractional value. If this truncated value is representable in the result type, the result must be that value. When it isn't representable, the result value is undefined. # Silicon - [Top 5 differences between NPN and PNP transistors | NPN vs PNP](https://www.electricalclassroom.com/npn-vs-pnp-transistor-difference/) - [Use Transistors to Build a NAND Gate](http://mathcenter.oxford.emory.edu/site/cs170/nandFromTransistors/) - [Software | nand2tetris](https://www.nand2tetris.org/software) # Resources - [C docs - get started, tutorials, reference. | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/c-language/?view=msvc-170) - [Conversions from signed integral types | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/c-language/conversions-from-signed-integral-types?view=msvc-170) - [Conversions from unsigned integral types | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/c-language/conversions-from-unsigned-integral-types?view=msvc-170) - [Conversions from floating-point types | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/c-language/conversions-from-floating-point-types?view=msvc-170) - [IEEE-754 Floating Point Calculator](https://mason.cc/float/)