# Binary-coded decimals and PLCs

## Binary-coded decimal (BCD) is a class of binary encodings of decimal numbers where each decimal is represented by a fixed number of bits, usually four or eight, which goes against the way humans compute data. This disconnect can cause problems for programmable logic controller (PLC) users.

Computers hate decimals. Just because we have 10 fingers and 10 toes, we think we can dictate what base other computing entities should think in. In fact it's really quite inefficient. It's easy to convert base 2 to base 8 or base 16, which any logical device can handle quite well. Binary-coded decimals (BCDs) are a class of binary encodings, which are usually represented by four or eight bits. It's only humans who need to add up all those individual bits in our registers to convert them to base 10 because that's the way we're programmed to think.

Back in the days before touchscreens, seven-segment displays and thumbwheel switches were used as a numerical interface between humans and programmable logic controllers (PLCs). Even before the PLC, these devices were the only graphical way to interface with circuits numerically. Users could move plugs around like in the days of ENIAC (the first electronic general purpose computer), but it was a lot easier to view and adjust decimal numbers with these devices. The problem was, they were very input/output (I/O) intensive. Each thumbwheel segment requires four inputs (+ power), while each 7-segment display requires four outputs (+2 power connections). Still, it was a lot easier for most of us human types than using pushbuttons and pilot lights to interface directly with signed or unsigned integers in decimals.

A common complaint is the math; every data type has to be explicitly declared on the platform, and converted if data types are not equivalent. Not only that, but the standard timer and counter data types incorporate BCD into their data structures. This is because the structures go all the way back to when people had to deal with things like these thumbwheels and seven-segment displays. In fact, the timer setpoints are still entered as "S5T#3S" for a 3-second setpoint. The timer uses three BCD digits (12-bits) and two extra bits for the time base. This is also true for the counters, meaning that they only count from -999 to +999.

Each four-digit section can only carry bit values from 0000 to 1001; for the next value, rather than indexing to 1010, ("10″ in signed or unsigned decimal or "A" in hexadecimal), the next bit gets bumped to the next section of bits. This means that the last six combinations of bits (A-F) are effectively wasted—not possible in the BCD structure.

The BCD structure or base is also still usable in many of the newer touchscreens, but most programmers tend to choose an integer base to express decimal numbers. BCD is sort of like DOS; engineering schools still touch on it, but people really don't know where it comes from. Hopefully referring back to those old thumbwheels and seven-segment displays will help clear up some of the mystery and "why" of BCD.

**Frank Lamb **is the founder of Automation Consulting Services Inc. This article originally appeared on the Automation Primer blog. Automation Primer is a CFE Media content partner. Edited by Chris Vavra, production editor, *Control Engineering*, CFE Media, cvavra@cfemedia.com.

**ONLINE extra**

For more articles on PLCs and programming, see related articles below.