Number Systems and Logic Gates: Conversions, 2's Complement, MCQs

Computers store everything as binary digits, so every computer syllabus includes number systems and the logic gates that process bits. Tests ask quick conversions between decimal, binary, octal and hexadecimal, the 1's or 2's complement of a binary number, and the output of a gate or small circuit. Each type has a fixed method; once you learn the grouping trick for octal and hex and the truth tables of the seven gates, these become some of the surest marks in the paper.

Asked in: PPSC, FPSC, NTS, ETEA, KPPSC, SSC, IBPS and ICS/ECAT computer

Number systems and place value

A number system is defined by its base (radix), the number of different digits it uses. Decimal has base 10 (digits 0 to 9), binary base 2 (0 and 1), octal base 8 (0 to 7) and hexadecimal base 16 (0 to 9 followed by A = 10, B = 11, C = 12, D = 13, E = 14, F = 15).

In every system the value of a digit depends on its position. Moving one place to the left multiplies the place value by the base. In binary the place values from the right are 1, 2, 4, 8, 16, 32, 64, 128 and so on.

The base is shown as a subscript: (1011)β‚‚, (17)β‚ˆ, (2F)₁₆. With n bits, 2ⁿ different values can be represented, from 0 up to 2ⁿ βˆ’ 1. Four bits make a nibble and match one hex digit; three bits match one octal digit.

Decimal to binary, octal and hex

To convert a decimal whole number to another base, divide it repeatedly by the base and write down the remainders. Stop when the quotient becomes 0, then read the remainders from the last to the first (bottom to top).

For example, 45 Γ· 2 gives remainders 1, 0, 1, 1, 0, 1 in order; read upwards they give (101101)β‚‚. A faster way for small numbers is to subtract place values: 45 = 32 + 8 + 4 + 1, so put 1 under 32, 8, 4 and 1 and 0 elsewhere.

For octal divide by 8 and for hex divide by 16, writing remainders 10 to 15 as A to F. For example, 255 Γ· 16 = 15 remainder 15, and 15 Γ· 16 = 0 remainder 15, so 255 = (FF)₁₆.

Any base to decimal

Multiply each digit by its place value and add. For binary, simply add the place values where a 1 appears: (110101)β‚‚ = 32 + 16 + 4 + 1 = 53.

For octal use powers of 8: (157)β‚ˆ = 1 Γ— 64 + 5 Γ— 8 + 7 Γ— 1 = 111. For hex use powers of 16: (2F)₁₆ = 2 Γ— 16 + 15 = 47.

Check the digits before converting: a digit 8 or 9 cannot appear in an octal number, and only 0 and 1 are allowed in binary.

Binary ↔ octal ↔ hex by grouping

Because 8 = 2³ and 16 = 2⁴, conversion between binary and octal or hex needs no division. For octal, group binary digits in threes starting from the right and replace each group with its value. For hex, group in fours from the right.

Example: (10110111)β‚‚ β†’ octal groups 10 | 110 | 111 = 2 6 7, so (267)β‚ˆ; hex groups 1011 | 0111 = B 7, so (B7)₁₆. Add leading zeros to complete the leftmost group.

To go the other way, write each octal digit as 3 bits or each hex digit as 4 bits: (3C)₁₆ = 0011 1100, and (75)β‚ˆ = 111 101. To convert octal to hex, pass through binary.

Binary addition and 1's and 2's complement

Binary addition rules: 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 10 (write 0, carry 1), and 1 + 1 + 1 = 11 (write 1, carry 1). For example, 1011 + 0110 = 10001 (11 + 6 = 17).

The 1's complement of a binary number is found by inverting every bit (0 becomes 1 and 1 becomes 0). The 2's complement is the 1's complement plus 1. A quick shortcut: copy bits from the right up to and including the first 1, then invert all the remaining bits.

Computers store negative integers in 2's complement form, which lets subtraction be done by addition. In 8 bits the leftmost bit is the sign bit (1 means negative), and the range is βˆ’128 to +127. For example, +44 = 00101100, so βˆ’44 = 11010100.

Logic gates and truth tables

A logic gate is an electronic circuit that produces one binary output from one or more binary inputs. The three basic gates are AND (output 1 only when all inputs are 1), OR (output 1 when at least one input is 1) and NOT (inverter: output is the opposite of the single input).

NAND is AND followed by NOT: its output is 0 only when all inputs are 1. NOR is OR followed by NOT: its output is 1 only when all inputs are 0. NAND and NOR are called universal gates because any other gate, and so any digital circuit, can be built using only NAND gates or only NOR gates.

