Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:operators [2023/07/10 16:53] pczekalskien:iot-open:introductiontoembeddedprogramming2:cppfundamentals:operators [2023/11/23 10:20] (current) pczekalski
Line 1: Line 1:
-==== Operators and Specifiers ==== +====== OperatorsSpecifiers and Pointers ====== 
 +{{:en:iot-open:czapka_b.png?50| General audience classification icon }}{{:en:iot-open:czapka_e.png?50| General audience classification icon }}\\
 Operators represent mathematical, relational, bitwise, conditional, or logical data manipulations. There are many operators in the C/C++ language. In this chapter, the most important are presented. Logical operators will be shown in the next chapter as they are used together with conditional statements. Operators represent mathematical, relational, bitwise, conditional, or logical data manipulations. There are many operators in the C/C++ language. In this chapter, the most important are presented. Logical operators will be shown in the next chapter as they are used together with conditional statements.
  
-=== Assignment Operator ===+===== Assignment Operator =====
  
   * **Assignment operator ( = )** – the operator that assigns the value on the right to the variable on the assignment operator's left. The left side should represent the variable (must be able to be modified), and the right side can be the number (constant), variable, or expression. In the case of an expression, its value is first calculated, and then the result is assigned to the variable on the left. The work of an assignment operator can be seen in any of the following operation examples.   * **Assignment operator ( = )** – the operator that assigns the value on the right to the variable on the assignment operator's left. The left side should represent the variable (must be able to be modified), and the right side can be the number (constant), variable, or expression. In the case of an expression, its value is first calculated, and then the result is assigned to the variable on the left. The work of an assignment operator can be seen in any of the following operation examples.
  
-=== 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:
 </code> </code>
  
-=== 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 "1" at the chosen bit position, the resulting bit will also be "1". Bitwise and ( & ) means that if at least one bit is "0", the resulting bit is "0". Bitwise operators shouldn't be confused with Logic Operators ( || ), ( && ), which operate on a single boolean logic value. 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 "1" at the chosen bit position, the resulting bit will also be "1". Bitwise and ( & ) means that if at least one bit is "0", the resulting bit is "0". Bitwise operators shouldn't be confused with Logic Operators ( || ), ( && ), which operate on a single boolean logic value.
Line 61: Line 61:
 </code> </code>
  
-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 2while shifting one bit right gives the effect of dividing by 2.
  
 <code c> <code c>
Line 71: Line 71:
 </code> </code>
  
-=== 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
 </code> </code>
-  * **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:
 </code> </code>
  
-=== Defining new types === +==== and * Operators: Pointers and References ==== 
-A ''typedef'' specifier can give another name for existing types or declares a new one. Renaming types is possible, but software development frameworks used have a number of aliases already. It is helpful, however, when combined with enumerations, classes and structures to give them reasonable names and re-use them later in the code to improve their readability. We present more details on structures in the following chapters, but here is an example presenting a reasonable use of the ''typedef'' specifier. +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 of the pointer is equivalent to the length of the memory address (usually 16, 32 or 64 bits). A pointer does not contain a value but instead points to the variable (a memory) where the value is stored. A pointer variable must be initialised and dereferenced with Address-Of and Dereferencing operators.\\
-<code c> +
-typedef struct {int x; int y;} tWaypoint; //Declare complex type named waypoint +
-... +
-//Declare a variable of the type of tWaypoint +
-tWaypoint wp1; +
-</code> +
- +
-=== 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 whose length is equivalent to the length of the memory address (usually 32 or 64 bits). A pointer does not contain a value but rather points to the variable (a memory) where the value is stored. A pointer variable must be initialised and dereferenced with Address-Of and Dereferencing operators.\\+
 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 139: Line 130:
  
 <code c> <code c>
-int n = 10;     //Declare a variable of type int and initialize it with 10+int n = 10;     //Declare a variable of type int and initialise it with 10
 int *ptr;       //Declare a pointer variable. int *ptr;       //Declare a pointer variable.
                 //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 = &n;       //Assign to the pointer ptr an address of the variable n ptr = &n;       //Assign to the pointer ptr an address of the variable n
-                //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;       //Assign k a value that is pointed by ptr k = *ptr;       //Assign k a value that is pointed by ptr
 </code> </code>
 +Simple type variables such as ''int, double, float'' and so on are passed to the function arguments as values, so the original value is copied, and a copy is presented to the function code (more on functions one can find in the [[en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:functions|]]). Modifications to the argument do not change the original value but just a copy. This is not the case when passing a complex type, such as an array, as an argument. 
 +The importance of pointers is not to be underestimated in this case: you declare a pointer pointing to the array's first element and pass it to the function. Then, by modifying the pointer value (an address), it is possible to refer to the following elements of the array. In this case, any modification to the referred array element modifies an original one, so the change in the value is instant. It does not need a return variable from a function.
 +
 +
en/iot-open/introductiontoembeddedprogramming2/cppfundamentals/operators.1689008034.txt.gz · Last modified: 2023/07/10 13:53 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0