Created on: 3/21/05 LESSON 1-b: OPERATIONS Section 1: THE OPERATORS Being able to declare variables, is a pretty useless ability, if you can't do anything with those variables. That is why we have operations: To alter, and operate on data. The symbol which represetns an operation is called an "operator". The data that the operators operate on, are called "operands". C++ has many, many operators for us to use. Possibly the most important operation you can use, is the "assignment" operation. The assignment operator is a single "=" sign. Example: int a; a = 0; This code fragment declares a variable, and then sets it to the value "0". It is also possible to initialize a variable when you declare it: int a = 0; The 2 code fragments are essentially the same. We can combine the assignment operator with other operators. For example, the "addition" operator (obviously the "+" sign): int a = 0; //a equals 0 a = 2 + 3; //a equals 5 Furthermore, you can operate on any number of variables together: int a = 1; int b = 2; int c = 3; int d = 4; int e = 5; //a equals 1 a = b + c + d + e //a equals 14 (2 + 3 + 4 + 5 = 14) Just like humans, a compiler follows a certain order of operations in math. C++ only partially defines this order of operations, by defining the "precedence" of the various operators. The higher the precedecne, the sooner an operation is performed in relation to others. For example, the "=" operator has a very low precedence - so most other operations are performed before it is performed. A very useful feature, it wouldn't be very useful to assign b to a, and then add the other variables to a without assigning that value anywhere. I have provided this precedence table for your reference: HIGHEST LEVEL: () [] -> . ! ~ ++ -- (type) * & sizeof + - (All unary operators) * / % + - << >> < <= > >= == != & ^ | && || ?: = += -= *= /= etc... LOWEST LEVEL: , Other operators that I don't know where their precedence is: new delete You can use paranthesis (not the "()" operator!) to override precedence. And for some reason, I can not find the precendence for the "new" and "delete" operators for the life of me. But do know that they have a higher precedence than the assignment operator, and that it doesn't really relate well to the other operators anyways (more on this later). Section 2: OPERATIONS OF THE OPERATORS () - Function Call [] - Array index -> - pointer selector . - object selector ! - logical not operator ~ - bitwise not operator ++ - increment operator -- - decrement operator (type) - casting operator *(unary)-"value of" operator &(unary)-"address of" operator sizeof - returns the size of an object in bits +(unary)- doesn't really do anything -(unary)- multiplies number by -1 * - multiplication / - division % - modulous + - addition - - subtraction << - bit-shift left >> - bit-shift right < - logical less-than operator > - logical greater-than operator <= - logical less-than or equal to operator >= - logical greater-than or equal to operator == - logical equal to operator != - logical not equal to operator & - bitwise AND ^ - bitwise XOR | - bitwise OR && - logical AND || - logical OR ?: - ternary conditional operator = - assignment operator +=,-=,*=,/=,%=... perform operation on 2 numbers, and assign to first: (IE: a += b translates to a = a + b) , - comma operator (has special uses) I won't be going over all of the operators, but I will be going over MOST of them here. the others will just have to wait. Arithmetic Operators: '=' - assigns a value to a variable. Variable to be assigned a value is the left hand operator, while the value is on the right. Precedence is processed from right to left (right hand value is evaluated first). "+=,-=,..." - A short hand operation which operates the left hand value with the right hand value, and stores it in the left hand operand. The operation is determined by the symbol immediately to the left of the '=' symbol. | - Performs a bitwise OR between the 2 operands that it recieves. A bitwise OR compares each bit in both operands, and places a '1' whenever there is a '1' in either operand. EXAMPLE: 3 | 8 -> 0011 | 1000 == 1011 -> 11 011100 | 000111 == 011111 -> 31 As you can see, OR is similar to addition. In fact, your computer uses OR as part of it's logic in adding 2 numbers together! & - Performs a bitwise AND between the 2 operands that it recieves. A bitwise AND compares each bit in both operands, and places a '1' ONLY when there is a '1' in BOTH operands. EXAMPLE: 3 | 8 -> 0011 & 1000 == 0000 -> 0 011100 & 000111 == 000100 -> 4 ^ - Performs a bitwise XOR between the 2 operands that it recieves. A bitwise XOR compares each bit in both operands, and places a '1' ONLY when there is a '1' in ONLY ONE operand. EXAMPLE: 3 | 8 -> 0011 ^ 1000 == 1011 -> 11 011100 & 000111 == 011011 -> 27 << - Shifts the bits in the operand to the left the number of times specified by an int on the right. Bits that are shifted off of the left end are lost. Bits that 'appear' on the right are set to '0'. EXAMPLE: 5 << 3 -> 000101 << 3 == 101000 == 40 10110100 << 2 == 11010000 >> - Shifts the bits in the operand to the right the number of times specified by an int on the right. Bits that are shifted off of the right end are lost. Bits that 'appear' on the left are set to '0'. EXAMPLE: 5 << 3 -> 000101 >> 3 == 000000 == 0 10110100 >> 2 == 00101101 ~ - The bitwise NOT unary operator. The "~" sign is placed immediately to the left of the operand. Returns a value which has the opposite bits of the operand. EXAMPLE: ~5 -> ~0101 == 1010 -> 10 The NOT operation is used in operations on 2's compliment binary numbers - specifically finding the magnitude of a negative number (the absolute value of a negative number) + - The unary variation of the plus sign really does nothing. Maybe the compiler multiplies the value that it is prefixed to by 1 or something. Useless if you ask me. At least we can provide our own definition of the unary + operator by overloading its operation (a more advanced topic to be discussed MUCH later). - - The unary variation of the minus sign multiplies the value that it is prefixed to by -1. Technically, it returns the "2's compliment" of that value, but it does the same thing as far as we care. % - The modulous operator. Modulous math is essentially finding the remainder of an integer division. so 3 % 2 == 1, 3 % 3 == 0, anything % 1 == 0. Anythig % 0 is undefined, and may cause an exception to be thrown depending upon your system/compiler. / - I presume that you know that this means division, and that you can figure out that it divides the operand on the left by the operand on the right. However, you should know that dividing by 0 is undefined, and can do wierd things to your program. It can either cause an exception to be thrown (an error), or when you try to output it, it appears as a system or compiler constant (I remember one being "-#.INF"). Try to prevent the user from being able to cause a division by zero. ++/-- - Ah yes, the increment and decrement operators. These are the trademark of the C++ language. In fact, these are what caused the "++" to be added in front of the "C" in C++. Many other languages have followed C++ by adding support for these operators. The ++/-- operators are unary operators. An interesting thing about these operators, is that they have 2 forms - the post-fix and the pre-fix forms. In the pre-fix form, the ++/-- precedes the operand. In the post-fix form, the ++/-- procedes the operand. The only difference between the 2 forms is what value is returned to the next operator. The pre-fix form increments the value of the operand, and returns the incremented value. The post-fix form increments the value, but returns the OLD, unincremented value. The assignment operation is performed inside of both forms of increment/decrement, so you don't have to worry about using the assignment operator. Examples: a++ == (a = a + 1) a-- == (a = a - 1) but the next statements, is FALSE: a++ == (a + 1) a-- == (a - 1) consider the following code fragment: int a; int b = 2; int c = 3; int d = 4; a = b + ++c - d++; The last statement can be expanded to thus, as far as the a variable is concerned: a = (b) + (c + 1) - (d) This can be a very confusing concept, and I myself find myself looking up the difference between postfix and prefix form. ?: - Ternary conditional operator. The only operator which takes in 3 operands. The "?:" operator follows this format: ? : If the boolean expression is true, then statement1 is executed, if it is false then statement2 is executed. The value of the statement that is executed is returned for further processing by other operators to the left of the ?: operator. Learn more about boolean expressions, and this operator in a later chapter.