This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:operators [2023/07/10 18:57] – pczekalski | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:operators [2023/11/23 10:20] (current) – pczekalski | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== Operators, Specifiers and Pointers ==== | + | ====== Operators, Specifiers and Pointers |
+ | {{: | ||
Operators represent mathematical, | Operators represent mathematical, | ||
- | === Assignment Operator === | + | ===== Assignment Operator |
* **Assignment operator ( = )** – the operator that assigns the value on the right to the variable on the assignment operator' | * **Assignment operator ( = )** – the operator that assigns the value on the right to the variable on the assignment operator' | ||
- | === Arithmetic Operators === | + | ==== Arithmetic Operators |
Arithmetic operations are used to do mathematical calculations with numbers or numerical variables. The arithmetic operators are the following. | Arithmetic operations are used to do mathematical calculations with numbers or numerical variables. The arithmetic operators are the following. | ||
Line 37: | Line 37: | ||
</ | </ | ||
- | === Bitwise Operators === | + | ==== Bitwise Operators |
Bitwise operators perform operations on bits in the variable. Among them, there exist bitwise logic operations. It means the same logic function is applied to every pair of bits in two arguments. Bitwise or ( | ) means that if at least one bit is " | Bitwise operators perform operations on bits in the variable. Among them, there exist bitwise logic operations. It means the same logic function is applied to every pair of bits in two arguments. Bitwise or ( | ) means that if at least one bit is " | ||
Line 61: | Line 61: | ||
</ | </ | ||
- | Bitwise operators also allow shifting data left ( << ) or right ( >> ) chosen number of bit positions. Shifting is often used in embedded programming to access the bit at a specific position. Shifting data one bit left gives the result of multiplication by 2 while shifting one bit right gives the effect of dividing by 2. | + | Bitwise operators also allow shifting data left ( << ) or right ( >> ) chosen number of bit positions. Shifting is often used in embedded programming to access the bit at a specific position. Shifting data one bit left gives the result of multiplication by 2, while shifting one bit right gives the effect of dividing by 2. |
<code c> | <code c> | ||
Line 71: | Line 71: | ||
</ | </ | ||
- | === Compound Operators === | + | ==== Compound Operators |
Compound operators in C/C++ are a short way of writing down the arithmetic operations with variables. All of these operations are done on integer variables. These operands are often used in the loops when it is necessary to manipulate the same variable in each cycle iteration. The compound operators are the following. | Compound operators in C/C++ are a short way of writing down the arithmetic operations with variables. All of these operations are done on integer variables. These operands are often used in the loops when it is necessary to manipulate the same variable in each cycle iteration. The compound operators are the following. | ||
Line 79: | Line 79: | ||
a++; //The operation a = a + 1; the result will be 6 | a++; //The operation a = a + 1; the result will be 6 | ||
</ | </ | ||
- | * **Decrement ( -- )** – decreases the value of the integer variable by one. | + | * **Decrement ( - - )** – decreases the value of the integer variable by one. |
<code c> | <code c> | ||
int a = 5; | int a = 5; | ||
Line 122: | Line 122: | ||
</ | </ | ||
- | === & and * Operators: Pointers and References === | + | ==== & and * Operators: Pointers and References |
- | Simple and complex types can be referred to with the use of pointer variables. A pointer is a variable that holds an address of the variable | + | Simple and complex types can be referred to with pointer variables. A pointer is a variable that holds the address of the variable. The length |
The following example presents a simple type declaration and the use of a pointer variable. | The following example presents a simple type declaration and the use of a pointer variable. | ||
Line 130: | Line 130: | ||
<code c> | <code c> | ||
- | int n = 10; // | + | int n = 10; // |
int *ptr; // | int *ptr; // | ||
//At this point, *ptr does not contain any address yet, | //At this point, *ptr does not contain any address yet, | ||
//rather some random address or null. | //rather some random address or null. | ||
ptr = & | ptr = & | ||
- | //ptr contains now an address of the memory where variable n is located, | + | //ptr contains now an address of the memory where |
- | //not a value 10 | + | //variable n is located, not a value 10 |
int k; //Declare another variable | int k; //Declare another variable | ||
k = *ptr; // | k = *ptr; // | ||
</ | </ | ||
- | Simple type variables such as '' | + | Simple type variables such as '' |
- | The importance of pointers is not to be underestimated in this case: you simply | + | The importance of pointers is not to be underestimated in this case: you declare a pointer pointing to the array' |
- | === Defining New Types === | ||
- | == Typedef Specifier == | ||
- | A '' | ||
- | <code c> | ||
- | typedef struct {int x; int y;} tWaypoint; //Declare complex type named waypoint | ||
- | ... | ||
- | //Declare a variable of the type of tWaypoint | ||
- | tWaypoint wp1; | ||
- | </ | ||
- | |||
- | == Enum Declaration == | ||
- | In the case a new enumeration is needed, it is possible to declare an own one that presents labels instead of values, i.e.: | ||
- | <code c> | ||
- | |||
- | </ |