Monday, November 7, 2011

Operators

An operator is a symbol, which can be used to produce a new value from one or more input values. In programming there are seven main types:

Assignment
  • The assignment operator (=) is used to assign a value to a variable. The value is on the right and the variable is on the left side of the assignment operator.
    • int letters = 26;

Arithmetic
Examples in Python
  • Arithmetic operators are used to perform calculations. In general, these are used the same as in mathematics.
  • The addition operator (+) is used to add two or more inputs.
    • sum = 3 + 5
    • print sum            # sum should equal 8
  • The subtraction operator (-) is used to subtract one or more inputs from another.
    • difference = 5 - 3
    • print difference   # difference should equal 2
  • The multiplication operator (*) is used to multiply one or more inputs.
    • product = 5 * 3
    • print product       # product should equal 15
  • The division operator (/) is used to divide one of more inputs from another.
    • quotient = 5 / 3
    • print quotient      # quotient should equal 1 because both numbers are integers. The result would be 1.6667 if floats were used (declared in Python by writing either of the numbers with a decimal: 5.0 / 3)
  • The remainder operator (%) or modulo is used to find the remainder of the division of inputs.
    • remainder = 5 % 3
    • print remainder    # remainder should equal 2

Compound Assignment
Examples in Python.
  • Compound assignment operators are shortcuts that allow you to perform a calculation and immediately assign the resulting value to a variable.
  • So instead of:
    • sum = 3 + 5
    • sum = sum + 2
  • You can do:
    • sum = 3 + 5
    • sum += 2
  • There is a compound operator for all of the normal arithmetic operators, including:
    • addition: +=
    • subtraction: -=
    • multiplication: *=
    • division: /=
    • modulo: %=

Unary
Examples in C++.
  • In C++ and Java there are even easier compound assignment operators for addition (++) and subtraction (--). These perform calculations and assign the result to variables or vice versa, depending on where they are placed:
          int x, y;
          x = 0;
          y = x++;                            // the unary addition operator is placed after the variable name
          cout << x << ", ";               // assignment to y occurs first, addition to x occurs second
          cout << y << endl;             // 0, 1 should print to the screen
          
         int x, y;
         x = 0;
         y = ++ x;                           // the unary addition operator is placed before the variable name
         cout << x << ", ";               // addition to x occurs first, assignment to y occurs second
         cout << y << endl;              // 1, 1 should print to the screen


Equality and Relational
Examples in Python.
  • These operators are used to determine the relationship between two operands.
  • Equals (==) determines whether the operands are equal
    • print 3 == 5               # should print 0 (for false)
    • print 3 == 3               # should print 1 (for true)
  • Does not equal (!=) determines whether the operands are not equal
    • print 3 != 5                # should print 1 (for true)
    • print 3 != 3                # should print 0 (for false)
  • Greater than (>) determines whether the first operand is greater than the second
    • print 3 > 5                 # should print 0 (for false)
    • print 5 > 3                 # should print 1 (for true)
  • Less than (<) determines whether the first operand is less than the second
    • print 3 < 5                # should print 1 (for true)
    • print 5 < 3                # should print 0 (for false)
  • Greater than or equal (>=) determines whether the first operand is greater than or equal to the second
    • print 3 >= 5              # should print 0 (for false)
    • print 3 >= 3              # should print 1 (for true)
  • Less than of equal (<=) determines whether the first operand is less than or equal to the second
    • print 3 <= 3              # should print 1 (for true)
    • print 5 <= 3              # should print 0 (for false)

Logical
Examples in C++.
  • Logical operators (related to Boolean logic) are used to evaluate the inverse of an expression or two expressions to obtain one result.
  • The NOT operator (!) is used to determine the inverse of an expression.
           bool a = true;
           result = !a;
           cout << result << endl;      // should print 0 (for false)

           bool a = false;
           result = !a;
           cout << result << endl;       // should print 1 (for true)
  • The AND operator (&&) is used to determine whether both expressions are true. 
           bool, a, b;
           a = true;
           b = true;
           result = a && b
           cout << result << endl;      // should print 1 (for true)

           a = true;
           b = false;
           result = a && b;
           cout << result << endl;       // should print 0 (for false)
  • The OR operator (||) is used to determine whether either expression is true.
           bool a, b;
           a = true;
           b = false;
           result = a || b;
           cout << result << endl;        // should print 1 (for true)

           a = false;
           b = false;
           result = a || b;
           cout << result << endl;         // should print 0 (for false)
  • note that these symbols are appropriate for C++ and Java. Python also supports logical operations, but !, &&, and || are replaced by the words "not," "and," and "or," respectively.

Bitwise
Examples are just binary numbers. They are NOT written in a language's syntax.
  • Underneath the surface, all computations in programming are done in binary. Occasionally it may be more useful to work directly in binary, so many languages support bitwise operations. There is some overlap between logical operations and bitwise operations, so these operators should look familiar.
  • The bitwise complement operator (~) is equivalent to the logical NOT. It is used to invert all of the bits of a binary number. (returns 1 if 0, returns 0 if 1)

    ~01001001
    ---------------
      10110110
  • The bitwise AND operator (&) is equivalent to the logical AND. It is used to determine whether both bits are true. (returns 1 only if both bits are 1, 0 otherwise)

        01110011
    & 10010110
    -----------------
        00010010
  • The bitwise OR operator (|) is equivalent to the logical OR. It is used to determine whether either bit is true. (returns 0 only if both bits are 0, 1 otherwise)

      11010110
    | 00100011
    ---------------
      11110111
  • The bitwise XOR (exclusive or) operator (^) is used to determine whether both bits are equal. (returns 1 if both 1 or both 0, returns 0 otherwise)

       11101100
    ^ 01010110
    ----------------
       01000101
  • The bitwise left shift operator (<<) is used to shift the bits a specified number left. Zeros are placed in the empty spaces created on the right side of the binary number. (numbers shifted out of the number in green, numbers that remain in blue, placeholder zeros in red)

    11010100 << 2
    --------------------
    01010000
  • The bitwise right shift operator (>>) is used to shift the bits a specified number right. Zeros are placed in the empty spaces created on the left side of the binary number. (numbers shifted out of the number in green, numbers that remain in blue, placeholder zeros in red)

    11010100 >> 2
    --------------------
    00110101

No comments:

Post a Comment