Understanding Variable Arithmetic in Onboard Programming
Table 1.
![]() | In this code we have two variables ‘x’ and ‘y’ and we are adding them, the result here is stored in a third variable. In C you will have the following: z = x + y; Or this if you want to same variable to keep the new value: x = x + y; |
Figure 1.

Figure 2.
Wiring diagram of the Add Variables function. We are declaring three variables, 0x01, 0x02 and 0x03, with values 3, 5 and 0 respectively, then we add variable 0x01 and 0x02 and we specify that the ‘return vector’ or place to store the result will be in 0x03. So after executing this code you will have ‘8’ in 0x03. If we would like to save space and want to store the result in one of the variables been added you just wire the same variable to the return vector, like this:

Figure 3.
Which is equivalent to: x = x + y in C. In the arithmetic functions you have access to the following operations:
- 1. Add
2. Subtract
3. Multiply
4. Divide
5. AND
6. OR
7. Ex-OR
8. Invert (NOT)
9. Logical Shift
10. Load Constant to a variable
11. Read Constant from a variable
For all number functions (add, subtract, multiply, divide) the result can be returned to a new variable or one of the two input variables, returned to the host computer, or thrown away. In all cases the EQUAL condition code is set if the result is equal to zero (0) and GREATER THAN and LESS THAN in case the value is greater than zero or less than zero respectively.
For all bit functions, the result can be returned to a new variable or one of the two input variables, returned to the host computer, or thrown away. In all cases the EQUAL condition code is set True if the result equals zero (all bits low) and False if any bit is set. The GREATER THAN and LESS THAN codes are also set but can be confusing after logical bit wise operations. For the bit functions examples are as follows:
AND.
If the values in Var 1 and Var 2 are 0x0000 1234 and 0x0000 EEEE, respectively, the result of the bit wise AND is 0x0000 0224, which is NOT EQUAL to zero.
OR.
If the values in Var 1 and Var 2 are 0x5A5A 1234 and 0x8282 0000, respectively, the result of the bit wise OR is 0xDADA 1234, which is NOT EQUAL to zero.
Ex-OR.
If the values in Var 1 and Var 2 are 0x5A5A 1234 and 0xFFFF 4321, respectively, the result of the bit wise XOR is 0xA5A5 5115, which is NOT EQUAL to zero.
Invert.
If the value in Var 1 is 0x0000 5A5A, the result of the bit wise NOT is 0xFFFF A5A5. The EQUAL condition code is set to False.
Logical Shift.
If the value in Var 1 is 0x0000 F002 and Logical Shift = –1, this VI returns 0x00007801.
With all this functionality you can program complex arithmetic operations inside an onboard program, which gives you more options to migrate your code to the onboard processor. Is always a good practice to read thoroughly the software manual so you will be aware of ‘all’ the important notes and features of a function.
Reader Comments | Submit a comment »
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).

