Overview
This unit examines the system of numeration used in computing and digital electronics. It begins with the concepts of positional notation, radix and place value, then studies the binary, octal and hexadecimal systems used to represent data inside computers. Students learn systematic methods to convert numbers between bases, and gain skill in binary arithmetic: addition, subtraction, multiplication and division. The unit then studies representations of signed integers (sign-magnitude, 1's complement, 2's complement), complement arithmetic and the consequences for overflow and underflow. Fixed-point and floating-point representations are explained, including normalization, rounding, and the IEEE 754 formats that standardise floating-point numbers. The unit also discusses special encodings such as BCD and Gray code, character encodings like ASCII and Unicode, and practical issues like endianness and bitwise operations. These topics matter because all higher-level computing depends on accurate low-level representation and manipulation of numbers: programming, debugging, hardware design and numerical analysis all require a clear grasp of how data is stored and processed. By the end of the unit students should be able to convert numbers between bases, perform binary arithmetic, understand signed formats and floating-point behaviour, detect overflow, and apply appropriate encodings for particular tasks.
Learning Objectives
- Describe different positional number systems and explain the meanings of base, place value and radix point.
- Convert numbers accurately between decimal, binary, octal and hexadecimal systems using systematic methods.
- Perform binary arithmetic operations including addition, subtraction, multiplication and division and identify overflow conditions.
- Explain signed number representations such as sign-magnitude, 1's complement and 2's complement and perform arithmetic using them.
- Use and explain binary-coded decimal (BCD), Gray code and character encodings and state typical applications.
- Explain fixed-point and floating-point representations including normalization, rounding and IEEE 754 formats.
- Detect and explain representation errors such as rounding and truncation and state methods to reduce their effects.
Topics in this chapter
19 topics · tap a topic title to jump straight to it.
Introduction to Number Systems
What is a number system?
A number system is a formal way to represent quantities using a set of symbols (digits) and rules. The most important idea is the concept of a positional number system: the value of each digit depends on its position and the base (also called radix). In everyday life we use the decimal system (base 10) because humans historically counted with ten fingers. Computers, however, prefer base 2 (binary) because physical circuits have two stable states.
Place value and positional notation
In a positional system with base b, a digit in position i contributes digit × b^i to the total value, where i is counted from 0 at the units place and increases leftwards. For fractional parts, negative powers of b are used: the first place right of the radix point is b^{-1}, then b^{-2}, and so on. The allowed digit symbols are integers from 0 to b−1, inclusive. For example, in decimal (b = 10) the number 407.3 means 4×10^2 + 0×10^1 + 7×10^0 + 3×10^{-1}.
Why bases other than decimal?
Different bases suit different technologies. Binary (base 2) maps naturally to logic gates and transistors which detect two states (on/off). Octal (base 8) and hexadecimal (base 16) act as compact notations for binary: three binary bits map to one octal digit and four binary bits map to one hexadecimal digit. Hexadecimal is widely used in programming, debugging and memory addresses because it compresses binary data and is easy to convert to/from binary.
Positional vs non-positional systems
Positional systems are efficient for arithmetic because place values follow geometric progression (powers of the base). Non-positional or additive systems (found historically) do not scale well for computation. Computers require positional systems to implement arithmetic algorithms, storage and transfer of values.
Radix point and representation limits
When using a fixed number of digits, not all numbers can be represented exactly. For integers, a fixed number of bits limits the maximum and minimum values. For fractions, some rational numbers terminate in one base and repeat in another (for example 1/5 terminates in decimal but repeats in binary). This limitation is important when designing algorithms, choosing data types, and handling rounding and overflow.
Summary
Learn base, digit set, place value and radix point. Understand why binary underpins computing and why other bases are useful as compact representations. These foundations make it possible to convert, store and operate on numbers inside digital systems.
- Decimal 407.3 = 4×10^2 + 0×10^1 + 7×10^0 + 3×10^{-1}
- In base 5, number 243_5 = 2×5^2 + 4×5^1 + 3×5^0 = 2×25 + 4×5 + 3 = 63 (decimal)
- Radix point example: 101.11 in base 2 = 1×2^2 + 0×2^1 + 1×2^0 + 1×2^{-1} + 1×2^{-2} = 4 + 0 + 1 + 0.5 + 0.25 = 5.75
- Value = Σ d_i × b^i where i runs over integer and negative positions
- Allowed digits: 0, 1, ..., b−1
Binary Number System
Binary basics
Binary is the base-2 positional system that uses only the digits 0 and 1. In binary each position represents a power of two: the rightmost bit is 2^0 (1), the next is 2^1 (2), then 2^2 (4), 2^3 (8), etc. Because of its two-digit alphabet, binary maps directly to the two stable voltage levels or logic levels used in digital circuits, which is why it is the core representation in computers.
Interpreting binary numbers
To find the decimal value of a binary integer, multiply each bit by its positional weight and sum. For example, 10110_2 = 1×16 + 0×8 + 1×4 + 1×2 + 0×1 = 22 decimal. For fractional binary numbers, negative exponents give fractional weights: 0.01_2 = 0×2^{-1} + 1×2^{-2} = 0.25 decimal.
Storage in bytes and words
Computers group binary digits (bits) into bytes (commonly 8 bits), words (machine-dependent, e.g., 16, 32, 64 bits) and larger units. Bytes are often shown in hexadecimal for compactness, but the underlying storage is binary. Bit positions in a byte are numbered and each position has a fixed weight; this allows masking, shifting and bitwise operations for low-level manipulation.
Conversion methods
Converting a positive decimal integer to binary is done by repeated division by 2: each division yields a remainder (0 or 1) that becomes a binary digit starting from the least significant bit. Converting a fractional decimal to binary uses repeated multiplication by 2: the integer part of each product gives the next fractional binary digit. These algorithmic methods are important for programming conversion routines and for understanding finite precision limits.
Practical binary arithmetic
Binary arithmetic follows simple bit rules: 0+0=0, 0+1=1, 1+0=1, 1+1=0 with carry 1. Multi-bit addition propagates carries through higher bits. Subtraction can be carried out by complement techniques, and multiplication is reduced to shifts and adds. Hardware implements efficient adder circuits such as ripple-carry adders or carry-lookahead adders to speed up arithmetic.
Summary
Master binary interpretation, conversion algorithms and bit grouping conventions (nibbles, bytes, words). These ideas form the practical basis for all digital computation, data representation and bit-level programming.
- Binary 11010 = 1×16 + 1×8 + 0×4 + 1×2 + 0×1 = 26 (decimal)
- Fraction binary 0.101 = 1×2^{-1} + 0×2^{-2} + 1×2^{-3} = 0.5 + 0 + 0.125 = 0.625
- Convert decimal 13 to binary: 13/2=6 R1, 6/2=3 R0, 3/2=1 R1, 1/2=0 R1 → binary 1101
- Integer conversion: divide by 2 repeatedly; remainders form binary digits from LSB to MSB
- Fraction conversion: multiply fractional part by 2; integral parts give binary digits in order
Octal and Hexadecimal Systems
Definition and purpose
Octal (base 8) and hexadecimal (base 16) systems are positional number systems used as compact, human-friendly representations of binary data. Octal uses digits 0–7 and groups binary bits in threes; hexadecimal uses digits 0–9 and letters A–F (for values 10–15) and groups binary bits in fours. Because of these groupings, conversion between binary and octal/hex is straightforward and error-resistant, which makes octal and hex common in low-level computing work.
Hexadecimal details
Hexadecimal is particularly popular in programming, debugging and memory addressing. Each nibble (4 bits) maps to one hex digit; thus a byte (8 bits) is shown as two hex digits. Hex strings are often prefixed by 0x (e.g., 0xA3) or followed by an 'h' in some notations. Hex reduces long binary sequences into shorter readable forms: 11110000_2 becomes F0_16.
Octal usage
Octal was more common in older systems whose word sizes were multiples of 3 bits. It appears in contexts such as UNIX file permission notation (three octal digits represent owner, group and others permissions). Mapping groups of three bits to octal digits makes it convenient where that alignment exists.
Conversion between bases
To convert binary to octal, pad the binary string on the left with zeros so its length is a multiple of 3, then write groups of three bits and translate each group into an octal digit. For hex, pad to a multiple of 4 bits and translate each 4-bit group to a hex digit. Converting from octal/hex to binary is the reverse: replace each digit with its binary group. To convert between decimal and hex/oct, you can either go via binary or use repeated division (by 8 or 16) and remainders.
Practical tips
Remember the hex digit values A=10, B=11, C=12, D=13, E=14, F=15. Use leading zeros when grouping so every group has the required number of bits. When working with bytes and words, hex notation aligns naturally: a 32-bit word becomes 8 hex digits. This alignment is why hex is widely used in assembly language and debugging tools showing memory dumps.
Summary
Octal and hexadecimal are compact aliases for binary segments. Master conversion by grouping bits and using division-remainder methods. Real-world programming and system tools rely heavily on hex, so fluency with it is crucial.
- Binary 10111100 grouped as hex: 1011 1100 → B C → 0xBC
- Binary 10111100 grouped as octal (pad left to 9 bits): 010 111 100 → 2 7 4 → octal 274
- Decimal 255 to hex: 255/16=15 R15 → F; result FF
- Binary to hex: group 4 bits and convert; binary to octal: group 3 bits and convert
- Decimal to base b (integer): divide by b; remainders form digits from LSB to MSB
Base Conversion Methods
Purpose and overview
Converting numbers from one base to another is a routine task in computing. The correct method depends on whether the number is an integer or contains a fractional part, and on the particular bases involved. The main systematic methods are: repeated division for integer parts; repeated multiplication for fractional parts; and grouping via binary for conversions involving bases that are powers of two (binary⇄octal⇄hex).
Integer conversion by division-remainder
To convert a decimal (or any base) integer to base b, repeatedly divide the number by b and collect remainders. Each remainder gives one digit of the result starting from the least significant. Continue dividing the quotient by b until quotient becomes zero. Reading the remainders in reverse order gives the number in the new base. This method works for conversion from decimal to any base and can be applied after first converting from an arbitrary base into decimal if required.
Fractional conversion by repeated multiplication
For the fractional part, multiply the fractional portion by the target base b. The integer part of the product is the next digit after the radix point. Subtract this integer part and repeat with the new fractional part. Continue until the fractional part becomes zero or until desired precision is reached. If the fractional expansion does not terminate, it will repeat and you must round or truncate according to precision requirements.
Conversion via binary groups
When converting between bases that are powers of two (2, 8, 16), using binary as an intermediate is the easiest method. Convert the source number into binary, then group bits into groups of 3 (for octal) or 4 (for hex) both to the left and right of the radix point, padding with zeros where necessary. Translate each group into the corresponding digit. This approach avoids long division and multiplication and is preferred for byte-oriented data conversion.
Conversion between arbitrary bases
To convert from base r to base s where neither is decimal, the simplest practical route is to convert from base r to decimal (or binary) and then from decimal to base s. For very large numbers or when implementing algorithms, digit-array arithmetic simulates divisions and multiplications in the source base to produce digits in the target base without ever forming a large decimal integer.
Precision, repeating fractions and rounding
Not every rational fraction has a finite representation in a given base. For example, 1/3 repeats in base 10, and 0.1 repeats in base 2. When a fractional conversion repeats, stop after a chosen number of digits and apply an appropriate rounding rule. Be aware that rounding accumulates error and can change subsequent computations. When exactness is required (e.g., monetary calculations), choose a representation such as fixed-point or decimal types that can represent required fractions exactly.
- Decimal 45 to binary: 45/2=22 R1, 22/2=11 R0, 11/2=5 R1, 5/2=2 R1, 2/2=1 R0, 1/2=0 R1 → binary 101101
- Decimal 0.625 to binary: 0.625×2=1.25 → 1; 0.25×2=0.5 → 0; 0.5×2=1.0 → 1 → fractional binary 0.101
- Binary 110101 to hex: pad to 8 bits 00110101 → 0011 0101 → 3 5 → 0x35
- Integer: while N>0: quotient = floor(N/b); remainder = N mod b; digits = remainders reversed
- Fraction: while fraction>0 and limit not reached: fraction = fraction × b; digit = floor(fraction); fraction = fraction − digit
Signed Number Representations: Sign-Magnitude
Why signed representations are needed
Computers must handle both positive and negative integers. Representations must allow concise storage and arithmetic. Sign-magnitude is the simplest signed format conceptually: one bit is reserved to indicate sign and the remaining bits store the magnitude (absolute value) in ordinary binary. The MSB (most significant bit) is usually the sign bit: 0 for positive, 1 for negative.
Sign-magnitude layout
For an n-bit word in sign-magnitude, bit n−1 is the sign bit. The lower n−1 bits hold the magnitude in standard binary. For example, with 8 bits: +5 is 00000101 and −5 is 10000101. This direct mapping makes it easy to read the sign and magnitude at a glance and to display numbers for human consumption.
Arithmetic in sign-magnitude
Arithmetic is more involved than with two's complement. To add two numbers, you must consider their signs: if signs are the same, add magnitudes and keep the sign; if signs differ, subtract the smaller magnitude from the larger and assign the sign of the larger magnitude. This requires hardware or software to do magnitude comparison and conditional subtraction, which complicates implementation. Subtraction similarly requires sign analysis and magnitude operations. Because of this complexity, modern CPUs avoid sign-magnitude for arithmetic operations.
Representation of zero and redundancy
A notable disadvantage is that sign-magnitude allows two encodings for zero: +0 (all bits zero) and −0 (sign bit 1 and all magnitude bits zero). This redundancy complicates comparisons and canonical representations and wastes available bit patterns. Also range is symmetric but arithmetic handling of carries and borrows across the sign bit is awkward.
When sign-magnitude is used
Sign-magnitude appears rarely in arithmetic units, but it may be used in specialized formats such as floating-point mantissas (where sign and magnitude separation is useful for display or certain algorithms) or in teaching to illustrate signed number concepts. Because sign-magnitude preserves a human-friendly sign bit, it can be useful for input/output formats or specific hardware where sign handling is separate from magnitude processing.
Summary
Sign-magnitude is simple to understand but cumbersome for arithmetic. Students should learn it to compare with complement systems and appreciate why two's complement became the standard in CPUs.
- 8-bit sign-magnitude: +13 → 00001101; −13 → 10001101
- Adding +7 (00000111) and −5 (10000101) in sign-magnitude: signs differ, compute 7−5=2 and take sign of larger (positive) → +2 = 00000010
- Value = (−1)^s × magnitude where s is sign bit (0 positive, 1 negative)
- Zero representations: +0 = 0...0, −0 = 1 followed by zeros
Signed Number Representations: 1's Complement
Definition and formation
One's complement represents negative numbers by inverting all bits of the positive number. For an n-bit system, form −N by flipping each bit of the binary representation of N. The MSB still functions as a sign indicator (0 for positive, 1 for negative). For example, in 8-bit one's complement, +12 = 00001100 and −12 = 11110011.
Properties and range
The numeric range is symmetric about zero but once again there are two representations of zero: +0 = all zeros and −0 = all ones. This duplication complicates equality tests and makes canonical zero handling necessary in many algorithms. Arithmetic in one's complement often requires an end-around carry rule to produce correct results when adding numbers.
Addition using 1's complement
To add two one's complement numbers, perform ordinary binary addition on n bits. If there is a carry out of the most significant bit, add that carry back into the least significant bit (this is called end-around carry). After performing the end-around carry if necessary, the result is in correct one's complement form. For subtraction, add the one's complement of the subtrahend; remember to apply the end-around carry. This extra step makes hardware and software slightly more complex than plain binary addition.
Advantages and disadvantages
One's complement simplifies obtaining a negative value (just invert bits) and supports certain logical operations directly. However, the presence of two zeros and the need for end-around carry in arithmetic are significant drawbacks. Compared with two's complement, one's complement arithmetic is less straightforward and is largely historical in computing practice.
Applications and historical notes
Some early computers used one's complement arithmetic. It is mainly of pedagogical interest today to illustrate complement systems and the evolution of computer arithmetic. Learning one's complement clarifies why two's complement was adopted: two's complement removes the double-zero problem and simplifies addition by making subtraction just another addition without end-around carry.
Summary
Know how to form one's complement, apply end-around carry for addition, and recognise its limitations compared with two's complement.
- In 8-bit one's complement, +12 = 00001100, −12 = 11110011 (bitwise inversion).
- Add +5 (00000101) and −3 (1111100 adjusted to 8-bit 11111011): 00000101 + 11111011 = 00000000 with carry 1; add end-around carry → 00000001 → +1.
- Negative: −N = bitwise NOT(N) (for fixed n bits)
- Addition rule: sum bits, if carry-out = 1 then result = result + 1 (end-around carry)
Signed Number Representations: 2's Complement
Definition and method
Two's complement is the dominant representation for signed integers in modern computers. For an n-bit word, the two's complement representation of −N is obtained by taking the bitwise inversion (one's complement) of N and adding 1. This yields a unique representation for every integer in the range −2^{n−1} to 2^{n−1}−1. The MSB acts as the sign bit: 0 indicates non-negative, 1 indicates negative under two's complement interpretation.
Why two's complement is preferred
Two's complement has several practical advantages: addition and subtraction can be performed by the same binary adder circuitry without special sign handling; there is a single zero representation; and the mapping to arithmetic modulo 2^n simplifies wrap-around behaviour. Because of these properties two's complement simplifies CPU design and programming conventions.
Range and asymmetry
The representable range for n bits is asymmetric: you can represent one more negative number than positive. For example, in 8 bits the range is −128 to +127. The pattern 1000...0 is the most negative number and has no positive counterpart within the same bit width. Attempts to represent the absolute value of that number as positive will overflow.
Computing negatives and arithmetic
To compute −N, invert the bits of N and add 1. When adding two n-bit two's complement numbers, perform standard binary addition and discard the carry beyond the n bits; the n-bit result will be correct if no overflow occurs. Overflow detection for signed addition can be done by checking if the carry into the sign bit differs from the carry out of the sign bit or by noting if two operands of the same sign produced a result with the opposite sign.
Relation to modulo arithmetic
Two's complement arithmetic corresponds to addition modulo 2^n. This modular view clarifies wrap-around behaviour and is useful in algorithms that rely on cyclic counters or modular arithmetic. Programmers must be aware that arithmetic can silently wrap unless explicitly checked.
Summary
Two's complement is the standard signed integer representation because it simplifies hardware and software arithmetic. Be able to form negatives, detect overflow and convert between decimal and two's complement binary representations.
- 8-bit: +5 = 00000101, −5 = invert 00000101 → 11111010, add 1 → 11111011
- Add +50 (00110010) and −20 (11101100): 00110010 + 11101100 = 00011110 (carry discarded) → +30.
- Negative: −N = (2^n − N) mod 2^n equivalently invert bits then add 1
- \[Range (n bits): −2^{n−1} to +2^{n−1} − 1\]
Binary Arithmetic: Addition and Subtraction
Binary addition rules
Binary addition uses simple bit rules: 0+0=0, 0+1=1, 1+0=1, 1+1=0 with carry 1 to the next higher bit. For multi-bit numbers, add from least significant bit (LSB) to most significant bit (MSB), propagate carries as needed. Carry propagation can be slow in hardware for long words; faster adder designs (carry-lookahead, carry-select) reduce delay by predicting carry values.
Subtraction methods
Subtraction may be performed by borrow-based digit subtraction, analogous to decimal subtraction, or by complement-based methods. In two's complement systems it is efficient to perform A−B as A + (two's complement of B). This converts subtraction into addition using the same adder circuitry and avoids separate hardware for borrow handling.
Overflow detection
Overflow indicates that the mathematical result cannot be represented in the chosen number of bits. For unsigned numbers, overflow is signaled by a carry out of the MSB. For two's complement signed arithmetic, overflow occurs when adding two operands of the same sign produces a result of the opposite sign. A hardware test compares the carry into and out of the sign bit: if they differ, overflow occurred. Careful handling is needed in software to avoid incorrect logic when overflow happens.
Signed vs unsigned operations
The same bit pattern may represent different values under signed and unsigned interpretations. Addition hardware does not know which interpretation is intended; programmers must use correct data types and overflow checks. For instance, adding two large positive signed values may create a negative result due to overflow, while the same bit pattern interpreted as unsigned could be a valid large positive value.
Practical examples and edge cases
When working with fixed-width registers, adding numbers that exceed the word size causes wrap-around. Many languages provide flags or exceptions to detect overflow; others wrap silently. Understand platform-specific behavior and use larger types or software checks where overflow could be harmful.
Summary
Master bitwise addition and subtraction rules, complement-based subtraction, overflow detection criteria, and differences between signed and unsigned arithmetic. These are essential for systems programming, debugging and understanding hardware limitations.
- Add 0101 (5) and 0011 (3): 0101 + 0011 = 1000 (8) with carries handled LSB→MSB.
- Subtract 9 − 12 using two's complement (4-bit): 9=1001, 12=1100; two's complement of 12 = invert 1100→0011 +1→0100; 1001 + 0100 = 1101 (−3 in 4-bit two's complement).
- Binary addition per bit: sum = (a XOR b XOR carry_in), carry_out = majority(a,b,carry_in)
- Overflow (two's complement): overflow = carry_into_sign_bit XOR carry_out_of_sign_bit
Multiplication and Division in Binary
Binary multiplication concept
Binary multiplication is conceptually identical to decimal multiplication but simpler to implement because each multiplier bit is either 0 or 1. Multiplying by 0 yields 0, multiplying by 1 yields the multiplicand. For multi-bit multiplication form partial products by shifting the multiplicand left according to the position of 1 bits in the multiplier, then add the partial products. This is the schoolbook method that is easy to implement in software and hardware.
Unsigned multiplication
For two unsigned n-bit numbers, the product can occupy up to 2n bits. Perform shift-and-add: for each bit of the multiplier starting from LSB, if bit is 1 add the multiplicand shifted left by that bit index to an accumulator. Hardware implements this with sequential multipliers or parallel structures (array multipliers) for speed. More advanced algorithms (e.g., Booth's algorithm) reduce the number of adds required when runs of ones exist in the multiplier.
Signed multiplication
For signed numbers in two's complement, multiplication needs to handle sign extension correctly. Booth's algorithm is commonly used for signed multiplication because it encodes runs of ones to reduce operations and handles negative multipliers naturally. Alternatively, convert operands to magnitudes, multiply, and apply the final sign to the product. Ensure enough bits are allocated to represent the signed product without overflow.
Binary division methods
Division is essentially repeated subtraction of shifted divisors. Long division in binary mirrors decimal long division: align the divisor with the leftmost portion of the dividend, subtract if possible, write a 1 in quotient, bring down next bit and repeat. Hardware implements restoring and non-restoring division algorithms; some CPU designs include dedicated division circuits while others implement division in microcode or software routines.
Remainder and quotient
Division yields a quotient and a remainder: dividend = divisor × quotient + remainder with 0 ≤ remainder < divisor. For signed division, common practice is to divide magnitudes and assign sign to quotient, and ensure remainder has the same sign as dividend or follows language-specific conventions. Edge cases include division by zero (undefined) and overflow when dividing the most negative number by −1 in two's complement arithmetic (result is out of range).
Practical considerations
Multiplication and division are more costly than addition and shifting in terms of cycles and hardware complexity. Optimizations include using shifts for powers of two, using fixed-point arithmetic when floating-point is unnecessary, and choosing algorithms appropriate to operand sizes and hardware capabilities.
- Multiply 1011 (11) by 110 (6): partial products: 1011×0→0000, 1011 shifted one and ×1→10110, shifted two and ×1→101100; sum appropriately aligned → 1000010 (66 decimal).
- Divide 11010 (26) by 11 (3) using binary long division: quotient 1001 (9), remainder 1 because 3×9 + 1 = 26.
- Maximum product bits: n-bit × m-bit → up to n+m bits
- Unsigned division: dividend = divisor × quotient + remainder with 0 ≤ remainder < divisor
Complement Systems and Arithmetic
Concept of complements
Complements transform subtraction into addition, making arithmetic circuits simpler. In a base-b system with n digits, there are two common complements: the diminished radix complement (b^n − 1 − N) and the radix complement (b^n − N). For binary, these become one's complement (diminished) and two's complement (radix).
How complements work
Using complements, A − B can be computed as A + complement(B) and then adjusting for any final carry according to method used. In binary one's complement, after adding the complement, an end-around carry must be added back. In two's complement, no such adjustment is needed: simple addition yields the correct two's complement result, discarding any carry out of the MSB.
Advantages for hardware
Complement arithmetic lets subtraction be implemented with the same adder used for addition, simplifying ALU design. Two's complement is particularly convenient because it yields a unique representation for zero and arithmetic modulo 2^n so that overflow wraps naturally. These properties made two's complement the standard for integer arithmetic in modern processors.
Modular interpretation
For n-bit two's complement arithmetic, operations correspond to arithmetic modulo 2^n. Negative numbers correspond to their modular residues: the stored bit pattern is equivalent to adding 2^n to a negative number to get a non-negative residue. This modular viewpoint helps predict wrap-around behaviour: adding 1 to the maximum representable unsigned number wraps to zero, and similar behaviour occurs for signed numbers at their bounds.
Practical uses
Complement systems are used in ALUs, embedded processors, cryptographic modular arithmetic and cyclic counters. When designing algorithms that rely on modular properties (for example ring buffers, cyclic redundancy checks), understanding complement arithmetic is essential. Also when diagnosing bugs where signed vs unsigned interpretations cause differences, the complement viewpoint clarifies the outcome.
Summary
Learn the definitions of radix and diminished radix complements, how they simplify subtraction, and why two's complement became the widely used standard due to its arithmetic simplicity and single-zero property.
- Compute −6 in 8-bit: two's complement → invert 00000110 → 11111001, add 1 → 11111010.
- Compute 20 − 37 in 8-bit two's complement: two's complement of 37 is 219 (11011011); 20 + 219 = 239 (11101111) interpreted as −17 in two's complement.
- Radix complement (base b, n digits): b^n − N
- Diminished radix complement: (b^n − 1) − N
Overflow and Underflow
Definitions
Overflow and underflow describe situations where results of arithmetic cannot be represented correctly with the chosen finite representation. Overflow usually refers to values that exceed the maximum representable magnitude; underflow commonly refers to values whose magnitude is too small to be represented in normalized floating-point form and thus may become subnormal or zero. For integers, underflow as a term is rarely used—values below the minimum representable negative integer lead to overflow in the negative direction.
Integer overflow detection
For unsigned integers, overflow is detected by a carry out of the most significant bit. For two's complement signed arithmetic, overflow occurs when two operands of the same sign produce a result of the opposite sign. A practical hardware test is to compare the carry into the sign bit and the carry out of the sign bit; if they differ, overflow occurred. High-level languages differ in behaviour: some saturate, some raise exceptions, others wrap silently. Programmers must be aware of language and platform specifics to avoid bugs.
Floating-point overflow and underflow
In floating-point arithmetic governed by IEEE 754, overflow happens when the magnitude of a result exceeds the largest finite representable value; the result becomes ±infinity or triggers an overflow flag. Underflow happens when a nonzero result is too small to be represented as a normalized number; it may become a denormal (subnormal) number with reduced precision or zero, potentially raising an underflow flag. Rounding and gradual underflow help preserve some information but precision is lost.
Consequences and dangers
Overflow leads to wrap-around (in modular integer arithmetic) or to infinities/nan in floating-point, which can cascade through subsequent calculations creating incorrect results. Underflow reduces precision and can silently degrade numerical accuracy, especially in iterative algorithms. Catastrophic cancellation of precision may occur when subtracting nearly equal numbers, amplifying relative error.
Prevention and handling
To prevent overflow use larger data types (e.g., 64-bit instead of 32-bit), check operands before operations, apply scaling to intermediate results, and use safe arithmetic libraries that detect overflow. For floating-point, use higher precision or reformulate algorithms to avoid extremes. In critical systems, trap and handle overflow explicitly rather than allowing silent wrap-around.
Summary
Detecting and handling overflow/underflow is essential for reliable numerical and systems programming. Understand hardware flags, language semantics and numerical algorithms to reduce errors and ensure correctness.
- 8-bit signed two's complement: adding 100 (01100100) and 50 (00110010) gives 10010110 which is interpreted as −106 → signed overflow occurred.
- Unsigned 8-bit: 200 + 100 = 44 with carry out = 1 indicating overflow and wrap-around; correct modulo 256 result is 44.
- Two's complement overflow: overflow = carry_into_sign_bit XOR carry_out_of_sign
- Unsigned overflow: overflow if carry_out_of_MSB = 1
Fixed-Point Representation
Concept and purpose
Fixed-point representation stores numbers as integers scaled by a fixed factor to represent fractional values without using floating-point hardware. It is useful in embedded systems, digital signal processing and situations where performance, deterministic behaviour and low hardware complexity are required. Fixed-point gives predictable rounding and range but limited dynamic range compared with floating-point.
Notation and scaling
In binary fixed-point, formats are often described as Qm.f where m is bits for integer part (including sign) and f is bits for fractional part. The stored integer value V represents the real number R = V / 2^f. For signed values, two's complement is usually used for the stored integer so negative numbers are naturally handled. Choice of f fixes precision: each LSB represents 2^{-f} in real terms.
Arithmetic rules
Addition and subtraction of fixed-point numbers with the same scaling factor are simple integer operations. For multiplication, multiplying two Qm.f numbers produces a result scaled by 2^{2f}; to restore the original scaling, shift the product right by f bits (or divide by 2^f). Care is needed to allocate sufficient bits to avoid overflow during intermediate multiplication. Division likewise often requires aligning scales or using shifts to preserve precision.
Range and precision trade-off
Choosing f increases fractional precision but reduces the range of representable integer magnitudes because total word size is fixed. Designers must balance precision and dynamic range based on application needs. Saturation arithmetic (clamping results to max/min instead of wrapping) is useful in signal processing to prevent wrap-around artifacts.
Advantages and disadvantages
Fixed-point arithmetic is faster and requires simpler hardware than floating-point. It also yields deterministic rounding. But it cannot represent very large or very small numbers efficiently, and programmers must manage scaling explicitly which increases code complexity. For financial calculations requiring exact decimal fractional representations, decimal fixed-point or integer representation of smallest currency unit may be preferred.
Summary
Fixed-point is a practical technique to represent fractional numbers with integer arithmetic. Master scaling, Q-format notation, and correct rescaling after multiplication/division to use fixed-point safely and effectively.
- Q3.4 format (7 bits total): stored value 0011010 (26 decimal) represents 26 / 16 = 1.625.
- Multiplying two Q2.6 numbers: after integer multiply, shift right by 6 bits to restore scaling.
- Real value = stored_integer / 2^f for binary fixed-point with f fractional bits
- Product scaling: (A×B)/2^f when both A and B are stored with scaling 2^f
Floating-Point Representation and IEEE 754
Motivation and structure
Floating-point representation stores a wide range of real numbers using three fields: sign, exponent and significand (mantissa). It is analogous to scientific notation: a number is represented approximately as (−1)^sign × significand × base^{exponent}. Floating-point gives a large dynamic range while using a fixed number of bits, making it essential for scientific, engineering and graphics computations.
IEEE 754 standard
IEEE 754 standardises binary floating-point formats, rounding rules and special values. The most used formats are single precision (32-bit) and double precision (64-bit). Single precision uses 1 sign bit, 8 exponent bits and 23 fraction bits; double precision uses 1 sign bit, 11 exponent bits and 52 fraction bits. The exponent is stored with a bias to allow positive and negative exponents to be encoded as unsigned fields.
Normalized numbers and implicit leading bit
For normalized binary floating-point numbers, the significand is adjusted so that its integer part is 1. Because this leading 1 is always present for normalized numbers, IEEE 754 omits it from storage (implicit bit), effectively gaining one extra bit of precision. The stored fraction field holds the fractional part of the significand. The actual exponent is E − bias where E is the stored exponent field.
Special cases: zero, denormals, infinities, NaN
Exponent field of all zeros indicates zero or subnormal numbers (denormals) where the implicit leading 1 is absent; this allows representation of values very close to zero but at reduced precision. Exponent field of all ones indicates infinities (if fraction is zero) or NaN (Not a Number) if fraction is nonzero. NaN is used to represent undefined operations like 0/0.
Rounding and accuracy
Floating-point arithmetic requires rounding when results do not fit in the available bits. IEEE 754 specifies several rounding modes; the default is round-to-nearest-even which reduces systematic bias. Floating-point arithmetic is not associative; the order of operations affects results because of rounding. Catastrophic cancellation can occur when subtracting nearly equal numbers, causing loss of significant digits. Awareness of these behaviours is critical when designing numerical algorithms.
Summary
Understand the bit-field layout, bias, normalization, special values and rounding rules of IEEE 754. These principles explain many surprising behaviours of floating-point arithmetic and guide choices for numerical stability and precision in programs.
- Single precision example: +6.5 = 110.1_2 = 1.101 × 2^2 → sign 0, exponent = 2 + 127 = 129 (10000001), mantissa = 101000...0 → hex 0x40D00000.
- Smallest positive normalized single: exponent E=1 → e = −126; value = 1.0 × 2^{−126}.
- \[Value = (−1)^S × (1.F) × 2^{E−bias} for normalized numbers\]
- \[Bias = 2^{k−1} − 1 where k is number of exponent bits (e.g.\]\[k=8 → bias=127)\]
Binary-Coded Decimal (BCD) and Other Codes
What is BCD?
Binary-Coded Decimal (BCD) encodes each decimal digit separately in binary, typically using 4 bits per decimal digit. In packed BCD two decimal digits fit in one byte: the high nibble stores the tens digit and the low nibble stores the units digit. For example, decimal 59 becomes 0101 1001 in packed BCD (5→0101, 9→1001). BCD ensures exact decimal digit preservation which is useful in financial computations and decimal displays where binary rounding errors are unacceptable.
Types of BCD and arithmetic
Unpacked BCD stores each decimal digit in a full byte (useful for easy digit manipulation), while packed BCD stores two digits per byte for compactness. BCD addition is performed per digit: add corresponding nibbles, if a nibble sum exceeds 9 or a digit carry occurs, add 6 (0110) to correct the binary sum back into valid BCD; propagate carry to higher digit. Many processors historically included decimal adjust instructions to support BCD arithmetic efficiently.
Other binary codes
Gray code is a binary representation where consecutive values differ by only one bit. It is used in mechanical and optical encoders to reduce transitional errors when reading positions. ASCII and Unicode are character encoding schemes mapping characters to numeric values so text can be stored and transmitted; ASCII uses 7 or 8 bits, Unicode uses code points and encodings like UTF-8, UTF-16, UTF-32 to handle world scripts.
Advantages and disadvantages
BCD's main advantage is exact decimal digit representation and easy decimal digit extraction, making it suitable for financial applications and decimal displays. Its disadvantages are storage inefficiency and more complex arithmetic compared with binary. Gray code's advantage is single-bit transitions, reducing transient error in noisy transitions; it is not suited for arithmetic operations because numerical ordering is not straightforward.
Applications in computing
BCD is used in calculators, some financial systems, and older hardware where decimal I/O was central. Gray code is used in position encoders and some error-resistant counting mechanisms. Character codes like ASCII and Unicode are essential for text processing and internationalisation.
- Decimal 27 in packed BCD → 0010 0111.
- Gray code sequence for 3 bits: 000, 001, 011, 010, 110, 111, 101, 100.
- Gray code from binary: G = B XOR (B >> 1)
- BCD correction: if a 4-bit sum > 9 or if carry occurred, add 6 (0110) to that nibble
Character Codes: ASCII and Unicode
Purpose of character codes
Character codes assign numeric values to letters, digits, punctuation and control characters so text can be stored, processed and transmitted by digital systems. Without a standard mapping there would be no consistent meaning for byte values representing characters across different programs and machines.
ASCII details
ASCII (American Standard Code for Information Interchange) originally used 7 bits to represent 128 characters: control codes (like newline), digits, uppercase and lowercase letters, and common punctuation. ASCII values 0–127 are stable across many systems. Extended ASCII uses 8 bits (0–255) with various vendor-specific extensions for accented characters and symbols. ASCII is compact and efficient for English-centric text.
Unicode and UTF encodings
Unicode is a universal character set designed to represent scripts and symbols from most of the world's writing systems. Unicode assigns each character a code point (written as U+XXXX). To store code points in bytes, encodings such as UTF-8, UTF-16 and UTF-32 are used. UTF-8 is variable-length (1–4 bytes) and is backward-compatible with ASCII: ASCII characters encode as single bytes identical to ASCII. UTF-16 uses 16-bit units and sometimes surrogate pairs for characters beyond the Basic Multilingual Plane; UTF-32 uses fixed 32-bit units for each code point.
Binary representation and implications
When text is stored in memory it becomes a sequence of bytes. The meaning of those bytes depends on the chosen encoding. For example, the ASCII character 'A' is 65 decimal = 01000001 binary; in UTF-8 it is also encoded as single byte 0x41. Multibyte encodings mean that string length in characters can differ from byte length, which affects indexing, slicing and memory allocation in programs.
Practical consideration
Use the appropriate encoding for the application and be careful when reading or writing files between systems. Mismatched encodings cause mojibake (garbled text). Many programming languages and libraries default to UTF-8 because it supports international text and is backward-compatible with ASCII.
- ASCII: 'a' = 97 decimal = 01100001 binary.
- UTF-8 encoding of U+20AC (Euro sign) = bytes 0xE2 0x82 0xAC.
Gray Code and its Uses
Definition and property
Gray code is a binary code in which successive integers differ in only one bit position. The canonical form called binary-reflected Gray code is constructed so that adjacent values have Hamming distance 1. This property is useful in systems where multiple bit lines change simultaneously; reducing the number of changing bits reduces transient errors and the chance of misread intermediate states.
Construction methods
Gray code can be generated from binary using the formula G = B XOR (B >> 1). For n bits, the sequence can be built recursively by taking the (n−1)-bit Gray sequence, prefixing each code with 0, then taking the reversed (n−1)-bit Gray sequence and prefixing each code with 1. This produces an ordered list where only one bit changes between successive entries.
Conversion back to binary
To convert a Gray code back to binary, use an iterative XOR method: the most significant binary bit equals the most significant Gray bit; then each subsequent binary bit is the XOR of the previous binary bit and the current Gray bit. This recovers the original binary value reliably and with simple bitwise operations.
Applications
Gray code is commonly used in position encoders (rotary or linear) where mechanical movement may cause multiple contact transitions; single-bit changes lower the chance of reading intermediate incorrect values. It is used in analog-to-digital interfaces, Karnaugh maps for minimising Boolean functions, and certain combinatorial and optimisation algorithms where small Hamming distance transitions are desired.
Limitations and considerations
Gray code sacrifices straightforward numeric ordering for reduced transition errors; arithmetic on Gray-coded values is not simple. Therefore Gray code is used for encoding states or positions rather than for arithmetic computations. When arithmetic is required, convert to binary, perform the arithmetic, then convert back to Gray if needed.
Summary
Understand how to generate Gray code, how to convert between Gray and binary and where its single-bit-change property is beneficial in hardware and encoding tasks.
- 3-bit Gray sequence: 000, 001, 011, 010, 110, 111, 101, 100.
- Compute Gray of binary 1011: B>>1 = 0101 → XOR → 1011 XOR 0101 = 1110.
- Gray from binary: G = B XOR (B >> 1)
- \[Binary from Gray: B_0 = G_0\]\[B_i = B_{i−1} XOR G_i for i>0\]
Representing Boolean and Bitwise Operations
Bits and logical operations
Bitwise operations act directly on binary representations at the level of individual bits. The primary boolean operations are AND, OR, XOR and NOT. AND produces 1 only when both input bits are 1; OR produces 1 if at least one input is 1; XOR produces 1 when inputs differ; NOT inverts each bit. These operations are the building blocks of digital logic and of many algorithms that manipulate data at the bit level.
Bitwise operators in practice
In programming and hardware, bitwise operators are applied to whole words (8, 16, 32, 64 bits) and operate independently on corresponding bit positions. Masks are bit patterns used with these operators to select or modify particular bits. For example, x & 0x0F extracts the lower 4 bits of x; x | 0x10 sets bit 4; x &= ~0x10 clears bit 4; x ^= 0x01 toggles the least significant bit. These idioms are used frequently in device drivers, embedded systems and performance-critical code because they are fast and predictable.
Shifts and their meanings
Shift operations move bits left or right. A left shift by k positions (x << k) moves all bits toward more significant positions and inserts zeros into the low-order bits—this is equivalent to multiplication by 2^k for unsigned integers if no overflow occurs. Right shifts come in two kinds: logical right shift inserts zeros at the high-order positions and is suitable for unsigned values; arithmetic right shift preserves the sign bit for signed values (replicating the MSB) and is used to implement signed division by powers of two with rounding toward negative infinity in two's complement representation. Understanding the distinction avoids subtle bugs when shifting signed integers.
Bit-fields, packing and unpacking
Bits can store multiple small values compactly using bit-fields: reserve specific bit ranges for different fields within a word. For example, a 32-bit control word may use bits 0–3 for a mode value, bits 4–7 for flags, and so on. Packing reduces memory but requires masking and shifting to read/write fields: to read a field use (word >> shift) & mask; to write clear the field with word &= ~(mask << shift) then set word |= (value & mask) << shift. Proper masking prevents accidental overwriting of nearby fields.
Logical vs arithmetic operations and precedence
Bitwise operations differ from logical boolean operators used in high-level languages (like && and ||) which operate on boolean truth values and may short-circuit. Bitwise operators operate on integers and produce integer results. Operator precedence matters in expressions; parentheses clarify intent and prevent errors when combining shifts, masks and arithmetic.
Hardware and truth tables
At circuit level, boolean operations correspond to logic gates: AND, OR, XOR and NOT implemented with transistors. Truth tables summarize the output for all input combinations and are a useful tool to design and reason about circuits and conditional logic. More complex boolean functions are built by combining gates; Karnaugh maps and boolean algebra help simplify such expressions.
Common patterns and applications
Common bitwise patterns include bit tests, setting/clearing/toggling bits, constructing masks, computing parity with XOR, and packing small fields. Applications include compression, cryptography, hashing, network protocol header parsing, graphics pixel manipulation, and device register control. Because bitwise operations are fast and map closely to hardware, they are preferred in low-level code where performance and size matter.
Summary
Master bitwise operators, masking idioms, shifting semantics and bit-field access patterns. Know when to use logical operators vs bitwise operators and be careful with signed shifts. These skills are essential for systems programming, embedded development and understanding how data is represented and manipulated at the binary level.
- Mask lower nibble: 0xAB & 0x0F = 0x0B.
- Set bit 3 in 00101000: OR with 00001000 → 00101000 | 00001000 = 00101000 (already set) or toggling example: 00101000 XOR 00001000 = 00100000.
- Set bit i: x = x OR (1 << i)
- Clear bit i: x = x AND NOT(1 << i)
- Toggle bit i: x = x XOR (1 << i)
Normalization and Rounding in Floating Point
Normalization
Normalization keeps the significand (mantissa) of a floating-point number in a canonical range so the representation uses available bits efficiently. In binary floating-point, normalized numbers typically have an implicit leading 1 before the fractional bits, so the stored fraction represents the bits after that leading 1. For example, 6.5 in binary is 1.101 × 2^2; the stored mantissa holds 101... and the exponent field stores the value 2 plus the bias.
Why normalize?
Normalization maximises precision by ensuring the leading digit is non-zero, thereby preventing leading zero bits from wasting storage space. It also makes comparison and ordering simpler because normalized values have consistent formats.
Rounding modes
When a floating-point operation yields more precision than can be stored, the result must be rounded. IEEE 754 specifies rounding modes: round-to-nearest-even (the default), round toward zero, round toward +∞ and round toward −∞. Round-to-nearest-even reduces bias over many operations by rounding ties to the nearest even significand.
Guard, round and sticky bits
During arithmetic, extra bits beyond the target mantissa are kept to decide rounding. The guard bit is the first extra bit, the round bit is the next, and the sticky bit is the logical OR of all remaining lower bits. These bits inform the rounding decision: if guard is 1 and (round or sticky is 1 or least significant stored bit is 1 under tie rules), then round up; otherwise round down.
Errors and numerical stability
Rounding introduces small errors which can accumulate. Catastrophic cancellation happens when subtracting nearly equal numbers: significant digits cancel leaving a result with few accurate bits and amplifying relative error. To reduce such problems, rewrite formulas to avoid subtracting close quantities, use higher precision or compensated algorithms, and be conscious of operation order in sums to minimise rounding impact.
Summary
Know how normalization preserves precision, how guard/round/sticky bits guide rounding, and why rounding mode selection and algorithm structure matter for numerical accuracy and stability in floating-point computations.
- In single precision, machine epsilon ≈ 2^{−23} ≈ 1.19×10^{−7}, the spacing of representable numbers around 1.0.
- If extra bits produce 1.000000119..., rounding to nearest-even yields 1.00000012 approximately in single precision.
- \[Machine epsilon for binary floating-point ≈ 2^{−p} where p is number of mantissa bits (implicit leading 1 excluded)\]
- Rounded value determined by guard, round, sticky bits and chosen rounding mode
Practical Examples: Memory Representation and Endianness
How multi-byte numbers are stored
Multi-byte numbers occupy several consecutive memory addresses. Endianness defines the order of bytes in memory: in big-endian systems the most significant byte is stored at the lowest memory address; in little-endian systems the least significant byte is stored first. The interpretation of a byte sequence as a number differs with endianness. This matters for file formats, network protocols and interoperability between systems.
Examples and conversion
Consider the 32-bit value 0x12345678. In big-endian memory it appears as bytes 12 34 56 78 in increasing addresses. In little-endian it appears as 78 56 34 12. Reading these bytes with the wrong endianness yields incorrect numeric values. Standard network byte order is big-endian; functions like htons/ntohl (host-to-network short/long) convert between host and network order in network programming.
Alignment and padding
Many architectures prefer data aligned at natural boundaries (e.g., 4-byte alignment for 32-bit words) for efficient memory access. Compilers may insert padding between structure members to respect alignment, increasing size. Packed structures remove padding but may cause slower accesses or require special instructions. Knowing these layout rules is important when interfacing with hardware or binary file formats.
Interpreting memory dumps
Debugging low-level programs often involves inspecting memory bytes. Understanding endianness allows correct reconstruction of integers, floats and strings from raw byte sequences. Also be mindful of signed vs unsigned interpretations and of floating-point layout (IEEE 754) when interpreting bytes as real values.
Cross-platform data exchange
File formats and network protocols must specify byte order. When exchanging data between different-endian systems, perform explicit byte swapping. Structured binary formats often define fields with fixed sizes and endianness to ensure portability. Text formats avoid these issues at the cost of increased size and parsing cost.
Summary
Understand how endianness affects byte order and numeric interpretation, the role of alignment and padding, and how to convert and interpret multi-byte values correctly across systems. These skills help avoid subtle bugs when moving binary data between machines and when debugging low-level code.
- 32-bit int 305419896 (0x12345678) stored little-endian: memory bytes 78 56 34 12.
- Interpreting bytes 0xFF 0x00 as unsigned 16-bit big-endian → 65280; little-endian → 255.
Key Concepts
- Base (Radix)
- The number of unique digits including zero used in a positional numeral system.
- Positional Notation
- A representation where the value of a digit depends on its position and the base's powers.
- Binary
- Base-2 numeral system using digits 0 and 1; fundamental to digital electronics.
- Octal
- Base-8 numeral system with digits 0–7, useful as a compact binary grouping of 3 bits.
- Hexadecimal
- Base-16 system with digits 0–9 and A–F, commonly used to represent bytes as two hex digits.
- Two's Complement
- A method to represent signed integers by inverting bits and adding one to obtain negatives.
- One's Complement
- A signed representation formed by inverting all bits of the positive number.
- Sign-Magnitude
- A signed representation where one bit indicates sign and remaining bits represent magnitude.
- IEEE 754
- A standard specifying floating-point representation, arithmetic, and special values.
- Mantissa (Significand)
- The fraction part of a floating-point number representing significant digits of the value.
- Exponent Bias
- A fixed number added to the actual exponent to store non-negative exponent fields in floating-point.
- BCD
- Binary-Coded Decimal where each decimal digit is encoded separately in binary, typically using 4 bits.
- Gray Code
- A binary code sequence where successive numbers differ by only one bit.
- Endianness
- The byte ordering convention (big-endian or little-endian) used to store multi-byte values in memory.
- Overflow
- When a computed result lies outside the representable range and cannot be stored in the given number of bits.
- Underflow
- When a value is too close to zero to be represented in normalized floating-point form and may become subnormal or zero.
- Fixed-Point
- A representation that stores numbers as scaled integers with a fixed number of fractional bits.
- Machine Epsilon
- The smallest positive number that, when added to 1.0, yields a representable number different from 1.0 in floating-point.
Practice Questions
-
Convert decimal 156 to binary / दशमलव 156 को द्विआधारी में बदलें
Show answer
Procedure: divide by 2 repeatedly and record remainders. 156 ÷ 2 = 78 remainder 0; 78 ÷ 2 = 39 remainder 0; 39 ÷ 2 = 19 remainder 1; 19 ÷ 2 = 9 remainder 1; 9 ÷ 2 = 4 remainder 1; 4 ÷ 2 = 2 remainder 0; 2 ÷ 2 = 1 remainder 0; 1 ÷ 2 = 0 remainder 1. Read remainders from last to first: 10011100. So 156_{10} = 10011100_2. / प्रक्रिया: 2 से बार-बार भाग देकर शेष रखें: 156÷2=78 शेष0; 78÷2=39 शेष0; 39÷2=19 शेष1; 19÷2=9 शेष1; 9÷2=4 शेष1; 4÷2=2 शेष0; 2÷2=1 शेष0; 1÷2=0 शेष1. शेष को उल्टा पढ़ें → 10011100. अतः 156_{10} = 10011100_2.
-
Represent +18 and −18 in 8-bit two's complement / 8-बिट दो के पूरक में +18 और −18 को दर्शाइए
Show answer
Positive 18 in binary (8-bit) is 00010010. To get −18 in two's complement: start with +18 = 00010010, invert bits → 11101101, add 1 → 11101110. Thus +18 = 00010010 and −18 = 11101110 in 8-bit two's complement. / +18 का 8-बिट बाइनरी 00010010 है। −18 पाने के लिए 00010010 को उलटें → 11101101, फिर 1 जोड़ें → 11101110। अतः +18 = 00010010 और −18 = 11101110।
-
Add binary numbers 01011101 and 00110111 (8-bit unsigned). State result and whether overflow occurred / द्विआधारी संख्याएँ 01011101 और 00110111 जोड़ें (8-बिट अपरसंख्य). परिणाम बताइए और क्या ओवरफ़्लो हुआ?
Show answer
Add bitwise: 01011101 (93) + 00110111 (55). 93 + 55 = 148. Binary addition gives 10010100 (which is 148). Since both are unsigned 8-bit and result 148 fits within 0–255, there is no unsigned overflow and no carry out beyond 8 bits occurred. So result = 10010100 and no overflow. / चरण: 01011101 (93) + 00110111 (55) = 148। बाइनरी परिणाम 10010100 है। 8-बिट unsigned सीमा (0–255) में है, इसलिए कोई अपरसंख्य ओवरफ़्लो नहीं हुआ।
-
Convert binary 110101111.101 to hexadecimal / द्विआधारी 110101111.101 को हेक्साडेसिमल में बदलें
Show answer
Group bits into 4s from the radix point. Integer part 110101111 → pad left to multiple of 4 bits: 0110 1011 111 → groups 0110=6, 1011=B, 0111=7 but correct grouping of integer is 110101111 → pad left to 12 bits: 0001 1010 1111 gives 1 A F, however simplest standard grouping is pad to left 0110 1011 111 → treat last group 111 as 0111. Thus integer = 6 B 7. Fraction .101 pad to 4 bits .1010 = A. So final hex = 6B7.A. Therefore 110101111.101_2 = 6B7.A_16. / पूर्णांत भाग को 4 बिट समूहों में बाँटकर और भिन्नांश को पैड कर लिखें → परिणाम = 6B7.A।
-
Explain how to get 1's complement and 2's complement of a binary number and give 1's and 2's complement of 00101100 / किसी द्विआधारी संख्या का 1 का और 2 का पूरक कैसे प्राप्त करते हैं बताइए और 00101100 का 1 का तथा 2 का पूरक दीजिए
Show answer
1's complement: invert every bit (0→1, 1→0). 2's complement: take 1's complement and add 1 to the result. For 00101100: 1's complement = 11010011. Add 1 to get 2's complement: 11010011 + 1 = 11010100. So 1's complement = 11010011 and 2's complement = 11010100. / 1 का पूरक: प्रत्येक बिट उलटें। 00101100 का 1 का पूरक → 11010011। 2 का पूरक = 1 का पूरक + 1 → 11010011 + 1 = 11010100।
-
Describe IEEE 754 single precision fields and how to compute the real number from the fields / IEEE 754 सिंगल प्रिसीजन के क्षेत्रों का वर्णन करें और फ़ील्ड से वास्तविक संख्या कैसे निकाले बताइए
Show answer
IEEE 754 single precision uses 32 bits: 1 sign bit S, 8 exponent bits E, and 23 fraction bits F. The exponent is stored with bias 127. For a normalized value (0 < E < 255): real value = (−1)^S × (1.F) × 2^{E−127}, where 1.F means 1 followed by the fractional bits as binary fraction. If E = 0 and F = 0 it represents signed zero. If E = 0 and F ≠ 0 it is a subnormal (denormal) number: value = (−1)^S × (0.F) × 2^{−126}. If E = 255 and F = 0 it is ±infinity; if E = 255 and F ≠ 0 it is NaN. To decode, read S, compute exponent e = E − 127 and then compute mantissa 1 + (sum of F_i×2^{−i}), apply sign and 2^{e}. / सिंगल प्रिसीजन: 1 साइन बिट, 8 एक्सपोनेंट बिट (bias =127), 23 फ्रैक्शन बिट। सामान्य (1≤E≤254) के लिए मान (−1)^S × (1.F) × 2^{E−127} होता है। E=0,F=0 → ±0; E=0,F≠0 → subnormal; E=255,F=0 → ±∞; E=255,F≠0 → NaN।
-
Convert hexadecimal 0x3FA to octal / हेक्साडेसिमल 0x3FA को ऑक्टल में बदलें
Show answer
Convert hex to binary, then binary to octal. 0x3FA → hex digits 3 F A → binary 0011 1111 1010. Group binary into 3-bit groups from right: 000 111 111 101 010 → groups: 000=0, 111=7, 111=7, 101=5, 010=2. So octal = 07752; leading zero for full group can be dropped, giving 7752_8. Thus 0x3FA = 7752 (octal). / 0x3FA = 0011 1111 1010_2. 3-बिट समूह बनाएं: 000 111 111 101 010 → 0 7 7 5 2 → ऑक्टल 07752 ⇒ 7752।
-
What is Gray code of binary 10100 and how to get binary back from that Gray code / बाइनरी 10100 का ग्रे कोड क्या होगा और उस ग्रे कोड से बाइनरी कैसे निकालेँगे
Show answer
Compute Gray G = B XOR (B >> 1). For B = 10100, B>>1 = 01010. XOR → 10100 XOR 01010 = 11110. So Gray code = 11110. To convert back: binary bit0 = gray0 = 1. Then binary bit1 = previous binary bit XOR gray1 = 1 XOR 1 = 0. Bit2 = 0 XOR 1 = 1. Bit3 = 1 XOR 1 = 0. Bit4 = 0 XOR 0 = 0. So binary recovered = 10100. / ग्रे = 11110। वापस परिवर्तित करने के लिए: B0=G0=1; B1=B0 XOR G1=1 XOR1=0; B2=0 XOR1=1; B3=1 XOR1=0; B4=0 XOR0=0 → 10100।
-
A 16-bit signed two's complement integer has hex value 0xFF9C. What decimal number does it represent? / 16-बिट साइन किए हुए दो के पूरक पूर्णांक का हेक्स मान 0xFF9C है। यह कौन सा दशमलव संख्या दर्शाता है?
Show answer
0xFF9C has MSB 1 so it's negative in two's complement. To find magnitude compute two's complement: invert bits → 0x0063, add 1 → 0x0064 = 100 decimal. Therefore the value is −100 decimal. So 0xFF9C represents −100. / 0xFF9C का MSB=1 है अतः नकारात्मक। बिट उलटने पर 0x0063 मिलता है, +1 → 0x0064 = 100। अतः मान = −100।
-
Why is 0.1 (decimal) not exactly representable in binary floating-point? / दशमलव 0.1 को बाइनरी फ्लोटिंग-पॉइंट में सटीक क्यों नहीं दर्शाया जा सकता?
Show answer
A decimal fraction terminates in binary only when its denominator (in lowest terms) is a power of 2. 0.1 decimal = 1/10 and 10 factors into 2×5; presence of factor 5 means the binary expansion repeats infinitely (it is a repeating fraction in base 2). Floating-point stores only a finite number of binary fraction bits, so it holds an approximation to 0.1, not the exact value. This causes small rounding errors when using 0.1 in binary floating-point arithmetic. / कारण: 0.1 = 1/10 का हराना 10=2×5 है; बाइनरी में केवल 2 के गुणनखंड पर समाप्ति संभव है, इसलिए 1/10 का बाइनरी रूप अनंत आवर्ती होगा। फ्लोटिंग-पॉइंट में सीमित बिट्स होने से केवल निकटतम प्रतिनिधि संग्रहीत किया जा सकता है, सटीक मान नहीं।
-
Perform BCD addition: add decimal 47 and 68 using packed BCD and show result / BCD में जोड़ करें: दशमलव 47 और 68 को पैक्ड BCD में जोड़ें और परिणाम दिखाइए
Show answer
Packed BCD: 47 = 0100 0111, 68 = 0110 1000. Add lower nibbles: 0111 (7) + 1000 (8) = 1111 (15). Since >9, add 0110 (6) to correct → 1111 + 0110 = 1 0101; write 0101 (5) and carry 1 to upper nibble. Now add upper nibbles: 0100 (4) + 0110 (6) + carry1 = 1011 (11). Since >9, add 0110 → 1011 + 0110 = 1 0001; write 0001 (1) and final carry 1 into new digit. So result digits are carry1, 0001 (1), 0101 (5) → decimal 115. Therefore 47 + 68 = 115 and BCD result (packed) is 0001 0001 0101 or as packed nibbles across bytes: 00010001 0101 (practically stored with leading carry as an extra nibble). / चरण: निचला निबल 7+8=15 → >9, +6 → gives 0101 with carry1. ऊपरी निबल 4+6+1=11 → >9, +6 → gives 0001 with carry1. अंतिम परिणाम = 115।
Related Laws & Principles
Explore allFoundational laws & principles connected to this chapter — tap to open in the Laws Explorer.