Bits, integers, and REAL values

The Core Idea

A data type tells the program what kind of value a tag can hold. A BOOL (Boolean) stores TRUE or FALSE, an integer (INT) stores whole numbers, and a REAL (floating-point number) can represent fractional measurements, each with its own limits.

What you will learn

  • Read a group of TRUE/FALSE bits as a 16-bit word in binary, hexadecimal, and decimal.

  • Explain why adding one to the largest signed 16-bit INT produces a negative value here, and why division into an INT loses a fraction.

  • Choose a useful range check for a changing REAL measurement instead of asking whether it exactly equals a target.

Why it matters

Choosing a type affects the calculation itself, not just its display. Counts, measurements, and working results need types that preserve the information the application needs.

The problem

A counter stores whole counts. A level sensor reports a measured level in %. We want to add one to a count, halve it, and show whether the measured level is close enough to a target level in %. These numbers look similar on a screen, but the way they are stored changes the result.

SignalMeaning
COUNT_INWhole-number sample in counts; starts at 32767 counts.
NEXT_INTCOUNT_IN plus one, stored as a signed 16-bit INT in counts.
HALF_INT / HALF_REALCOUNT_IN divided by two in counts, stored as a whole-number INT or a fractional REAL.
MEASURED_PCTMeasured level in %; starts at 49.9%.
TARGET_PCTDesired level in %; starts at 50.0%.
EXACT_MATCHTRUE only when the two level numbers are exactly equal.
NEAR_TARGETTRUE when the measured level is within 0.2 percentage points of the target, including the limits.

A BOOL is one TRUE/FALSE value. Sixteen bit positions together form the word below. Binary shows each bit as a 0 or 1; hexadecimal groups the same pattern more compactly. The signed decimal reading uses the leftmost bit to indicate a negative number. None of these views changes the bits.

Predict

COUNT_IN starts at 32767 counts, the largest positive signed 16-bit INT. The first rung adds one and stores it in another INT.

Run or step the demo once. Watch Next INT, then change Whole count to 7 counts and step again. Compare Half as INT with Half as REAL.

Try it

Use the bit and word inspector. Toggle bit 0, then bit 3. Notice that each bit is a BOOL state while the whole word has one numeric reading. Use Reset, then Add 1 twice: 32766 → 32767 → −32768 in the signed display. The hexadecimal display moves from 16#7FFF to 16#8000 at the wrap.

In the demo above, set COUNT_IN to 7 counts and step once. HALF_INT shows 3 counts while HALF_REAL shows 3.5 counts. Set it to −7 counts too: the INT result is −3 counts because this simulator drops the fractional part toward zero when storing into an INT.

Now leave TARGET_PCT at 50.0% and set MEASURED_PCT to 49.9%, 50.0%, and 50.1%, stepping once after each change. Watch both indicator lights. Exact equality turns on only at 50.0%; the useful range check stays on for all three readings.

Look closer

At the overflow boundary, the bits go from 0111 1111 1111 1111 to 1000 0000 0000 0000. The word still has sixteen bits. The signed interpretation changes from 32767 to −32768. Hexadecimal is a shorter way to inspect those same bits, not a different stored number.

Look at the two division destinations after entering 7 counts. The division calculation is 3.5 counts in both rungs. Storing it in HALF_INT discards the .5; storing it in HALF_REAL keeps a fractional result. The destination type matters as much as the operation.

For the level, EXACT_MATCH asks whether two numbers have precisely the same value. A sensor can vary between scans, and many decimal fractions are only approximate in a REAL. Even a perfectly useful reading near the target can fail exact equality. NEAR_TARGET instead checks 49.8% through 50.2% while the target is 50.0%. These are percentage points around the target, not 0.2% of the target.

Change it

Open the guided program. Its NEAR_TARGET rung incorrectly uses exact equality. Keep the count and division rungs. Replace that decision with two comparisons in series: measured level at or above LOW_PCT, and measured level at or below HIGH_PCT, followed by the existing NEAR_TARGET output coil. The program already calculates both limits from TARGET_PCT.

Check 49.9%, 50.0%, and 50.1% against a 50.0% target. Then move the target to 60.0% and check 60.1%. Run the guided checks.

Check yourself

The challenge leaves the NEAR_TARGET rung empty. Build the two-limit decision yourself. Your result should include readings exactly at the lower and upper limits, and it should follow a changed target. The count and division rungs are already in place so you can focus on the measured value. Run the checks and revise your rung as often as you need.

Hint 1: What does close enough mean?

The measurement must be no lower than one limit and no higher than the other. Both conditions must be TRUE together.

Hint 2: Use the limits already calculated

Compare MEASURED_PCT with LOW_PCT and HIGH_PCT. Include equality at each edge of the range.

Hint 3: Put the decision in one rung

Put a greater-than-or-equal comparison with LOW_PCT, then a less-than-or-equal comparison with HIGH_PCT, then the NEAR_TARGET coil in series.

Show a worked solution

Keep the two rungs that calculate LOW_PCT = TARGET_PCT − 0.2 and HIGH_PCT = TARGET_PCT + 0.2. In the final rung, require MEASURED_PCT ≥ LOW_PCT and MEASURED_PCT ≤ HIGH_PCT before writing NEAR_TARGET. With a 50.0% target, 49.8% and 50.2% are both accepted. At 49.7%, the lower comparison is false. At a 60.0% target, the bounds move to 59.8% and 60.2%.

What happened and why

One bit answers a yes/no question. A 16-bit word can represent a whole-number pattern, but its signed INT range is −32768 through 32767. This simulator wraps an INT result when that range is crossed. Inspect the destination value whenever a calculation approaches a type limit.

An INT destination stores only a whole number, so 7 ÷ 2 becomes 3 there. A REAL destination can store a fractional result, so it becomes 3.5. For measurements, REAL preserves useful fractions, but exact equality remains a brittle way to decide whether a changing signal is acceptable. Choose bounds that match the process requirement; the ±0.2 percentage point band here is an example, not a universal tolerance.

Show the math

For a signed 16-bit INT, the highest positive bit pattern is 0111 1111 1111 1111 (32767). Adding one produces 1000 0000 0000 0000, interpreted as −32768. The full word has 2¹⁶ distinct patterns.

For 7 counts, 7 ÷ 2 = 3.5 counts. HALF_INT truncates it to 3 counts; HALF_REAL retains 3.5 counts. With a 50.0% target and a ±0.2 percentage point allowance, the bounds are 49.8% and 50.2%.

Common mistakes

  • A count suddenly becomes negative: arithmetic crossed the signed 16-bit limit. Check the expected range and choose a suitable type or a limit check before adding.

  • A half-count disappears: the destination is an INT, so it cannot hold a fraction. Store the result in a REAL when fractions matter.

  • An in-range measurement looks rejected: exact equality was used on a changing REAL. Compare against an acceptable range in the same units as the measurement.

See Comparison Instructions for more numeric decisions and Math Instructions for additional calculations.

Enable JavaScript to run the embedded simulator and complete the interactive exercises.