How to Use Bitwise Operators and Bit Shifting in WinCC C-Scripts for PCS 7 Status Tag Decoding
In WinCC ANSI C scripting, the ampersand symbol '&' functions as the bitwise AND operator, while '>>' is the bitwise right shift operator. Unlike arithmetic addition or logical evaluation, bitwise operations evaluate integers at the binary level. In PCS 7 picture navigation scripts, the expression 'nCallFrom & 0xF' applies a bitmask (hexadecimal 0x0F, binary 0000 1111) to isolate the lower 4 bits representing a button number, whereas '(nCallFrom & 0xF0) >> 4' masks the upper 4 bits and shifts them right by 4 positions to decode the operating area number.
Table of Contents
- Binary Arithmetic and Bitwise Operators in ANSI C for WinCC
- Bitwise AND (&) vs Logical AND (&&)
- Bitwise Shift Operators (>> and <<) Explained with Numerical Examples
- Application in PCS 7 Faceplates and Picture Hierarchy Scripts
- Summary of C-Script Bitwise and Shift Operators
- Verification
- Limitations and Operational Precautions
- Engineer FAQ
- Technical References
- Related Articles
- Related Tags
Binary Arithmetic and Bitwise Operators in ANSI C for WinCC
WinCC Graphics Designer utilizes an ANSI C script interpreter (pdlrt.exe) to execute dynamic graphic actions, button clicks, and picture navigation calls. In PCS 7 system scripts—such as @PCS7Typicals.pdl, @screen.pdl, or custom faceplate callers—multiple pieces of state information are frequently packed into a single integer or double-word tag to optimize communications across the terminal bus and plant bus.
To extract or encode packed information, C scripts rely on bitwise operators:
* & (Bitwise AND): Compares bits of two integer operands; each resulting bit is 1 only if both corresponding bits are 1.
* | (Bitwise OR): Resulting bit is 1 if either corresponding bit is 1.
* ^ (Bitwise XOR): Resulting bit is 1 if the corresponding bits differ.
* ~ (Bitwise NOT / One's Complement): Inverts all bits.
* >> (Bitwise Right Shift): Shifts bits to the right by a specified number of positions.
* << (Bitwise Left Shift): Shifts bits to the left by a specified number of positions.
Bitwise AND (&) vs Logical AND (&&)
A frequent source of confusion among automation engineers is distinguishing bitwise operators from logical operators:
- Bitwise AND (
&):
* Evaluates two numbers bit-by-bit.
* Example:51 & 1551decimal =0x33hex =0011 0011binary15decimal =0x0Fhex =0000 1111binary0011 0011 & 0000 1111=0000 0011binary =3decimal (or0x03hex).- This operation acts as a bitmask: bits set to 1 in the mask (
0x0F) allow the corresponding bits of the source number to pass through, while bits set to 0 are forced to zero.
- Logical AND (
&&):
* Evaluates boolean truth values (non-zero = TRUE, zero = FALSE).
* Example:(x > 10) && (y < 20)
* Returns purely boolean1(TRUE) or0(FALSE) and employs short-circuit evaluation.
* Using&where&&is intended evaluates every operand without short-circuiting, whereas using&&where&is intended destroys integer bitmasks.
Bitwise Shift Operators (>> and <<) Explained with Numerical Examples
The bitwise shift operators move the binary representation of a value left or right:
- Right Shift (
>>):
* Shifts bits to the right byNpositions. Upper bits are filled with zeros (for unsigned integers), and lower bits are discarded.
* Shifting right by 1 is mathematically equivalent to integer division by 2 (x / 2).
* Shifting right by 4 is mathematically equivalent to dividing by 16 (x / 16orx / 2^4).
* Example from PCS 7 script:nOpArea = (nCallFrom & 0xF0) >> 4;- Suppose
nCallFrom = 51(0x33hex,0011 0011binary). 51 & 0xF0: Masking with0xF0(1111 0000binary) yields0011 0000binary (48decimal,0x30hex).- Shifting right by 4 (
0011 0000 >> 4): The bits0011move to the lower 4 positions:0000 0011binary (3decimal). - Thus, the upper nibble (representing the Operating Area ID) is extracted cleanly as the integer
3.
- Suppose
- Left Shift (
<<):
* Shifts bits to the left byNpositions, filling vacated lower bits with zeros.
* Shifting left by 1 multiplies the integer by 2 (x * 2).
* Shifting left by 4 multiplies the integer by 16 (x * 16).
Application in PCS 7 Faceplates and Picture Hierarchy Scripts
In PCS 7 Picture Tree Manager and standard APL faceplate scripts, bit manipulation is heavily utilized to optimize communication and standardize function calls:
// Example WinCC C-Action: Decoding packed parameter nCallFrom
#include "apdefap.h"
void OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName)
{
long nCallFrom;
long nButtonNumber;
long nOpArea;
// Retrieve packed tag representing picture call context
nCallFrom = GetTagWord("System_PictureCallContext"); // Assume value is 51 (0x33)
// Extract Button Number (Lower 4 bits: 0..15)
nButtonNumber = nCallFrom & 0x0F;
// Extract Operating Area (Upper 4 bits: 0..15)
nOpArea = (nCallFrom & 0xF0) >> 4;
// Diagnostic trace to apdiag window
printf("Decoded Context - Operating Area: %ld, Button: %ld\r\n", nOpArea, nButtonNumber);
// Call picture based on decoded area
if (nOpArea == 3)
{
OpenPictureArea3(nButtonNumber);
}
}
This packing technique allows a single 16-bit or 32-bit tag to transfer multiple status indicators (alarm state, maintenance lock, manual mode, operator authorization) simultaneously across the network in a single read cycle.
Summary of C-Script Bitwise and Shift Operators
The table below summarizes the ANSI C bitwise operators supported by the WinCC C-Script engine and their functional behaviors.
A & B1100 & 01010100b (4 decimal)A | B1100 | 01011101b (13 decimal)A ^ B1100 ^ 01011001b (9 decimal)~A~00001100 (8-bit)11110011b (243 decimal)A >> N12 >> 1 (1100 >> 1)0110b (6 decimal)A << N5 << 1 (0101 << 1)1010b (10 decimal)Verification
To verify bitwise calculations in WinCC C-scripts:
1. Open the WinCC Graphics Designer and create a temporary test button.
2. In the Mouse Click event, write a C-script utilizing printf statements to print the results of the bitwise expressions.
3. Open apdiag.exe (C:\Program Files (x86)\Siemens\WinCC\uTools\apdiag.exe) and set focus on the Output Window.
4. Launch WinCC Runtime, click the test button, and observe the output in APDIAG:
* Verify that 51 & 0xF prints 3.
* Verify that (51 & 0xF0) >> 4 prints 3.
5. Verify with edge cases (e.g., input values 0, 255, 0xFFFFFFFF) to ensure no sign-extension errors occur.
Limitations and Operational Precautions
- Always use unsigned integer types (
unsigned int,DWORD,BYTE) when performing bit shifts; right-shifting signed negative integers triggers arithmetic shift behavior (filling upper bits with 1s rather than 0s). - Do not confuse bitwise AND (
&) with the address-of operator (&before a variable name like&myVar), which retrieves a memory pointer in C. - Avoid heavy bit-manipulation loops inside cyclic C actions (e.g., 250ms triggers); execute bitwise decoding only on event triggers (such as tag changes or mouse clicks) to prevent WinCC runtime thread exhaustion.
- Ensure operator precedence is clearly defined using parentheses; shift operators (
>>,<<) have lower precedence than arithmetic operators (+,-).
Engineer FAQ
Why did 65 + 15 not equal 65 & 15?
+ is arithmetic addition, so 65 + 15 = 80. & is bitwise AND: 65 decimal is 0100 0001 binary and 15 decimal is 0000 1111 binary. Comparing them bit-by-bit yields 0000 0001 binary, which equals 1 decimal. Bitwise AND filters overlapping active bits, whereas addition sums quantities.
What is the difference between & and && in an if condition?
&& is a logical AND that evaluates whether both conditional statements are true, stopping evaluation immediately if the first expression is false (short-circuit). & is a bitwise operator that computes the binary intersection of two integer values.
Why does WinCC pack multiple values into a single integer tag?
Packing multiple boolean status flags or small integers into a single 16-bit or 32-bit tag significantly reduces communication bandwidth and WinCC tag license consumption, allowing rapid synchronization between the AS and OS.
Technical References
- WinCC Scripting: VBS, ANSI-C, VBA Operating Manual
- Siemens Industry Online Support FAQ: Bitwise Operations in WinCC C-Scripts
Related Articles
- Writing High-Performance WinCC C-Actions and Avoiding Thread Deadlocks
- Decoding PCS 7 APL Status Words Status1, Status2, and Status3
- Debugging WinCC Scripts Using APDIAG and Diagnostics Tracing
Related Tags
- WinCC C-Script
- Bitwise Operators
- Bit Shift
- Graphics Designer
- APDIAG