Opto Script: Bitwise Operation


I want to represent 8 (on/off) signals with bits.
How do you represent bit notation in Opto Script:
Can I write:
nSignals = 0b0000_0000; what is opto script equivalent?

How do I set 2nd bit in Opto? result: nSignals = 0b0000_0010

How do I set multiple bit (4th and 6th) in Opto? result: nSignals = 0b0010_1010

How do I reset bit (6th) in Opto? result: nSignals = 0b0000_1010

How do I reset Multiple bit (2nd and 4th)? result: nSignals = 0b0000_0000

how do you merge 2 binary notation:
plus 0b0000_0110
equals 0b0110_0110


I don’t think OptoScript supports binary literals, but it does support hexadecimal:

nSignals = 0x00;
nSignals = nSignals bitor 0x02; //set 2nd
nSignals = nSignals bitor 0x28; //set 4th and 6th
nSignals = nSignals bitand 0xDF; //reset 6th
nSignals = nSignals bitand (bitnot 0x20); //reset 6th alternative
nSignals = nSignals bitand (bitnot 0x28); //reset bits 2 and 4
nSignals = 0x30 bitor 0x03; //merge? I think this is what you mean…

These operators are in the PAC Control Help under Using OptoScript/OptoScript Expressions and Operators/Using Bitwise Operators
There are also various bit commands (like bit testing and rotate, etc.) you will find under Logical Commands in the PAC Control Command Help that may make working with bits easier.


Thanks Philip.

That gave me an idea that OptoScript does not support binary notation.
Manual is not really clear about bit operators, because examples use decimals.

But I found commands that really helps:
set all bits:
nSignals = 0xFF;

Test bit if set:
if (IsBitOn(nSignals, 3)) then…

Clear bit-3:
nSignals = BitClear(nSginals,3);