Quick Review
Always use parenthesis to disambiguate the precedence of operators if there is any question or opportunity for confusion.
The arithmetic operators all work like they do in normal mathematics. The Modulus (%) operator returns the remainder from an integer division. Beware about rounding or sign errors when the operands of integer division and modulus are negative.
The increment and decrement operators can be used to easily increment or decrement numbers. Beware of side effects, particular when it comes to the order that function parameters are evaluated.
Relational operators can be used to compare floating point numbers. Beware using equality and inequality on floating point numbers.
Comprehensive quiz
1) Evaluate the following:
a) (5 > 3 && 4 < 8)
b) (4 > 6 && true)
c) (3 >= 3 || false)
d) (true || false) ? 4 : 5
2) Answer the following:
a) 7 / 4
b) 14 % 5
3) Convert the following from binary to decimal:
a) 1101
b) 101110
4) Convert the following from decimal to binary:
a) 15
b) 53
5) Why should you never do the following:
a) int y = foo(++x, x);
b) int x = 7 / -2;
c) int x = -5 % 2;
d) float x = 0.1 + 0.1; if (x == 0.2) return true; else return false;
e) int x = 3 / 0;
Solutions
4.1 — Blocks (compound statements) and local variables
|
Index
|
3.8 — Bitwise operators
|
4.1 — Blocks (compound statements) and local variables
Index
3.8 — Bitwise operators
[...] 3.X — Comprehensive Quiz [...]
Woah, It’s been a while since you actually posted something.
Is this something you made because you were bored, or can we expect more tutorials (C++11 for example) in the future?
What I don’t understand from the quiz is #4; whether or not i’m using 4 bit, 8 bit etc… How would I know that with 15 decimal, I should start of with “15 >= 8″ instead of “15 >= 16″. The same goes for 53 decimal (or any other number for that matter…