Bitwise (or logical) operators are arithmetic instructions that compare each bit of an integer value independently. For example, a bitwise “And” instruction would result in a 1 value for each bit that is 1 in both numbers. The INFORM language supports the following bitwise operations:
- And
- Or
- Xor
- Not
The following sections will describe each of these instructions in more detail and then provide a higher-level application example.
And Instructions
The And instruction has the form “And <Data1> <Data2>” which carries out the operation “Data1 = Data1 And Data2”. Data1 must be a B (or LB) Variable, and Data2 can either be a Constant or a B (or LB) Variable. For example, consider the code below:
This code will perform the operation “B000 = 103 And 50”. The resulting value of B000 in this case will be 34 as shown below.
Or Instruction
The Or instruction has the form “Or <Data1> <Data2>” which carries out the operation “Data1 = Data1 Or Data2”. Data1 must be a B (or LB) Variable, and Data2 can either be a Constant or a B (or LB) Variable. For example, consider the code below:
This code will perform the operation “B000 = 103 Or 50”. The resulting value of B000 in this case will be 119 as shown below.
Xor Instruction
The Xor instruction has the form “Xor <Data1> <Data2>” which carries out the operation “Data1 = Data1 Xor Data2”. Data1 must be a B (or LB) Variable, and Data2 can either be a Constant or a B (or LB) Variable. For example, consider the code below:
This code will perform the operation “B000 = 103 Xor 50”. The resulting value of B000 in this case will be 85 as shown below.
Not Instruction
The Not instruction has the form “Not <Data1> <Data2>” which carries out the operation “Data1 = Not Data2”. Data1 must be a B (or LB) Variable, and Data2 can either be a Constant or a B (or LB) Variable. For example, consider the code below:
This code will perform the operation “B000 = Not 50”. The resulting value of B000 in this case will be 205 as shown below.
Application Example
One common use for bitwise/logical operators is in connection with I/O Signals. For example, suppose there is a gripper whose “open and ready” state consists of evaluating 4 Input Signals (e.g. Input #s 37-40). These signals make up with top 4 bits of Input Group #5, and the And instruction can be used to verify if these bits are turned on. This can be done by using an integer value of 240 which has the binary representation of the top 4 bits being turned on (11110000). Thus, the result of the And instruction will be 240 if those 4 bits are turned on (regardless of other bit values in the group).
The code below shows an example of this. The DigitalIn instruction can be used to read the value of an Input Group into a B Variable (see ). Then, the And instruction carries out the operation “B001 = B001 And 240”. Finally, this value can be checked against 240 and subsequent actions can be taken.
Comments
0 comments
Please sign in to leave a comment.