Integer Math
- + and - are as expected.
- But because of the way things are implemented it "wraps around"
-
INT_MAX+1
-
INT_MIN-1
- Look at intDemo.cpp
- Notice this produces a warning:
-
- Warnings: fix them, do not ignore them.
- This is called numeric overflow
- * is multiply
- Nothing different here.
- But it can produce numeric overflow.
- / is divide
- This is a bit strange.
- It does integer division.
- Or only looks at the integer part
- Remember long division
- 23/4 = 5 R 3
- in c++ 23/4 = 5
- in c++ 2/4 = 0
- % is modulus
- This the the remainder.
- 23%4 = 3
- 2 %4 = 2
- This is really useful some times.
- ++ and --
- we write
a = a + 1
quite a bit.
- So
a++
is a shortcut.
-
++a
is also a shortcut.
- But be aware, there are difference.
- Just use them on a line by themselves and they are fine.
- -- is the same as
a = a -1
- Some other shortcuts.
-
a += 4
is the same as a = a + 4
- This is true for many operators.
- The order of operations you learned in math applies.
- Please excuse my dear aunt sally
- Use () like you did in math.