XOR (exclusive OR) gives 1 when the two inputs are different; XNOR gives 1 when they are the same. In Boolean algebra AND is written AΒ·B, OR as A + B and NOT A as Aβ€². De Morgan's laws state (AΒ·B)β€² = Aβ€² + Bβ€² and (A + B)β€² = Aβ€²Β·Bβ€², which explains why NAND equals OR of the inverted inputs.

Solved examples

  1. Convert (45)₁₀ to binary.
    1. Divide by 2: 45 β†’ 22 r 1, 22 β†’ 11 r 0, 11 β†’ 5 r 1, 5 β†’ 2 r 1, 2 β†’ 1 r 0, 1 β†’ 0 r 1.
    2. Read the remainders from bottom to top: 101101.
    3. Check: 32 + 8 + 4 + 1 = 45.
    Answer: (101101)β‚‚
  2. Convert (110101)β‚‚ to decimal.
    1. Place values from the right: 1, 2, 4, 8, 16, 32.
    2. The 1s are under 32, 16, 4 and 1.
    3. 32 + 16 + 4 + 1 = 53.
    Answer: 53
  3. Convert (10110111)β‚‚ to octal and to hexadecimal.
    1. Octal: group in threes from the right: 10 | 110 | 111 β†’ 2, 6, 7, giving (267)β‚ˆ.
    2. Hex: group in fours from the right: 1011 | 0111 β†’ 11 = B and 7, giving (B7)₁₆.
    3. Check through decimal: the number is 183; 2 Γ— 64 + 6 Γ— 8 + 7 = 183 and 11 Γ— 16 + 7 = 183.
    Answer: (267)β‚ˆ and (B7)₁₆
  4. Find the decimal values of (2F)₁₆ and (157)β‚ˆ.
    1. (2F)₁₆ = 2 Γ— 16 + F = 32 + 15 = 47.
    2. (157)β‚ˆ = 1 Γ— 8Β² + 5 Γ— 8 + 7 = 64 + 40 + 7 = 111.
    Answer: 47 and 111
  5. Find the 1's and 2's complement of the 8-bit number 00101100 and state what the 2's complement represents.
    1. 1's complement: invert every bit, 00101100 β†’ 11010011.
    2. 2's complement: add 1, 11010011 + 1 = 11010100. (Shortcut: keep the rightmost 100, invert the rest.)
    3. 00101100 is +44, so its 2's complement 11010100 is how βˆ’44 is stored in 8 bits.
    Answer: 1's complement 11010011; 2's complement 11010100 (= βˆ’44)
  6. For the circuit Y = (A AND B) OR (NOT C), find Y when A = 1, B = 0 and C = 0.
    1. A AND B = 1 AND 0 = 0.
    2. NOT C = NOT 0 = 1.
    3. Y = 0 OR 1 = 1.
    Answer: Y = 1

Exam shortcuts

Common mistakes

