一些笔记和随想

About the computer…

Recommendations: Turing Machine

When programming, you can imagine the computer as:

  1. A smart brain for operations/computing (i.e. Processor), but with no memory, e.g.
    • Calculate the result of 1 + 2
    • Read some number from the terminal
  2. A scratch paper (i.e. Memory) to store your result, e.g.
    • Decalre a variable int x;, then the computer will allocate a space in the memory to store the value of variable x.
    • Calculate x + y, the computer will compute the value of x + y, store the intermediate result in memory. (actually you used the memory)
    • Perform z = x, the computer will copy the value of z to the value of x.

Notes about variable: When you decalre int x;, the x itself is just an Identifier just as we talked about in the lecture. Just imagine what you do in mathematics:

\[\mathrm{let}~~ x = 1\]

the variable $x$ here is just an identifier, the value of x is what we need actually.

Now you can take out a pen and a paper, and do like this (in sequential):

  1. For all variables decalred, allocate some space for each of them to store their value, the order does not matter
  2. For all operations performed, do the same operation on your paper.

For example:

int main() 
{
  // allocate some space for x, current value = 1
  int x = 1; /* Current Paper: [x = 1] */
  int y = 2; /* Current Paper: [x = 1] [y = 2]*/
  int z = 2; /* Current Paper: [x = 1] [y = 2] [z = 2] */
  
  // Update the value of z to x + y insteade of the original...
  z = x + y; /* Current Paper: [x = 1] [y = 2] [y = 3]*/

  ...
}

Basic Types

In the last section, when we talk about variables, we do not talk about how we store the values. As we know, computer use bits for storaging to represent all kinds of values.

TL; DR

If you do not have much time, or you have already mastered Types, just read this subsection and go ahead.

  1. use int / long for $i \in \mathbb Z$, diff in range
  2. use unsigned int/ unsigned long for $n \in \mathbb N$, diff in range
  3. use float / double for $\alpha \in \mathbb R$, diff in precision & range
  4. use char for one character.

Integers

The integers is the easiest case:

Take a list of 8 bits (0/1), the list can have \(2 ^ 8 = 256\) different permuatations. then we can simply use each permutation to represent one integer, which ranges from 0 to 255 (256 numbers in total). Unsigned numbered are all constructed this way.

00000000 => 0
00000001 => 1
00000010 => 2
...
11111111 => 256

The signed case is similar, but:

  • the first bit stand for the sign of number,
  • and complement is used for represent negative integers as we talked about in the lecture.

In C, we declare one variable using:

  1. int – 32 bits (4 bytes) in memory.
  2. long – 64bits (8 bytes) in memory.
  3. unsigned – for unsigned int / long numbers.

Floatings.

IEEE.

Chars

Reference: ASCII

To understand chars, you can just imagine you are refer to a dict: In computer, we only have numbers, and we use integer to represent char. For example

  • a is the first character, we use 1 to represent
  • b is the second, use 2
  • c is the third, use 3
  • z is 26-th, use 26

To represent all the characters, we need about 100+ different integers(ASCII Table).

Const Variables, Const Expressions

Each of the followings is const variables / const expressions? (now we talk about traditional c++, not the modern one)

  1. const int x;
  2. 3.1415926
  3. 1 + 2
  4. "some text"
  5. let const char x[] = "some text": x is …?

C++ is really Massy…

Operators

There are multiple types of operators:

  1. assignment
  2. increment/decrement
  3. arithmetic
  4. logical
  5. comparison
  6. member access: .
  7. other: ... , a ? b : c

Operator Precedence

See: Operator Precedence

I can never remember the precedence, because () is useful when coding!

Diff a++ and ++a

let a = 0

  • ++a: the result of the expression is 1
  • a++: the result of the expression is 0

Diff l-value and r-value

(a + b)++ cause error.

  • l-value: has physical address
  • r-value: not a l-value.

Address will be covered in future (Pointer & Physical Memory Arrangement)

The a++ + ++a

Never write this code, and never consider this code.

F***!

  • For those put similar code in assignments, 0 grade.
  • If you are not sure about the evaluation order, just put () in your code.

() is an elegant and easy way.

The , expression

You should know about it (because bugs may arise at this point), but never use this expression in your code.

w = ++x || ++y && ++z expression

Reference: Lazy Evaluation

because the value of ++x is 2 (stand for true), the rest of the || expression will not be evaluated!

Never use this expression, because different platforms may have different implementation.

Type Conversion

Just test the feature on your machine, and you will get the answer.

About char, refer to the Basic Type - Char section.