Two's complement and INT overflow
Click the bits or type a number: you see the same value signed and unsigned, the weight of every bit and what happens when a counter goes past its maximum. Press “Maximum + 1” to see the overflow.
| INT | 32767 |
|---|---|
| WORD | 16#7FFF |
Indicative results: always verify with a qualified technician and the applicable standards. Legal notice
The top bit is negative
In two's complement every bit has its normal weight (1, 2, 4, 8…), except the top one which is negative: −128 in a SINT, −32768 in an INT, −2147483648 in a DINT. So 16#FFFF in an INT is −32768 + 32767 = −1, and 16#8000 is −32768, the minimum.
Changing sign: NOT + 1
To go from 5 to −5 invert the bits and add 1: 0000 0000 0000 0101 → 1111 1111 1111 1010 → 1111 1111 1111 1011 = 16#FFFB. The trick works for every number except the minimum: −(−32768) does not exist in an INT and gives −32768 again.
Overflow: the odometer that rolls over
An INT is like an odometer with 65536 positions arranged in a circle. After 32767 comes −32768, after −1 comes 0. An INT part counter that adds one per part turns negative after 32767 parts: that is why counters and positions use DINT (up to 2147483647) or LINT.
Reading the same bits as another type
A negative analog value such as −27648 (16#9400) read as WORD or UINT becomes 37888. Converting INT → DINT the PLC copies the sign (16#FFFF → 16#FFFF_FFFF, still −1); converting WORD → DWORD it adds zeros (16#0000_FFFF, i.e. 65535). Always use the proper conversion (INT_TO_DINT, not a plain word MOVE).
Frequently asked questions
Why does 32767 + 1 give −32768?
32767 is 16#7FFF, a 0 followed by fifteen 1s. Adding 1 gives 16#8000: the top bit, which is worth −32768 in an INT, becomes 1 and all others 0. The result does not fit in an INT and wraps around.
How do you compute two's complement?
Invert all bits (NOT) and add 1. 16-bit example: 5 = 16#0005, NOT = 16#FFFA, +1 = 16#FFFB, which is −5.
Why does an INT go from −32768 to 32767 and not up to 32768?
16 bits give 65536 combinations: half (32768) are negative, the other half holds zero and the 32767 positives.
What does the PLC do on overflow?
The value wraps around as shown here. Siemens sets ENO = FALSE in LAD/FBD boxes (or the OV and OS status bits in STL), Rockwell sets the S:V overflow flag, Codesys carries on silently. For long counters use DINT or LINT.