Bit Addressing


Hi All.
This is a bit related discussion. I’m trying to use a 32 bit integer for my alarm register. I thought it would be better to have one variable instead of 31. Each alarm would toggle it’s respective bit. If the alarm variable was not 0 then I would know an alarm is active.
Here’s what I’ve learned so far:

In PacDisplay if you send a discrete variable, set true, and don’t specify a bit, all 32 bits toggle to 1. This is actually -1 as an integer. So if you are testing for 1 in your strategy the result will be false. If however ,you check if the variable is true, the result will be true as any non-zero value is read as true by the strategy.

In PacDisplay when you send a bit, the variable is Alarm.BIT00
I tried to set a bit in OptoScript like this
Alarm.BIT00 = 1;
The compiler returned:
Error on line 60: Unexpected character "."
Error on line 60: Unable to find variable or command "BIT00"
Error on line 60: syntax error at or near "BIT00"
3 error(s), 0 warning(s)

I tried to use the following
Example from command help>BitChange(SET_FLAG, Bit, Output_Value);
The compiler returned:
Error on line 60: Invalid type for argument #2
1 error(s), 0 warning(s)

I then tried
The compiler returned:
Error on line 60: syntax error at or near "0"
1 error(s), 0 warning(s)

Any suggestions?


Hi Andy,

Bit twiddling can be tricky, and there are multiple ways to set or clear a particular bit from your strategy.

Looks like your first attempt at using the BitChange command was very close, you just had the first and last parameters swapped. You might also consider using the [B]BitSet[/B] and [B]BitClear[/B] commands, which may be less confusing.

Also, you may eventually want to use the bitwise operators available in OptoScript (check out form 1700, the PAC Control User’s guide, and search for “bitwise operators”). Below is an example of how you might use the bitor command to set an additional bit in your Alarm variable (which I called Alarm_Flags here):
[INDENT]// set the 0th (least-significant) bit of Alarm_Flags
// BitChange(SET_FLAG, Bit, Output_Value);
BitChange( 1, 0, Alarm_Flags);

// Set the bit for Alarm_Number (3 in this example)
// without changing any of the other bits
Alarm_Number = 3;
Alarm_Flags = Alarm_Flags bitor (1 << Alarm_Number);
[/INDENT]When you step through and debug your code, you may find that this “BINary” option in the debug window is helpful.

Here I’m showing the Alarm_Flags variable just after calling that BitChange command, then after the bitor assignment above. You can see the individual bits getting set.

Thanks for the good question, keep them coming! I’m just working on some OptoScript tutorial materials and will be sure to include a section on bit manipulation. As you’ve seen, there are usually several ways to do something in PAC Control, it’s just a matter of finding what makes most sense to you, and what’s easiest to maintain.

Write on!


p.s. Here’s another thought: instead of using bits, what about an integer table (still one variable/tag) where each element represents one of these alarms? Given how much memory our controllers have these days, saving memory is not typically a big concern. Saving frustration–by creating strategies and projects that are easy to understand and maintain by even non-bitheads–might be your bigger concern here. Of course, I can name many situations where we HAVE to manipulate bits, so I’m glad you asked this question!