Data Representation and Digital LogicIntroductionComputing systems represent, store, transmit, and process information using physical devices whose most reliable states are commonly interpreted as 0 and 1. In a mechatronic system, these states may represent a switch position, a sensor threshold, a memory cell, an instruction, or a control signal. Understanding data representation and digital logic is therefore essential for programming microcontrollers, interpreting sensor data, designing interfaces, and diagnosing embedded systems. A digital representation is not the information itself; it is an agreed encoding of information. The same binary pattern may represent an integer, a character, a colour, an instruction, or a sensor reading depending on the convention being used. Engineers must therefore understand both the numerical pattern and its interpretation. Learning ObjectivesAfter studying this chapter, you should be able to: - convert values between binary, decimal, hexadecimal, and other number systems;
- perform binary arithmetic and identify overflow;
- explain Boolean variables, operators, and logic gates;
- simplify and evaluate Boolean expressions;
- distinguish combinational from sequential logic;
- describe character, numerical, sensor, image, and audio representations;
- select suitable data units and encodings for mechatronic applications;
- identify quantization, sampling, precision, range, and error limitations.
1. Number Systems and Positional RepresentationA positional number system uses a base, or radix, to determine the value of each digit. If a number has digits dₙdₙ₋₁…d₀ in base b, its value is V = ∑ᵢ₌₀ⁿ dᵢbⁱ Here, b is the base, dᵢ is a digit satisfying 0 ≤ dᵢ < b, and i is the position counted from the radix point. This expression assumes a conventional positional representation. Binary uses base 2 and the digits 0 and 1. Decimal uses base 10. Hexadecimal uses base 16 and the symbols 0–9 and A–F. Octal uses base 8. Hexadecimal is especially useful because one hexadecimal digit corresponds exactly to four binary bits. | System | Base | Digits | Typical use |
|---|
| Binary | 2 | 0, 1 | Digital hardware and machine data | | Octal | 8 | 0–7 | Historical permissions and compact binary notation | | Decimal | 10 | 0–9 | Human numerical communication | | Hexadecimal | 16 | 0–9, A–F | Addresses, colours, machine words |
1.1 Converting Between BasesTo convert an integer from decimal to another base, repeatedly divide by the target base and read the remainders from bottom to top. To convert a non-negative binary integer to decimal, sum the powers of two associated with its 1 bits. For example: (110101₂) = 1×2⁵ + 1×2⁴ + 0×2³ + 1×2² + 0×2¹ + 1×2⁰ = 53₁₀ For hexadecimal conversion, group binary digits from the radix point in groups of four: 1101 0101₂ = D5₁₆ The grouping is a notation convenience, not a change in the underlying value. Fractions use negative powers. For example: (0.101₂) = 1×2⁻¹ + 0×2⁻² + 1×2⁻³ = 0.625₁₀ Not every decimal fraction has a finite binary representation. For example, 0.1₁₀ produces a repeating binary fraction, which is one reason floating-point calculations can contain small rounding errors. Worked Example 1: Decimal to Binary and HexadecimalConvert 173₁₀ to binary and hexadecimal. Repeated division gives remainders: 173 ÷ 2 = 86 remainder 1 86 ÷ 2 = 43 remainder 0 43 ÷ 2 = 21 remainder 1 21 ÷ 2 = 10 remainder 1 10 ÷ 2 = 5 remainder 0 5 ÷ 2 = 2 remainder 1 2 ÷ 2 = 1 remainder 0 1 ÷ 2 = 0 remainder 1 Reading upward gives 10101101₂. Grouping into four-bit units gives 1010 1101₂ = AD₁₆. Thus, 173₁₀ = 10101101₂ = AD₁₆. 2. Bits, Bytes, and Data UnitsA bit is a binary digit. A byte is conventionally eight bits, although historical systems have used other byte sizes. A nibble is four bits. Storage capacity is commonly specified using bytes, whereas communication rates are commonly specified in bits per second. The prefixes require care. In the International System of Units, decimal prefixes are used as follows: 1 kB = 1,000 bytes, 1 MB = 1,000,000 bytes, and 1 GB = 1,000,000,000 bytes. Binary prefixes are KiB, MiB, and GiB: 1 KiB = 1,024 bytes, 1 MiB = 1,024 KiB, and 1 GiB = 1,024 MiB. A vendor or software tool should be checked before interpreting a capacity. With n bits, an unsigned representation has 2ⁿ distinct patterns and represents values from 0 through 2ⁿ − 1. For example, eight unsigned bits represent 0 through 255. This range assumes that all patterns are used for non-negative integers and that no pattern is reserved. 3. Signed Integers and Binary Arithmetic3.1 Unsigned ArithmeticBinary addition follows the same place-value principle as decimal addition: | A | B | Sum bit | Carry |
|---|
| 0 | 0 | 0 | 0 | | 0 | 1 | 1 | 0 | | 1 | 0 | 1 | 0 | | 1 | 1 | 0 | 1 |
For fixed-width arithmetic, a carry beyond the most significant bit is discarded or reported as an overflow condition, depending on the processor instruction. 3.2 Two’s ComplementMost computers represent signed integers using two’s complement. For n bits, the range is −2ⁿ⁻¹ ≤ x ≤ 2ⁿ⁻¹ − 1 This range assumes one bit pattern for each integer and the standard two’s-complement interpretation. For eight bits, the range is −128 to +127. To form the negative of a positive binary number, invert every bit and add 1. For example, +5 is 00000101₂. Inverting gives 11111010₂; adding 1 gives 11111011₂, which represents −5 in eight-bit two’s complement. Two’s-complement addition uses ordinary binary addition, but signed overflow occurs when the mathematical result is outside the representable range. Adding two positive numbers and obtaining a negative result, or adding two negative numbers and obtaining a positive result, indicates signed overflow. Worked Example 2: Signed AdditionUsing eight-bit two’s complement, calculate 37 + (−12). 37 = 00100101₂. 12 = 00001100₂, so −12 = 11110100₂. 00100101₂ + 11110100₂ = 00011001₂ The final carry is discarded. 00011001₂ = 25₁₀, so the result is correct: 37 − 12 = 25. No signed overflow occurs because the operands have different signs. 3.3 Fixed-Point and Floating-Point ValuesA fixed-point format assigns a fixed number of bits to the fractional part. If four fractional bits are used, the stored integer is divided by 2⁴ when interpreted. Fixed-point arithmetic is predictable and efficient on many microcontrollers, but its range and resolution are limited. Floating-point represents a value using sign, significand, and exponent fields. In simplified form, x = (−1)ˢ × m × bᵉ where s is the sign indicator, m is the significand, b is the base, and e is the exponent. Actual formats such as IEEE 754 include normalized representations, special values, rounding rules, infinities, and NaNs. Floating-point is useful for large dynamic ranges but does not represent all real numbers exactly. 4. Boolean Algebra and Logic GatesBoolean algebra operates on variables whose values are usually 0 or 1. It is used to describe switching circuits, conditions in programs, safety interlocks, and digital control decisions. The principal operations are: - NOT: Y = ¬A; reverses the input.
- AND: Y = A · B; produces 1 only when both inputs are 1.
- OR: Y = A + B; produces 1 when at least one input is 1.
- XOR: Y = A ⊕ B; produces 1 when the inputs differ.
In Boolean algebra, + denotes OR and multiplication denotes AND; these symbols do not mean ordinary arithmetic in this context. | A | B | AND | OR | XOR |
|---|
| 0 | 0 | 0 | 0 | 0 | | 0 | 1 | 0 | 1 | 1 | | 1 | 0 | 0 | 1 | 1 | | 1 | 1 | 1 | 1 | 0 |
A NAND gate is NOT-AND and a NOR gate is NOT-OR. NAND and NOR are universal gates: either type alone can be combined to implement NOT, AND, OR, and therefore any Boolean function. 4.1 Fundamental Boolean LawsImportant laws include: A + 0 = A, A · 1 = A A + 1 = 1, A · 0 = 0 A + A = A, A · A = A A + ¬A = 1, A · ¬A = 0 ¬(A · B) = ¬A + ¬B ¬(A + B) = ¬A · ¬B The final two are De Morgan’s laws. They are useful when converting between active-high and active-low control signals or when implementing logic with NAND and NOR gates. Worked Example 3: Safety InterlockA motor may run only when the enable command E is active, the guard is closed G, and an emergency-stop signal S indicates safe status. The Boolean condition is M = E · G · S If E = 1, G = 1, and S = 0, then M = 0. The motor remains disabled even though the command and guard conditions are satisfied. This illustrates that a safety condition should be designed so that an unsafe or uncertain signal prevents operation where appropriate. In real machinery, the complete safety design must also address contact failures, diagnostics, redundancy, and applicable safety standards; a simple Boolean expression is not by itself a safety-rated system. 4.2 Combinational and Sequential LogicCombinational logic produces an output determined only by present inputs. Adders, multiplexers, decoders, and comparators are examples. Sequential logic includes memory, so its output depends on present inputs and prior state. Latches, flip-flops, counters, and registers are examples. A clocked digital system changes state at defined clock events. Timing constraints include setup time, hold time, propagation delay, and clock period. If an input changes close to a sampling edge, a flip-flop may enter metastability. Designers reduce the risk using synchronizers, appropriate timing analysis, and well-defined clock-domain crossings. 5. Character and Text EncodingCharacter encoding maps symbols to numerical code points and then to stored bit patterns. ASCII defines codes for English letters, digits, punctuation, and control characters; it uses seven data bits, often stored in an eight-bit byte. Unicode provides a much larger code space for writing systems, symbols, and emoji. UTF-8 is a variable-length encoding in which common ASCII characters occupy one byte and other characters occupy multiple bytes. Encoding, storage format, and display are distinct. A byte sequence is meaningful only when the receiving system knows the encoding. Incorrect interpretation produces mojibake, in which valid bytes are displayed as incorrect characters. Control characters such as line feed and carriage return may differ across operating systems. Embedded systems also need to define endianness, field lengths, checksums, and framing when exchanging text or binary packets. 6. Representing Sensor and Numerical DataA sensor produces a physical signal, such as temperature, pressure, position, light intensity, or acceleration. A transducer converts a physical quantity into an electrical quantity. An analogue-to-digital converter (ADC) then maps a voltage to a discrete code. For an ideal unipolar n-bit ADC with input range 0 to Vref, the number of codes is 2ⁿ. A common ideal quantization step is q = Vref / 2ⁿ where q is the nominal voltage interval per code, Vref is the reference voltage in volts, and n is the number of ADC bits. Actual ADC transfer conventions may associate code transitions differently, so the data sheet is authoritative. The ideal quantization uncertainty is often approximated as ±q/2 for a rounded measurement, but noise, nonlinearity, reference error, and offset may be larger. If a sensor has linear sensitivity k volts per physical unit and offset V₀, the estimated physical quantity is x = (V − V₀) / k where V is measured voltage, x is the physical quantity, and k must use consistent units. This model applies only over the sensor’s specified linear range. Worked Example 4: ADC ResolutionA 12-bit ADC uses a 3.3 V reference. The number of codes is 2¹² = 4096, so the ideal step is q = 3.3 V / 4096 ≈ 0.000805 V = 0.805 mV This is resolution, not guaranteed accuracy. A sensor connected to the ADC may have noise, calibration error, temperature drift, and nonlinearity. Increasing ADC bit depth does not automatically improve system accuracy if these other errors dominate. Digital sensor data may be transmitted as signed or unsigned integers, fixed-point values, floating-point values, or scaled engineering units. A robust interface specification states byte order, word size, sign convention, scale, offset, units, valid range, missing-value code, and error detection method. 7. Image and Audio Representation7.1 Digital ImagesA raster image is an array of pixels. A grayscale image may assign a numerical intensity to each pixel; an 8-bit grayscale channel commonly represents 256 levels. A colour image often uses red, green, and blue channels. Uncompressed storage is approximately S = W × H × C × B / 8 where S is storage in bytes, W and H are image width and height in pixels, C is the number of channels, and B is bits per channel. The formula excludes headers, padding, and compression. For a 640 × 480 image with three 8-bit channels, the uncompressed pixel data is 640 × 480 × 3 bytes = 921,600 bytes. Compression may reduce storage, but lossy compression changes image data and can remove details important to machine vision. 7.2 Digital AudioAudio conversion samples an analogue waveform at a sampling frequency fₛ. Under the ideal Nyquist condition, a band-limited signal with highest frequency fmax can be reconstructed if fₛ > 2fmax The strict inequality expresses the theoretical condition; practical systems use an anti-aliasing filter and a margin above twice the intended bandwidth. If sampling is too slow, higher-frequency components appear as false lower-frequency components, called aliasing. Uncompressed PCM audio storage is approximately S = fₛ × t × C × B / 8 where t is duration in seconds, C is the number of channels, and B is bits per sample. A one-minute, 44,100 Hz, 16-bit, stereo recording requires approximately 44,100 × 60 × 2 × 16 / 8 = 10,584,000 bytes before file overhead and compression. Quantization introduces amplitude error. Higher bit depth provides more amplitude levels and generally improves signal-to-quantization-noise performance, although analogue noise and converter quality remain important. 8. Data Integrity, Transmission, and StorageRepresentation errors may arise from overflow, truncation, rounding, encoding mismatch, bit corruption, or incorrect interpretation. Engineering systems therefore use validation and error detection. A parity bit makes the total number of 1 bits even or odd. It detects any odd number of bit errors in a protected word but cannot reliably detect an even number of errors. Checksums add selected values and compare the result with a transmitted check value. Cyclic redundancy checks (CRCs) treat data as a polynomial over binary arithmetic and are effective for detecting common burst errors. Error detection does not necessarily correct an error; retransmission or error-correcting codes may be required. Data integrity also depends on software design. Range checks, plausibility checks, sequence counters, timestamps, watchdogs, and fail-safe defaults can reveal stale, missing, or physically impossible sensor data. Logging the raw value together with units, calibration version, timestamp, and quality status supports later diagnosis. 9. Applied Case: Conveyor-Based Sorting CellBackgroundConsider an illustrative automated conveyor cell that sorts components using a photoelectric sensor, a colour camera, an encoder, and a pneumatic actuator. A microcontroller or industrial controller receives sensor data and commands the actuator. ProblemThe original design occasionally misclassified parts and activated the actuator at the wrong position. Investigation showed that the design documentation did not clearly define data representations or timing. AnalysisThe photoelectric sensor was treated as a Boolean input, but electrical noise caused rapid transitions. The encoder count was an unsigned 16-bit value, yet the conveyor could run long enough for the count to wrap around. Camera pixels were stored as three 8-bit channels, but one software module interpreted the order as RGB while another expected BGR. Finally, a multi-byte count was transmitted without an agreed byte order. Approach and SolutionThe engineering team specified a data dictionary: | Signal | Representation | Meaning |
|---|
| Part detected | Debounced Boolean | 1 only after a stable optical threshold | | Encoder count | 32-bit unsigned integer | Count, with documented wrap handling | | Colour pixel | Three 8-bit channels | Explicit channel order and colour space | | Pressure | 12-bit ADC code plus scale | Value converted to kPa with calibration version | | Packet | Fixed fields, byte order, CRC | Reject incomplete or corrupted frames |
A timer-based debounce filter prevented a single noisy transition from generating multiple part events. Encoder differences were calculated using documented modular arithmetic, and software converted raw counts to millimetres using the encoder pitch. A trigger timestamp was associated with each camera image, allowing the controller to estimate actuator position from conveyor speed and transport delay. The communication protocol added a length field, sequence number, explicit endianness, and CRC. OutcomeThe redesigned system reduced classification and positioning faults during testing and made faults reproducible because raw data and quality flags were logged. The improvement did not result merely from using more powerful hardware; it resulted from precise definitions of representation, timing, validation, and physical units. Learning Points- A binary pattern has no meaning without an agreed data type and scale.
- Sensor representation must include noise, calibration, and validity information.
- Timing and data representation are inseparable in control systems.
- Protocol specifications should define byte order, ranges, units, and error handling.
- Testing should include boundary values, overflow, corrupted packets, and missing data.
10. Limitations, Safety, and Contemporary PracticeDigital systems are discrete approximations of continuous physical phenomena. Quantization, sampling, finite word length, latency, and computation time can affect control quality. Engineers should distinguish resolution from accuracy, repeatability from correctness, and detected errors from corrected errors. In safety-related mechatronic systems, a logic expression is only one part of the design. Hazard analysis, independent protection, diagnostic coverage, fault tolerance, secure access, and compliance with applicable standards are required. Safety signals should not be assumed safe merely because they are binary. Cybersecurity is also relevant to data representation. An attacker may exploit unchecked lengths, integer overflows, malformed encodings, replayed sensor packets, or ambiguous units. Secure protocols should authenticate messages where appropriate, validate ranges, reject malformed input, and protect keys. Artificial intelligence and machine-learning systems add further representation issues: training data must be labelled consistently, sensor distributions must be monitored, and confidence or uncertainty should not be confused with physical validity. Best practice is to define representations before implementation. Document every field’s type, width, signedness, units, scale, offset, byte order, valid range, default state, and error semantics. Test with zero, maximum, minimum, negative, fractional, missing, and corrupted values. Use named constants rather than unexplained numeric literals, and review conversions at interfaces between analogue hardware, firmware, networks, and software. Chapter SummaryDigital computing represents information through agreed patterns of bits. Positional notation explains binary, decimal, octal, and hexadecimal systems, while fixed-width representations determine range and overflow. Two’s complement provides a practical representation of signed integers. Boolean algebra and logic gates form the basis of digital decision-making, with combinational circuits depending on present inputs and sequential circuits retaining state. Characters require defined encodings such as ASCII or UTF-8. Sensor values require attention to ADC resolution, calibration, units, scaling, noise, and validity. Images are represented as pixels and channels; audio is represented through sampling and quantization. Data can be corrupted or misinterpreted, so protocols should specify byte order, field formats, checksums or CRCs, and validation rules. In mechatronics, correct representation is inseparable from timing, safety, reliability, and system-level engineering. Key TermsBit: A binary digit with value 0 or 1. Byte: A group conventionally containing eight bits. Radix: The base of a number system. Unsigned integer: A non-negative integer representation. Two’s complement: A standard binary representation for signed integers. Overflow: A result outside the range representable by a fixed-width format. Boolean algebra: Algebra of binary-valued variables and logical operations. Logic gate: A circuit that implements a Boolean operation. Combinational logic: Logic whose output depends only on current inputs. Sequential logic: Logic whose output depends on inputs and stored state. Metastability: An uncertain temporary state caused by an input changing near a sampling event. Character encoding: A mapping between symbols and stored code values. ADC: An analogue-to-digital converter. Quantization: Mapping continuous amplitudes to discrete numerical levels. Resolution: The smallest nominal distinguishable increment of a representation. Sampling frequency: The number of samples acquired per second. Aliasing: False frequency content caused by inadequate sampling. Endianness: The order in which multi-byte values are stored or transmitted. CRC: A cyclic redundancy check used for error detection. Fixed-point representation: A numerical format with a predetermined fractional position. Review QuestionsShort Answer Questions- What is the positional-value principle used by a radix-based number system?
- Why is hexadecimal notation convenient for representing binary data?
- State the eight-bit two’s-complement range.
- What is the difference between resolution and accuracy in an ADC?
- What problem does aliasing create in sampled audio?
Long Answer Questions- Explain unsigned and two’s-complement signed arithmetic, including range and overflow.
- Describe the main Boolean operations and explain De Morgan’s laws with examples.
- Compare combinational and sequential logic and give two examples of each.
- Explain how an analogue sensor signal becomes a calibrated digital engineering value.
- Discuss the representation and storage requirements of a digital image and a PCM audio signal.
Analytical / Application Questions- An eight-bit sensor register contains 11101100₂. Interpret it as both an unsigned integer and a two’s-complement signed integer, and explain why the interpretations differ.
- Design a data specification for a temperature sensor packet containing a signed measurement, timestamp, validity flag, and error check. State field sizes, units, byte order, and at least two validation rules.
- A conveyor controller receives a 16-bit encoder count and a Boolean part-detection signal. Identify at least four representation or timing faults that could cause incorrect actuator positioning, and propose engineering controls for each.
Multiple Choice Questions with AnswersQuestion 1What decimal value is represented by 101101₂? A. 43 B. 45 C. 47 D. 51 Correct Answer: B. 45 Question 2What is the range of an eight-bit unsigned integer? A. −128 to 127 B. 0 to 127 C. 0 to 255 D. 1 to 256 Correct Answer: C. 0 to 255 Question 3Which Boolean operation produces 1 only when two inputs are different? A. AND B. OR C. NAND D. XOR Correct Answer: D. XOR Question 4Using eight-bit two’s complement, what value does 11111101₂ represent? A. −3 B. −2 C. 253 D. 3 Correct Answer: A. −3 Question 5A 10-bit ideal ADC has a 5.0 V reference. What is its nominal step size? A. Approximately 0.488 mV B. Approximately 4.88 mV C. Approximately 48.8 mV D. Approximately 0.00488 mV Correct Answer: B. Approximately 4.88 mV Question 6Which statement about UTF-8 is correct? A. Every character always occupies exactly two bytes. B. It can encode ASCII characters using one byte. C. It represents only numerical digits. D. It cannot represent non-English writing systems. Correct Answer: B. It can encode ASCII characters using one byte. Question 7What is the principal purpose of an anti-aliasing filter before an ADC? A. To increase the number of ADC bits B. To prevent high-frequency components from folding into the sampled band C. To convert binary data into hexadecimal D. To add a parity bit to each sample Correct Answer: B. To prevent high-frequency components from folding into the sampled band Question 8Which type of logic retains information about previous inputs? A. Combinational logic B. Sequential logic C. Purely analogue logic D. Static encoding Correct Answer: B. Sequential logic Question 9For an uncompressed 100 × 100 pixel image with three 8-bit channels, how much pixel data is required? A. 3,000 bytes B. 10,000 bytes C. 30,000 bytes D. 300,000 bytes Correct Answer: C. 30,000 bytes Question 10What does a CRC primarily provide? A. Error detection B. Image compression C. Signed-number conversion D. Analogue amplification Correct Answer: A. Error detection
|