This is the fourth lesson in a series on the C++ programming language. If you have not already checked out the previous lessons, particularly the second, or already have some experience in other languages this will probably be appear confusing. Today we will go over the various types and uses of operators in the C++ language.
We have seen many of the operators before now in our previous lessons. Most of their types, some operators have to operate on certain data types and not others, but some of them are probably unfamiliar. Some of the operators which we have seen also have variations which we have not seen but have used their basic form many times before. Each operator does something unique, some of the operators have similar behavior like the add assign operator which adds a value and makes the initial value equal to that sum but it is still different from the addition operator, and even though some of the operators have slightly different syntax that can be used, you can for example substitute the word “not” for the operator “!” but you should be consistent about which ones you use, the thing to remember is that each operator does some fundamental operation supported by the programming language which is generally not substitutable by some other operation or function. Previously, we looked at data types - integers, doubles, characters, strings, and bools to name a few, but we cannot do very much with them if we are not to perform some operation on them. After this tutorial you should think of types as a chunk of data and the operators we learn here, the things we can do to that data. There are of coursesome similarities between each of the operators so we will look at some of these “types” of operators.
Arithmetic operators are operators which perform some kind of mathematical operation on a chunk of data. Supported operations are: addition, subtraction, multiplication, division, modulo (integer remainder after division), and assignment. More sophisticated operations, such as factorials, exponents, and logarithms, will be covered later and while they are not fundamental parts of the language, they are still supported by the language in the sense that their behavior can be implemented by combining the concepts which we have learned previously as well as which we will learn here and in the next few chapters. An important feature of the arithmetic operators in the C++ language is that with the exception of the assignment operator, they are fundamentally nondestructive. This means that the value of an arithmetic operation, for example “x + y”, is lost unless you save the result, “x = x + y” or “x += y”. The reasoning behind this is that assignment and, for example, addition are two different operators, the practical advantage to this system is that we can use some temporary value, for example “x + y <1 z”, without having to create a temporary variable to handle that temporary value, “int w = x + y; w < y” would be a possiblesolut ion, which is very handy in developing useful programs.
Another one of type of operator is the relationship operator. These operators do not change the values in any way but returns a Boolean value. Think of each one of these one of these operators as a question which is being asked, “is x < y when x = 3 and y = 5? False”, where the answer is false if the assertion is not wrong and the answer is true if the assertion is valid. As mentioned before arithmetic operators do not affect the values in anyway, except the assignment operator, this is very useful when used in conjunction with many of the relationship operators because we often want to test a quantity without changing, mutating, that variable and preferably without creating a temporary variable.
Logical operators have not come up yet but the data type that they operator on, Boolean values, have appeared at least once. Logical operators stem from propositional logic, sometimes called propositional calculus, and only talk about how we can change a truth table. If you are not too familiar with propositional logic it probably will not matter much because we rarely use more than two or three of these operators in a single statement. They are similar to relationship operators in the sense that they do not mutate the values which they are working on.
Bitwise operators are closely related to logical operators, but instead of working on only one true/false value they operate on the true/false values for an entire byte of data. For example, you can try performing a shift operation on an integer which would shift over to the left or right, multiply or divide by some power of two, by some number of bits. In order to understanthis, you would have to have a solid understanding of how binary numbers, the way that most computers store their data, work. Expressing decimal values in binary is a process which we will discuss much later on but for now we will not have to worry about this too much.
Finally, there are two types of operators which we have not seen and who's use has not come up at all. We will go into greater depth regarding these operations later on as their applications become clearer but for now you can see them below. If there are some operators which seem strange or unfamiliar that is to be expected and will not matter until they are reintroduced. For the operators which we have seen this table will probably seem redundant, but it is handy for reference.
Name | Type | Syntax | Description | N-Ary | C | C# | Java | Overloadable |
---|---|---|---|---|---|---|---|---|
Addition | Arithmetic | x+y; | Adds y to x (non destructive) | Binary | Yes | Yes | Yes | Yes |
Subtraction | Arithmetic | x-y; | Subtract x from y (non destructive | Binary | Yes | Yes | Yes | Yes |
Multiplication | Arithmetic | x*y; | Multiply x by (non destructive) | Binary | Yes | Yes | Yes | Yes |
Division | Arithmetic | x/y; | Divide x by y (non destructive) | Binary | Yes | Yes | Yes | Yes |
Modulo | Arithmetic | x%y; | Returns the remainder after integer division of x by y | Binary | Yes | Yes | Yes | Yes |
Add Assign | Arithmetic | x+=y; or x=x+y; | Add y to x and make x equal to that sum | Binary | Yes | Yes | Yes | Yes |
Subtract Assign | Arithmetic | x-=y; or x=x-y; | Subtract y from x and make y equal to that difference | Binary | Yes | Yes | Yes | Yes |
Multiply Assign | Arithmetic | x*=y; or x=x*y; | Multiply x by y and make x equal to that product | Binary | Yes | Yes | Yes | Yes |
Divide Assign | Arithmetic | x/=y; or x=x/y; | Divide x by y and make x equal to that dividend | Binary | Yes | Yes | Yes | Yes |
Modulo Assign | Arithmetic | x%=y; or x=x%y; | Set x equal to the remainder of integer division of initial x by y | Binary | Yes | Yes | Yes | Yes |
Assignment | Arithmetic | x=y; | Set x equal to y | Binary | Yes | Yes | Yes | Yes |
Equality | Relationship | x==y; | Test if x is equal to y | Binary | Yes | Yes | Yes | Yes |
Inequality | Relationship | x!=y; | Test if x is not equal to y | Binary | Yes | Yes | Yes | Yes |
Greater Than | Relationship | x>y | Test if x is greater than y | Binary | Yes | Yes | Yes | Yes |
Less Than | Relationship | x<y; | Test if x less than y | Binary | Yes | Yes | Yes | Yes |
Greater Than Equal | Relationship | x>=y; | Test if x is greater than or equal to y | Binary | Yes | Yes | Yes | Yes |
Less Than Equal | Relationship | x>=y; | Test if x is less than or equal to y | Binary | Yes | Yes | Yes | Yes |
Logical AND | Logical | x&&y; | Returns true if x and y are true (ints are converted to bools, non destructive) | Binary | Yes | Yes | Yes | Yes |
Logical OR | Logical | x||y; | Returns true of x and y are true(ints are converted to bools, non destructive) | Binary | Yes | Yes | Yes | Yes |
Bitwise AND | Bitwise | x&y; | Performs a logical AND operation on each bit of x and y individually (non destructive) | Binary | Yes | Yes | Yes | Yes |
Bitwise OR | Bitwise | x|y; | Performs a logical OR operation on each bit of x and y Individually (non destructive) | Binary | Yes | Yes | Yes | Yes |
Bitwise XOR | Bitwise | x^y; | Performs a logical XOR operation on each of the bits individually | Binary | Yes | Yes | Yes | Yes |
Right Shift | Bitwise | x>>y; | Shifts each of the bits in x to right by y bits (non destructive) | Binary | Yes | Yes | Yes | Yes |
Left Shift | Bitwise | x<<y; | Shifts each of the bits in x to the left by y bits (non destructive) | Binary | Yes | Yes | Yes | Yes |
Right Shift Assign | Bitwise | x>>=y; | Sets x equal to x shifted to the right by y bits | Binary | Yes | Yes | Yes | Yes |
Left Shift Assign | Bitwise | x<<=y; | Sets x equal to x shifted to the left by y bits | Binary | Yes | Yes | Yes | Yes |
Subscript | Member | x[y] | Access the y'th position in the array x | Binary | Yes | Yes | Yes | Yes |
Scope Resolution | Member | x::y | Access the field y inside of class x | Binary | Yes | Yes | Yes | Yes |
Structure Dereference | Pointer | x-> | Access member b of pointer a | Binary | Yes | Yes | Yes | Yes |
Structure Reference | Pointer | x.y | Access member b of structure a | Binary | Yes | Yes | Yes | Yes |
Increment | Arithmetic | x++ or ++x | Add one to x | Unary | Yes | Yes | Yes | Yes |
Decrement | Arithmetic | x-- or --x | Subtract one from x | Unary | Yes | Yes | Yes | Yes |
Logical NOT | Logical | !x | Inverts the value of x | Unary | Yes | Yes | Yes | Yes |
Bitwise NOT | Bitwise | ~x | Flips all of the bits in x | Unary | Yes | Yes | Yes | Yes |
Indirection | Pointer | *x | Pointed to by x | Unary | Yes | Yes | Yes | Yes |
Reference | Pointer | &x | Address pointed to by x | Unary | Yes | Yes | Yes | Yes |