Data conversions
Data conversions occur when mixing data types in programming. When programming ladder diagram, mix data types for the parameters within one instruction or expression.
Instructions execute faster and require less memory if all the operands of the instruction use:
- The same data type.
- An intermediate data type:
- If mixing data types or use tags that are not the optimal data type, the controller converts the data according to these rules:
- Operands are converted according to the ranking of data types from SINT, USINT, INT, UINT, DINT, UDINT, LINT, ULINT, REAL, and LREAL with ranking from 1 (the lowest) to 10 (the highest).TIP: To reduce the time and memory for converting data, use the same data type for all the operands of an instruction.
Convert SINT or INT to DINT or DINT to LINT
A SINT or INT input source tag gets promoted to a DINT value by a sign-extension for Source Tag. Instructions that convert SINT or INT values to DINT values use one of the following conversion methods.
This conversion method | Converts data by placing |
---|---|
Sign-extension | The value of the leftmost bit (the sign of the value) into each bit position to the left of the existing bits until there are 32 or 64 bits. |
Zero-fill | Zeros to the left of the existing bits until there are 32 or 64 bits. |
Logical instructions use zero fill. All other instructions use sign-extension
The following example shows the results of converting a value using sign- extension and zero-fill.
This value | 2#1111_1111_1111_1111 | (-1) |
---|---|---|
Converts to this value by sign-extension | 2#1111_1111_1111_1111_1111_1111_1111_1111 | (-1) |
Converts to this value by zero-fill | 2#0000_0000_0000_0000_1111_1111_1111_1111 | (65535) |
If you use a SINT or INT tag and an immediate value in an instruction that converts data by sign-extension, use one of these methods to handle immediate values.
Specify any immediate value in the decimal radix.
If you enter the value in a radix other than decimal, specify all 32 bits of the immediate value. To do so, enter the value of the leftmost bit into each bit position to its left until there are 32 bits.
Create a tag for each operand and use the same data type throughout the instruction. To assign a constant value, either:
Enter it into one of the tags.
Add a MOV instruction that moves the value into one of the tags.
Use a MEQ instruction to check only the required bits.
The following examples show two ways to mix an immediate value with an INT tag. Both examples check the bits of a 1771 I/O module to determine if all the bits are on. Since the input data word of a 1771 I/O module is an INT tag, it is easiest to use a 16-bit constant value.
IMPORTANT:
- When mixing an INT tag with an immediate value, since remote_rack_1:I.Data[0] is an INT tag, the value to check it against is also entered as an INT tag. When mixing an INT tag with an immediate value, since remote_rack_1:I.Data[0] is an INT tag, the value to check it against is also entered as an INT tag.
- When mixing an INT tag with an immediate value, since remote_rack_1:I.Data[0] is an INT tag, the value to check it against first moves into int_0, also an INT tag. The EQU instruction then compares both tags.
Convert Integer to REAL
The controller stores REAL values in IEEE single-precision, floating-point number format. It uses one bit for the sign of the value, 23 bits for the base value, and eight bits for the exponent (32 bits total). If you mix an integer tag (SINT, INT, or DINT) and a REAL tag as inputs in the same instruction, the controller converts the integer value to a REAL value before the instruction executes.
- A SINT or INT value always converts to the same REAL value.
- A DINT value may not convert to the same REAL value:
- A REAL value uses up to 24 bits for the base value (23 stored bits plus a ‘hidden’ bit).
- A DINT value uses up to 32 bits for the value (one for the sign and 31 for the value).
If the DINT value requires more than 24 significant bits, it might not convert to the same REAL value. If it will not, the controller stores the uppermost 24 bits rounded to the nearest even value.
NOTE:
The Logix Designer application interprets numbers differently depending on whether the controller model is a 5x80 controller or a 5x70 controller. For example:
- For a 5x70 controller, Logix Designer interprets literal 2 as a REAL.
- For a 5x80 controller, Logix Designer interprets literal 2 as a DINT.
Convert DINT to SINT or INT
To convert a DINT value to a SINT or INT value, the controller truncates the upper portion of the DINT and stores the lower bits that fit in the data type. If the value is too large the conversion generates an overflow.
Convert a DINT to an INT and a SINT
This DINT value | Converts to this smaller value | |
---|---|---|
16#0001_0081 (65,665) | INT | 16#0081 (129) |
SINT | 16#81 (-127) |
Convert REAL to SINT, INT, or DINT
To convert a REAL value to an integer value, the controller rounds any fractional part and stores the bits that fit in the result data type. If the value is too large the conversion generates an overflow.
Numbers round as in the following examples.
Fractions < 0.5 round down to the nearest whole number.
Fractions > 0.5 round up to the nearest whole number.
Fractions = 0.5 round up or down to the nearest even number.
Conversion of REAL values to DINT values
This REAL value | Converts to this DINT value |
---|---|
-2.5 | -2 |
-3.5 | -4 |
-1.6 | .2 |
-1.5 | .2 |
-1.4 | .1 |
1.4 | 1 |
1.5 | 2 |
1.6 | 2 |
2.5 | 2 |
3.5 | 4 |
Provide Feedback