Operators


Managed C++ and traditional C++ are identical when it comes to operators. If you have programmed before in C++, then you should find nothing new in this section, but it might serve as a bit of a refresher. For anyone else reading this book, this section is essential because it shows all the basic operations available to a Managed C++ programmer.

Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on integer, floatingpoint, and decimal data types. Seven arithmetic operations are available, as shown in Table 2-8.

Table 2-8: Arithmetic Operators

OPERATOR

ACTION

-

Subtraction or unary minus

+

Addition

*

Multiplication

/

Division

%

Modulus

--

Decrement

++

Increment

The -, +, *, and / operators perform exactly as expected. The % operator evaluates to the remainder of a division operation. The -- and ++ operators decrease and increase the operand by one, respectively. You can place the -- and ++ operators before the operand, and in this way, the increment or decrement happens before any other operations take place in the expression. You can also place them after the operand, and in this case, the increment or decrement happens after all operations in the expression.

When an expression contains more than one arithmetic operator, the arithmetic operators will get evaluated according to the precedence shown in Table 2-9. If two operators of the same precedence occur in the expression, then they are evaluated from left to right.

Table 2-9: Arithmetic Precedence

PRECEDENCE

OPERATORS

Highest

-- ++ - (unary minus)

* / %

Lowest

- +

Comparisons and Logical Operators

Comparison operators are used to compare two expressions and then generate a Boolean value (true/false) based on the result of the comparison. There are six comparison operators, as shown in Table 2-10.

Table 2-10: Comparison Operators

OPERATOR

MEANING

>

Greater than

>=

Greater than or equal to

<

Less than

<=

Less than or equal to

==

Equal to

!=

Not equal to

Logical operators are similar to comparison operators except that they compare Boolean values instead of expressions. The three logical operators are shown in Table 2-11.

Table 2-11: Logical Operators

OPERATOR

MEANING

!

NOT: If the operand was true, then false is evaluated or vice versa.

&&

AND: If both operands are true, then evaluate to true; otherwise, evaluate to false.

||

OR: If either or both operands are true, then evaluate to true; otherwise, evaluate to false.

Often, you will find both a comparison and a logical operator in the same comparison statement. For grins and giggles, figure out what this means:

 a < b && c >= d || !e 

Caution

Be very careful when using the assignment operator = and the equal to operator ==. When you mistakenly use = for the comparison operator, the left value gets overwritten by the right and, if the left value is nonzero, then the comparison will have a true result. This is unlikely to be what you want.

When a statement contains more than one comparison or logical operator, then they will get evaluated according to the precedence shown in Table 2-12. If two operators of the same precedence occur in the expression, then they are evaluated from left to right.

Table 2-12: Comparison and Logical Operator Precedence

PRECEDENCE

OPERATORS

Highest

!

> >= < <=

== ! =

&&

Lowest

||

Bitwise Operators

The bitwise operators are used to manipulate the bits of an integer type value. There are six bitwise operators, as shown in Table 2-13.

Table 2-13: Bitwise Operators

OPERATOR

ACTION

&

Bitwise AND

|

Bitwise OR

^

Bitwise XOR

~

Ones complement

>>

Right shift

<<

Left shift

The bitwise AND operator compares the bit pattern of its two operands. If both the bits at the same offset in the bit pattern are 1s, then the resulting bit pattern will become a 1; otherwise, it will become a 0. For example:

 0101 & 0011 becomes 0001 

The bitwise OR operator compares the bit pattern of its two operands. If either or both the bits at the same offset in the bit pattern are 1s, then the resulting bit pattern will become a 1; otherwise, it will become a 0. For example:

 0101 & 0011 becomes 0111 

The bitwise XOR operator compares the bit pattern of its two operands. If either, but not both, of the bits at the same offset in the bit pattern is a 1, then the resulting bit pattern will become a 1; otherwise, it will become a 0. For example:

 0101 & 0011 becomes 0110 

The ones complement operator simply flips the bits. If it was a 1, then it becomes a 0 and vice versa:

 0101 becomes 1010 

The shift operators shift all the bits of the operand per the number of bits specified right (>>) or left (<<). For example:

 Right shift -  00101100 >> 2 becomes 00001011 Left shift  -  00101100 << 2 becomes 10110000 

Tip

Right-shifting by 1 bit is equivalent to dividing by 2, and left-shifting by 1 bit is equivalent to multiplying by 2. Both shifts are far faster than either dividing or multiplying on a computer. So, if you need a little more speed in your application, and you are working with integer types and dividing or multiplying by factors of 2, you might want to consider shifting instead.