Practice MCQs

  1. What is the base of the hexadecimal number system?
    A) 8
    B) 16
    C) 2
    D) 10
    Answer: B) 16 β€” Hexadecimal uses sixteen digits, 0 to 9 and A to F, so its base is 16. Base 8 is octal, base 2 is binary and base 10 is decimal.
  2. What is the binary equivalent of decimal 13?
    A) 1011
    B) 1110
    C) 1001
    D) 1101
    Answer: D) 1101 β€” 13 = 8 + 4 + 1, so the bits under 8, 4 and 1 are 1: 1101. 1011 is 11, 1110 is 14 and 1001 is 9, so none of them equals 13.
  3. What is the decimal value of (101010)β‚‚?
    A) 21
    B) 84
    C) 40
    D) 42
    Answer: D) 42 β€” The 1s are under 32, 8 and 2, and 32 + 8 + 2 = 42. 21 is the value of 10101, which drops the last 0; 84 is double the value, as if an extra 0 were added; 40 leaves out the 2s place.
  4. Which logic gate gives an output of 1 only when all of its inputs are 1?
    A) OR
    B) NOR
    C) AND
    D) XOR
    Answer: C) AND β€” An AND gate outputs 1 only when every input is 1. OR outputs 1 when any input is 1, NOR outputs 1 only when all inputs are 0, and XOR outputs 1 when the two inputs differ, which gives 0 when both are 1.
  5. What is the decimal value of (1A)₁₆?
    A) 17
    B) 26
    C) 110
    D) 160
    Answer: B) 26 β€” (1A)₁₆ = 1 Γ— 16 + 10 = 26. 17 treats A as 1; 110 writes 1 and A = 10 side by side as if they were decimal digits; 160 multiplies 16 by 10 instead of adding.
  6. What is the decimal value of (75)β‚ˆ?
    A) 61
    B) 75
    C) 57
    D) 35
    Answer: A) 61 β€” (75)β‚ˆ = 7 Γ— 8 + 5 = 61. 75 ignores the base and reads the digits as decimal, 57 reverses the digits (and (57)β‚ˆ is in fact 47), and 35 multiplies the two digits instead of using place values.
  7. Which pair of gates are called universal gates?
    A) AND and OR
    B) NAND and NOR
    C) XOR and XNOR
    D) NOT and AND
    Answer: B) NAND and NOR β€” Any digital circuit can be built using only NAND gates or only NOR gates, so these are the universal gates. AND and OR cannot make NOT on their own, XOR and XNOR cannot build every function by themselves, and NOT with AND is a combination of two gate types rather than a single universal gate.
  8. Convert (11101)β‚‚ to octal.
    A) 35
    B) 72
    C) 29
    D) 53
    Answer: A) 35 β€” Group in threes from the right: 11 | 101 β†’ 3 and 5, so (35)β‚ˆ. 72 comes from grouping from the left (111 | 01), 29 is the decimal value rather than octal, and 53 reverses the octal digits.
  9. What is the 1's complement of 1011001?
    A) 0100111
    B) 1011010
    C) 0100110
    D) 1101001
    Answer: C) 0100110 β€” The 1's complement inverts every bit: 1011001 β†’ 0100110. 0100111 is the 2's complement (1 has been added), 1011010 adds 1 to the original without inverting, and 1101001 only rearranges the original bits.
  10. What is the output of an XOR gate when both inputs are 1?
    A) 0
    B) 1
    C) 10
    D) 11
    Answer: A) 0 β€” XOR gives 1 only when the inputs are different; with both inputs 1 they are the same, so the output is 0. An output of 1 would be the OR result. A gate has a single output bit, so 10 (the binary sum 1 + 1) and 11 (the two inputs written together) are not possible outputs.
  11. What is the hexadecimal equivalent of decimal 255?
    A) FF
    B) EF
    C) F0
    D) 1FF
    Answer: A) FF β€” 255 = 15 Γ— 16 + 15, and 15 is F, so 255 = (FF)₁₆. EF is 239, F0 is 240 and 1FF is 511, so none of these equals 255. Since F is the largest hex digit, FF is the largest two-digit hex number.
  12. Add the binary numbers 1011 and 0110.
    A) 1101
    B) 11001
    C) 10001
    D) 10011
    Answer: C) 10001 β€” 1011 is 11 and 0110 is 6; 11 + 6 = 17 = 10001. 1101 is 13, which results from dropping the carries; 11001 is 25 and 10011 is 19, both caused by placing a carry in the wrong column.
  13. What is the 2's complement of the 8-bit number 01100100?
    A) 10011011
    B) 10011100
    C) 01100101
    D) 10011101
    Answer: B) 10011100 β€” Invert all bits to get 10011011, then add 1 to get 10011100. 10011011 is only the 1's complement, 01100101 adds 1 without inverting, and 10011101 adds 1 to the 1's complement twice.
  14. What range of integers can be stored in 8 bits using 2's complement?
    A) βˆ’127 to +127
    B) 0 to 255
    C) βˆ’128 to +127
    D) βˆ’256 to +255
    Answer: C) βˆ’128 to +127 β€” With n = 8 bits the 2's complement range is βˆ’2⁷ to 2⁷ βˆ’ 1, which is βˆ’128 to +127. βˆ’127 to +127 is the range of 1's complement or sign-magnitude form, which have two zeros; 0 to 255 is the range of unsigned 8-bit numbers; and βˆ’256 to +255 would need 9 bits.
  15. The circuit Y = (A NAND B) AND (A OR B) behaves exactly like which single gate?
    A) XNOR
    B) AND
    C) NOR
    D) XOR
    Answer: D) XOR β€” Check all four inputs: for 00 the OR part is 0, so Y = 0; for 01 and 10 both parts are 1, so Y = 1; for 11 the NAND part is 0, so Y = 0. Output 1 only when inputs differ is XOR. XNOR would give 1 for 00 and 11, AND would give 1 only for 11, and NOR would give 1 only for 00.

Questions

Why do computers use the binary system?

Electronic circuits are most reliable with two states, such as on and off or high and low voltage. These map directly to 1 and 0, so all data and instructions are stored in binary.

Why are octal and hexadecimal used?

They are short ways to write long binary numbers. One octal digit stands for 3 bits and one hex digit for 4 bits, so hex is widely used for memory addresses and colour codes.

What is the difference between 1's and 2's complement?

The 1's complement inverts every bit. The 2's complement adds 1 to that result and is the form computers use for negative integers, because it has only one zero and makes subtraction easy.

How are number system questions asked in job tests?

Usually as one-step conversions, the base of a system, the complement of a short binary number, or the output of a gate for given inputs. They can be solved in under a minute with the methods above.