Bitwise Parameters & Operations

When doing bitwise comparisons, always use the AND and the OR functions, never the && or the || versions.

Several functions have bitwise parameters, meaning that every bit value in the parameter has its own meaning. You can change the behavior of the function or switch features on and off by setting each matching bit to 1 or 0.

For example, the Style parameter of the Window function is described (in part) as follows:

Required. Any numeric expression that gives the style of the window when it opens. This parameter has no effect after the window has opened. Style is formed by adding together bit values from the table below

Bit Number

Description

0 Enable system close button (if bit 1 is set)
1 Show title bar
2 Thick border, not resizable
3 Thin border, not resizable
4 Enable minimize button (if bit 1 is set )

... etc.

In use, the code might look like the following: (Style parameter in bold under a bold comment line.)

  Window(  0,   0           { Upper left corner   },
         300, 70            { View area           },
         300, 70            { Virtual area        },
         Graphics()         { Start user graphics },
          {65432109876543210}0b00010000000110011, WinTitle, 0, 1);

Note the "0b" beginning the set of 0's and 1's, specifying that this is to be read as bits, not a base-10 integer value.

The comment line is added as a convenience to help the reader count bits. Note that the count does not start at 6, it starts at the right-most 0. In VTScada and most other programming languages, bitwise values are read from right to left , always starting with zero (0). When you reach the second 0 from the right, read that as 10, followed by 11, 12, etc. Thus in this example, there are 17 bit positions shown, numbered from 0 to 16.

We refer to a bit being "set" when its value is 1. If a bit position holds a 0, then that bit is "not set". Referring back to the example and the table before that, we see that bits 0 and 1 are set, therefore the window will have a system close button and a title bar. Bit 4 is set, therefore there will be a minimize button. For the remainder, refer to the documentation for the Window function.

 

All values of every type are stored as bits, whether represented in a bitwise format (0b...), as an integer, a hexadecimal value, or any other format. In VTScada (and most other programming languages) it is common to use an integer or hex value for brevity when specifying bitwise parameters. The following table provides an example using one byte. (You need only provide as many bits as you want to set, but it's standard practice to use a minimum of 8, or one byte.)

As shown, if your goal is to set bits 0, 1, and 3, it's shorter to write "11" or "0xB" than "0b00001011".

Bitwise value Decimal value Hexadecimal value
0b00000000 0 0
0b00000001 1 1
0b00000010 2 2
0b00000011 3 3
0b00000100 4 4
... ... ...
0b00001010 10 A
0b00001011 11 B

 

To check whether a particular bit is set, use the AND function to compare against a mask. (A "mask" is just a value that has set only the bits you want.) The AND function (but not && operator) will compare each bit in one value to each matching bit in the next, setting bits in the result to 1 only if both are set in the values being compared. For example if you are interested in knowing whether bit #3 is set, an AND comparison of 0b00001000 and 0b10101100 will return 0b00001000, which is TRUE. If you checked bit 1 (0b00000010) the return would be 0b00000000, which is FALSE.