ExamSaathi learning library · Government exams · SSC CGL computer knowledge; published syllabus labelled 2024 · Computer knowledge

Number systems for computers: binary, octal, hexadecimal and memory units

Learn how a computer writes numbers: convert between decimal, binary, octal and hexadecimal, add binary numbers with carries, and work with bits, bytes, KB to TB and ASCII codes.

Status: original pilot lesson. Teacher review is pending. It is not a complete course or official exam material.

You will learn to

Before you start

A computer's circuits work with just two states, such as a high or a low voltage, so everything it stores is a string of 0s and 1s. Each such binary digit is a bit. Everyday numbers are decimal, or base 10: each place is worth 10 times the place on its right, so 4,072 = 4 x 1000 + 0 x 100 + 7 x 10 + 2 x 1. Any base b works the same way, with the digits 0 to b - 1 and the place values 1, b, b^2, b^3 and so on from the right. Bases are named in words, as in binary 1011, and x means multiply.

Place values in base 2, 8 and 16

Binary (base 2) uses only 0 and 1, and its place values from the right are 1, 2, 4, 8, 16, 32, 64, 128 and so on. Octal (base 8) uses the digits 0 to 7, with place values 1, 8, 64 and 512. Hexadecimal, or hex (base 16), needs sixteen digits: 0 to 9, then A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15, with place values 1, 16, 256 and 4096. To turn any of them into decimal, multiply each digit by its place value and add. In binary only the places holding a 1 count, so binary 100111 = 32 + 4 + 2 + 1 = 39. Octal 157 = 1 x 64 + 5 x 8 + 7 = 111, and hex 2F = 2 x 16 + 15 = 47. With n bits there are 2^n different patterns, so an 8-bit byte can hold 256 values, from 0 to 255.

Decimal to binary: divide by 2 and read upwards

To write a decimal number n in binary, divide n by 2 and note the remainder, which is always 0 or 1. Divide the quotient by 2 again, and repeat until the quotient becomes 0. The first remainder is the rightmost bit, so read the remainders from the bottom up. For 37 the remainders, top to bottom, are 1, 0, 1, 0, 0, 1, so read upwards it is 100101; check: 32 + 4 + 1 = 37. Dividing by 8 or by 16 in the same way gives octal or hex digits directly; a hex remainder from 10 to 15 is written as a letter. Or subtract the largest power of 2 that fits, again and again, writing 1 in each place used and 0 elsewhere.

Octal and hexadecimal from groups of bits

Because 8 = 2^3, each octal digit stands for exactly three bits, and because 16 = 2^4, each hex digit stands for exactly four bits. For binary to octal, split the bits into groups of three starting from the right, fill the leftmost group with zeros if it is short, and write the value of each group, 0 to 7. For hex, use groups of four and write the values 10 to 15 as A to F. Binary 1011101 splits as 1 011 101, that is 001 011 101, which is octal 135. In fours it splits as 101 1101, that is 0101 1101, which is hex 5D. Both give 93 in decimal. To go back, write every octal digit as three bits and every hex digit as four bits, keeping the zeros: octal 70 is 111 000, and hex 3A is 0011 1010.

Binary addition, memory units and character codes

Add binary numbers column by column from the right, using four facts: 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 10 (write 0, carry 1) and 1 + 1 + 1 = 11 (write 1, carry 1). Memory is counted in bits and bytes: 4 bits make a nibble and 8 bits make a byte. Many exam questions and Windows take 1 KB = 1024 bytes, 1 MB = 1024 KB, 1 GB = 1024 MB and 1 TB = 1024 GB, because 1024 = 2^10; the unambiguous names for these binary units are KiB, MiB, GiB and TiB. In strict SI units 1 kB = 1000 bytes, and disk and pen-drive makers count in 1000s, so a new drive shows a little less space in Windows. ASCII stores text as 7-bit codes, 0 to 127: capital A is 65, B is 66 and so on up to Z at 90, while small a is 97, so each small letter's code is its capital's code + 32. These 128 codes hold only English letters, digits, symbols and control codes; Unicode covers nearly all the world's scripts, including Devanagari, Bengali, Tamil and Telugu.

Common mistakes

Reading the remainders from the top down, which writes the binary number backwards. Counting place values from the left end. Ignoring zeros in the middle: binary 1001 is 9, not 3. Grouping bits from the left when changing to octal or hex, which goes wrong whenever the number of bits is not a multiple of 3 or 4. Dropping zeros inside a group when changing back, so that octal 70 becomes 111 instead of 111000. Losing the final carry in binary addition. Using 1000 when a question fixes 1 KB = 1024 bytes, or mixing up bits and bytes.

A routine for number-system questions

1. Note the base given and the base needed. 2. To reach decimal, multiply each digit by its place value and add. 3. To leave decimal, divide again and again by 2, 8 or 16 and read the remainders upwards. 4. Between binary and octal or hex, group bits in threes or fours from the right. 5. For memory sizes, use the factor the question gives, usually 1024 for each step down from TB to GB, MB, KB and bytes, and 8 for bits. 6. Check each answer by converting back to decimal.

Worked examples

  1. Convert decimal 77 to binary and check the answer.

    1. Divide by 2 again and again: 77 = 2 x 38 + 1, 38 = 2 x 19 + 0, 19 = 2 x 9 + 1 and 9 = 2 x 4 + 1.
    2. Carry on: 4 = 2 x 2 + 0, 2 = 2 x 1 + 0 and 1 = 2 x 0 + 1. The quotient is now 0, so stop.
    3. Read the remainders from the bottom up: 1, 0, 0, 1, 1, 0, 1, that is 1001101, with its 1s in the places worth 64, 8, 4 and 1.

    Answer: Decimal 77 is binary 1001101, since 64 + 8 + 4 + 1 = 77.

  2. Write binary 11010110 in octal and in hexadecimal, and find its decimal value.

    1. Decimal: add the places that hold a 1: 128 + 64 + 16 + 4 + 2 = 214.
    2. Octal: in threes from the right, 11 010 110 becomes 011 010 110, giving 3, 2 and 6. Check: 3 x 64 + 2 x 8 + 6 = 214.
    3. Hex: in fours, 1101 0110 gives 13 and 6, that is D and 6. Check: 13 x 16 + 6 = 214.

    Answer: Binary 11010110 is octal 326 and hexadecimal D6, and each equals 214 in decimal.

  3. Add binary 101101 and 11011, and check the sum in decimal.

    1. Line the numbers up on the right (101101 over 011011) and add from the rightmost column.
    2. Columns 1 to 3: 1 + 1 = 10, 0 + 1 + 1 = 10 and 1 + 0 + 1 = 10, so write 0 and carry 1 each time.
    3. Columns 4 to 6: 1 + 1 + 1 = 11 (write 1, carry 1), then 0 + 1 + 1 = 10 and 1 + 0 + 1 = 10 (write 0, carry 1). The last carry becomes a new leftmost 1.
    4. The sum is 1001000 = 64 + 8 = 72. Check: 101101 is 45, 11011 is 27, and 45 + 27 = 72.

    Answer: The sum is binary 1001000, which is 72 in decimal.

Practice checks

  1. What is binary 1101101 in decimal?
  2. What is hexadecimal 1F4 in decimal?
  3. Add binary 10111 and 1011. Which option gives the sum in binary and in decimal?
  4. A text file takes 2 KB of memory. Taking 1 KB = 1024 bytes, how many bits is that?
  5. In ASCII, capital A is 65 and small a is 97. What is the ASCII code of small h?

Sources checked