Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 37 ยท Automation Basics

Number Systems and PLC Data Types

Learn binary, hexadecimal and BCD, signed integers, BOOL, INT, DINT, REAL and TIME in PLCs, with runnable C stand-ins for bit patterns, overflow and rounding.

A PLC stores everything as bits. A lamp is one bit; a tank level might be 16 bits; a temperature is a 32-bit floating-point number. To read watch tables, wire an analog card or debug a communications problem, you must be comfortable with binary, hexadecimal and the standard data types.

In this lesson you will convert between number systems, learn the IEC data types and their ranges, and see the two classic traps: integer overflow and floating-point rounding. The runnable examples are C programs because C uses the same fixed-width integers as a PLC.

Binary and hexadecimal

Binary has two digits, 0 and 1. A bit is one binary digit, a byte is 8 bits and a word is usually 16 bits. Reading long binary strings is hard, so PLC tools show groups of four bits as one hexadecimal digit (0-9, A-F).

Decimal Binary (8 bit) Hex
0 0000 0000 00
5 0000 0101 05
10 0000 1010 0A
15 0000 1111 0F
255 1111 1111 FF

IEC notation marks the base: 2#0000_1010 (binary), 16#0A (hex), 8#12 (octal), plain 10 for decimal.

C Stand-in: print numbers as binary and hex (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The IEC data types

Common PLC types

BOOL 0 or 1
INT 16-bit whole number
DINT 32-bit whole number
REAL 32-bit decimal

Pick the smallest that fits

1 / 2
Type Size Range Typical use
BOOL 1 bit FALSE / TRUE Switches, lamps, flags
BYTE 8 bits 0 to 255 Raw bytes
WORD 16 bits 0 to 65 535 Bit groups
INT 16 bits -32 768 to 32 767 Counts, analog raw values
UINT 16 bits 0 to 65 535 Unsigned counts
DINT 32 bits about -2.1 billion to 2.1 billion Big counters, totals
REAL 32 bits about 7 significant digits Temperature, pressure, math
TIME 32 bits milliseconds Timer presets, e.g. T#5s
STRING bytes text Messages, barcodes

WORD and BYTE are bit patterns, while INT and UINT are numbers. They can hold the same bits but you use them differently.

Signed numbers: two's complement

An INT uses two's complement: the top bit marks negative numbers. The 16-bit pattern 1111 1111 1111 1111 is -1, not 65 535. A mismatch between signed and unsigned interpretation is a common reason for strange values from a device.

C Stand-in: the same 16 bits, signed and unsigned (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Overflow

A 16-bit counter cannot count beyond 32 767. In most PLCs adding 1 to the maximum wraps around to -32 768 (some raise an error flag). A production counter that must go past 32 767 should be a DINT.

C Stand-in: INT wrap-around vs DINT (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

REAL numbers and rounding

A REAL stores numbers as sign, exponent and mantissa, and is only an approximation: 0.1 cannot be stored exactly. Two rules help:

  • Never compare two REALs with =. Test whether the difference is smaller than a tolerance.
  • Do math in REAL, but store totals that must be exact (counts, money) as integers.
C Stand-in: REAL is approximate (C float)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

BCD

Binary-coded decimal stores each decimal digit in 4 bits, so 259 is 0010 0101 1001. It still appears with thumbwheel switches, 7-segment displays and older equipment. A BCD word read as a normal integer gives a wrong number, so use a conversion function (often BCD_TO_INT).

C Stand-in: BCD to integer (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

You will use more of these conversions when scaling signals in digital and analog I/O.

Convert types explicitly (INT_TO_REAL, REAL_TO_INT). Many IEC compilers refuse silent mixing of types, which protects you from the bugs shown above.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is 16#FF in decimal?

    FF hex is 15 * 16 + 15 = 255.
  2. 2. What does adding 1 to an INT holding 32767 normally produce?

    A 16-bit signed integer wraps around from its maximum to its minimum.
  3. 3. Which type is best for a total that must count past 100,000 exactly?

    DINT holds 32 bits, which easily covers values above 100,000.
  4. 4. Why should you avoid comparing two REAL values with equals?

    Floating point cannot represent many decimals exactly; compare with a tolerance.

Summary

  • Bits, bytes and words are shown in binary or hex; IEC literals look like 16#FF.
  • Choose between BOOL, INT, DINT, REAL, TIME by range and need.
  • Signed integers use two's complement and can overflow.
  • REAL is approximate; BCD needs conversion.

Next: Digital and Analog I/O