When a statement contains more than one bitwise operator, then the bitwise operators will get evaluated according to the precedence shown in Table 2-14. If two operators of the same precedence occur in the expression, then they are evaluated from left to right.

Table 2-14: Bitwise Operator Precedence

PRECEDENCE

OPERATORS

Highest

~

>> <<

&

^

Lowest

|

Conditional Operator

The conditional operator is the only ternary operator available to Managed C++ programmers. A ternary operator uses three expressions.

The conditional operator takes the first expression and sees if it is true (nonzero) or false (zero). If it is true, then the second expression is executed. If it is false, then the third expression is executed. A conditional operator looks like this:

 expression1 ? expression2 : expression3; a < b ? S"a is less than b" : S"a is greater than or equal to b"; 

Comma Operator

The comma operator causes a sequence of expressions to act as a single expression, with the last expression ultimately becoming what the total expression evaluates to. You can place a series of comma-delimited expressions anywhere you can place a normal expression.

You will probably see the comma operator most frequently used in the initialization and increment sections of a for loop, but there is nothing stopping a programmer from using it elsewhere. I discuss for loops later in this chapter.

The following example, though completely contrived, shows the comma operator in action. First, b is incremented, then a is assigned the value of multiplying postincremented a and b, and finally c is assigned the value of a modulus b:

 Int32 a = 2; Int32 b = 3; Int32 c = (b++, a = b++ * a++, a % b); 

The values of the variables after this code snippet finishes are

 a = 9 b = 5 c = 4 

Assignment Operators

There are a total of 11 assignment operators available to Managed C++, as shown in Table 2-15.

Table 2-15: Assignment Operators

OPERATOR

ACTION

=

Assign

+=

Add then assign

-=

Subtract then assign

*=

Multiply then assign

/=

Divide then assign

%=

Modulus then assign

>>=

Shift right then assign

<<=

Shift left then assign

&=

AND then assign

^=

XOR then assign

|=

OR then assign

The operator to assign one value to another is simply the equal sign (=). What basically happens is the expression on the right side of the equal sign is calculated and then assigned to the value on the left side of the equal sign.

You have seen assignment used several times already in this chapter, but here are a few more examples:

 String *str = S"This is a managed string."; Int32 num1 = 0x1234; Int32 num2 = 4321; num1 = num2; 

Assigning a common value to several different variables can be accomplished by stringing together several assignments. For example, to assign 42 to the variables a, b, and c, you would write:

 a = b = c = 42; 

It is a common practice to take a value, do some operation it, and then place the results back into the original operator. For example:

 a = a + 5; b = b * 2; 

So common is this that Managed C++ provides a set of special assignments to handle it:

 a += 5; b *= 2; 

Address of and Indirection Operators

Two operators are available to Managed C++ programmers for handling pointers, as shown in Table 2-16.

Table 2-16: Address of and Indirection Operators

OPERATOR

ACTION

&

Address of

*

Indirection

The address of operator returns the address of the object after it. For example, if x were located at address 1024, then to place the address (1024) in variable y, you would write this:

 y = &x;    // place the address of x into y 

The indirection operator, on the other hand, gets its value from the address stored within itself. For example, if y contains the address 1024, then to place the value of 50 at the address 1024, you would write:

 *y = 50;   // place the value of 50 at the address y points to 

Listing 2-14 is a program that shows the address of and indirection operators in action.

Listing 2-14: Address of and Indirection Operators in Action

start example
 #using <mscorlib.dll> using namespace System; Int32 main(void) {     Int32 x;    // create a variable     Int32 *y;   // create a pointer to an Int32     y = &x;     // place the address of x in the pointer y     *y = 50;    // place 50 at the address y points to     Console::WriteLine(x);  // print out x. This should contain 50.     return 0; } 
end example

Figure 2-15 shows the results of this little program.

click to expand
Figure 2-15: Results of AddressIndirect.exe

Operator Precedence

I have shown operator precedence for each operator in its own section, but what if operators from different sections occur in the same statement? Table 2-17 shows the precedence of all the operators.

Table 2-17: Operator Precedence

PRECEDENCE

OPERATORS

Highest

() [] ::

! ~ ++ -- - * &

* / %

+ -

< <= > >=

== !=

&

^

|

&&

||

?:

Lowest

= += %= >>= <<= &= ^= |=




Managed C++ and. NET Development
Managed C++ and .NET Development: Visual Studio .NET 2003 Edition
ISBN: 1590590333
EAN: 2147483647
Year: 2005
Pages: 169

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net