MYF

[RA] Ch3 Types

Reading Assignment: All of Programming Chapter 3 Types

Hardware Representations

Everything is a number to the computer, so there is no way to move “past numbers”. A computer stores everything as a series of 0s and 1s. Each 0 or 1 is called a bit.

Binary Numbers

A decimal number is represented in base 10. A binary number is a number represented in base 2, in which there are only 2 possible values for each digit(0 and 1). The 0 and 1 correspond to low and high voltage values stored in your computer.

Binary numbers are interpreted such that each bit(the name for a binary digit) holds the value 2 raised to an increasing exponent.

Looking Under the Hood

Abstraction–the separation of interface from implementation.

Hex = hexadecimal, which is base 16, meaning that it represents a number with a 1s column, a 16s column, a 256s column, and so on. Instead of writing out the entire 32 bit binary sequence, we can use 8 digits of hex or shorthand 0x2A.

Basic Data Types

char

A char is the smallest data type–a mere 8 bits–and is used to encode characters. These 8 bits are interpreted via the American Standard Code for Information Interchange (or ASCII) character-encoding scheme. Another much more expressive character-encoding scheme you may encounter is Unicode.

Being able to write A instead of 65 is another example of abstraction.

int

unsigned int is used to express 0~2^32-1.

int is used to express -2147483648 to 2147483647.

short is only 16 bits long.

Technically, the only requirement that the C language standard imposes is that a short int has fewer than or equal to as many bits as an int, and that a long int has greater than or equal to as many bits as an int.

float, double

One way we could choose to represent real number is fixed point. However, it could not represent very large numbers, nor could we represent very small numbers precisely.

The most common choice is that computers use floating point notation, the same notation but implicitly in based 2: $m\times { 2 }^{ e }$. $m$ is called the mantissa, $e$ is the exponent.

A float has 32 bits used to represent a floating point number. The lowest 23 bits encode the mantissa, the next 8 bits encode the exponent, the most significant bit is the sign bit. ${ \left( -1 \right) }^{ s }\times m\times { 2 }^{ e }$. A double has 64 bits and uses them by extending the mantissa to 52 bits and the exponent to 11 bits.

Standards

The arrangement here is part of the IEEE(Institute of Electrical and Electronics Engineers) Standard.

Precision

The loss of some precision is unavoidable. A float is said to represent single-precision floating point whereas a double is said to represent double-precision floating point.

A double takes up twice as much space as a float. This may not matter for a single variable, but some programs declare thousands or even millions of variables at a time.

Printing Redux

Do not memorize, let it come to you naturally.

Expressions Have Types

Most(but not all) expressions with binary operators have the same type as their operands.

Type Conversion

When the two operands have different types, the compiler attempts to add a type conversion. If no type conversion is possible, the compiler will issue an error message and refuse to compile the code. When the compiler inserts a type conversion, it typically mush add instructions to the program which cause the processor to explicitly change the bit representation from the size and representation used by the original type to the size and representation used by the new type.

When a programmer treat the variables as though it were of another type, it is called casting. When a compiler dose it, it is called type conversion or type promotion.

Casting

Overflow & Underflow

Overflow: an operation results in a number that is too large to be represented by the result type of the operation. The opposite effect is called underflow in which an operation results in a number that is too small to be represented by the result type of the operation.

“Non-Numbers”

Strings

A string is a sequence of characters that ends with a special character called the null terminator, which can be written with the character literal \0 that signals the end of the string.

Images

The most common way is RGB encoding, which encodes each color by specifying how much red, green and blue they contain.

Sound

Sound is a naturally a waveform.

Videos

A video is a sequence of images and the corresponding sound.

Complex, Custom Data Types

struct

A struct allows a programmer to bundle multiple variables into a single entity. There are 4 syntactic options that create the same conceptual struct.

-w423

Struct declarations do not go inside functions; they live in the global scope of the program, next to function declarations and definitions.

If you cannot articulate why those data make sense together, they do not belong in a struct together.

typedef

Enumerated Types

Enumerated types are named constants that can increase the readability and the correctness of your code.