Documentation Center |
On this page… |
---|
The table below summarizes the interpretation of all binary operators in C charts. These operators work with the following order of precedence (0 = highest, 10 = lowest). Binary operators evaluate from left to right.
You can specify that the binary operators &, ^, |, &&, and || are interpreted as bitwise operators in Stateflow^{®} generated C code for a chart or for all the charts in a model. See these individual operators in the table below for specific binary or bitwise operator interpretations.
Example | Precedence | Description |
---|---|---|
0 | Operand a raised to power b Enabled when you clear Enable C-bit operations in the Chart properties dialog box. See Specify Chart Properties. | |
1 | Multiplication | |
1 | Division | |
1 | Remainder | |
2 | Addition | |
2 | Subtraction | |
3 | Shift operand a right by b bits. Noninteger operands for this operator are first cast to integers before the bits are shifted. | |
3 | Shift operand a left by b bits. Noninteger operands for this operator are first cast to integers before the bits are shifted. | |
4 | Comparison of the first operand greater than the second operand | |
a < b | 4 | Comparison of the first operand less than the second operand |
a >= b | 4 | Comparison of the first operand greater than or equal to the second operand |
a <= b | 4 | Comparison of the first operand less than or equal to the second operand |
a == b | 5 | Comparison of equality of two operands |
a ~= b | 5 | Comparison of inequality of two operands |
a != b | 5 | Comparison of inequality of two operands |
a <> b | 5 | Comparison of inequality of two operands |
6 | One of the following:
| |
7 | Bitwise XOR of two operands Enabled when you select Enable C-bit operations in the Chart properties dialog box (default). See Specify Chart Properties. | |
8 | One of the following:
| |
9 | Logical AND of two operands | |
10 | Logical OR of two operands |
The following unary operators are supported in C charts. Unary operators have higher precedence than binary operators and are evaluated right to left (right associative).
Example | Description |
---|---|
~a | Logical NOT of a Complement of a (if bitops is enabled) |
!a | Logical NOT of a |
-a | Negative of a |
The following unary actions are supported in C charts.
Example | Description |
---|---|
a++ | Increment a |
a-- | Decrement a |
The following assignment operations are supported in C charts.
Example | Description |
---|---|
a = expression | Simple assignment |
a := expression | Used primarily with fixed-point numbers. See Assignment (=, :=) Operations for a detailed description. |
a += expression | Equivalent to a = a + expression |
a -= expression | Equivalent to a = a - expression |
a *= expression | Equivalent to a = a * expression |
a /= expression | Equivalent to a = a / expression |
The following assignment operations are supported in C charts when Enable C-bit operations is selected in the properties dialog box for the chart. See Specify Chart Properties.
Example | Description |
---|---|
a |= expression | Equivalent to a = a | expression (bit operation). See operation a | b in Binary and Bitwise Operations. |
a &= expression | Equivalent to a = a & expression (bit operation). See operation a & b in Binary and Bitwise Operations. |
a ^= expression | Equivalent to a = a ^ expression (bit operation). See operation a ^ b in Binary and Bitwise Operations. |
The address operator (&) is available in C charts for use with both Stateflow and custom code variables. The pointer operator (*) is available for use only with custom code variables.
Note: The parser uses a relaxed set of restrictions and does not catch syntax errors until compile time. |
The following examples show syntax that is valid for both Stateflow and custom code variables. The prefix cc_ shows the places where you can use only custom code variables, and the prefix sfcc_ shows the places where you can use either Stateflow or custom code variables.
cc_varPtr = &sfcc_var; cc_ptr = &sfcc_varArray[<expression>]; cc_function(&sfcc_varA, &sfcc_varB, &sfcc_varC); cc_function(&sfcc_sf.varArray[<expression>]);
The following examples show syntax that is valid only for custom code variables.
varStruct.field = <expression>; (*varPtr) = <expression>; varPtr->field = <expression>; myVar = varPtr->field; varPtrArray[index]->field = <expression>; varPtrArray[expression]->field = <expression>; myVar = varPtrArray[expression]->field;
You can use type cast operators to convert a value of one type to a value that can be represented in another type. Normally, you do not need to use type cast operators in actions because Stateflow software checks whether the types involved in a variable assignment differ and compensates by inserting the required type cast operator of the target language (typically C) in the generated code. However, external (custom) code might require data in a different type from those currently available. In this case, Stateflow software cannot determine the required type casts, and you must explicitly use a type cast operator to specify the target language type cast operator to generate.
For example, you might have a custom code function that requires integer RGB values for a graphic plot. You might have these values in Stateflow data, but only in data of type double. To call this function, you must type cast the original data and assign the result to integers, which you use as arguments to the function.
Stateflow type cast operations have two forms: the MATLAB^{®} type cast form and the explicit form using the cast operator. These operators and the special type operator, which works with the explicit cast operator, are described in the topics that follow.
The MATLAB type casting form has the general form
<type_op>(<expression>)
<type_op> is a conversion type operator that can be double, single, int32, int16, int8, uint32, uint16, uint8, or boolean. <expression> is the expression to be converted. For example, you can cast the expression x+3 to a 16-bit unsigned integer and assign its value to the data y as follows:
y = uint16(x+3)
You can also type cast with the explicit cast operator, which has the following general form:
cast(<expression>,<type>)
As in the preceding example, the statement
y = cast(x+3,uint16)
will cast the expression x+3 to a 16-bit unsigned integer and assign it to y, which can be of any type.
To make type casting more convenient, you can use a type operator that works with the explicit type cast operator cast to let you assign types to data based on the types of other data.
The type operator returns the type of an existing Stateflow data according to the general form
type(<data>)
where <data> is the data whose type you want to return.
The return value from a type operation can be used only in an explicit cast operation. For example, if you want to convert the data y to the same type as that of data z, use the following statement:
cast(y,type(z))
In this case, the data z can have any acceptable Stateflow type.
Using the code replacement library published by Embedded Coder^{®} code generation software, you can replace a subset of arithmetic operators with target-specific implementations. Operator entries of the code replacement library can specify integral or fixed-point operand and result patterns. Operator entries can be used for the following built-in operators:
+ |
- |
* |
/ |
For example, you can replace an expression such as y = u1 + u2 with a target-specific implementation, as long as u1, u2, and y have types that permit a match with an addition entry in the code replacement library.
C chart semantics might limit operator entry matching because the chart uses the target integer size as its intermediate type in all arithmetic expressions. For example, suppose a Stateflow action contains this arithmetic expression:
y = (u1 + u2) % 3
This expression computes the intermediate addition into a target integer. If the target integer size is 32 bits, you cannot replace this expression with an addition operator from the code replacement library to produce a signed 16-bit result, without a loss of precision.
To learn how to create and register code replacement tables, see Introduction to Code Replacement Libraries.