A computer stores a salary, a temperature of −5 degrees, the value 3.14 and the letter A in the same way: as patterns of 0s and 1s. What a pattern means depends entirely on the rule used to read it.

Bits and Bytes

A bit is a binary digit, 0 or 1. Eight bits form a byte. With n bits, 2ⁿ different patterns can be formed; 8 bits give 256 patterns. A representation is a rule that assigns a meaning to each pattern.

Representing Signed Integers

Positive integers are stored as their binary equivalents. For negative integers, three methods exist. In all three, the leftmost bit, called the sign bit, is 0 for positive numbers and 1 for negative numbers.

Signed magnitude: the sign bit gives the sign, and the remaining bits give the size of the number.

1's complement: a negative number is formed by inverting every bit of the positive number.

2's complement: a negative number is formed by inverting every bit of the positive number and then adding 1.

Definition: 2's complement. A method of representing signed integers in which the negative of a number is obtained by inverting all its bits and adding 1.

Table 2.9: The number −5 in three 8-bit representations

Method +5 −5 Range in 8 bits
Signed magnitude 00000101 10000101 −127 to +127
1's complement 00000101 11111010 −127 to +127
2's complement 00000101 11111011 −128 to +127

Signed magnitude and 1's complement each have two patterns for zero, +0 and −0. The 2's complement method has only one zero, and addition and subtraction can be done by the same circuit. For these reasons nearly all modern computers use 2's complement.

Representing Real Numbers: IEEE 754

Numbers with fractional parts are stored in floating-point form, similar to scientific notation. The IEEE 754 standard defines the layout. In single precision, 32 bits are divided into three fields: 1 sign bit, 8 exponent bits and 23 fraction bits. The exponent is stored with 127 added to it, which is called the bias. Double precision uses 64 bits: 1 sign bit, 11 exponent bits and 52 fraction bits.

Many decimal fractions, such as 0.1, cannot be represented exactly in binary. Floating-point values are therefore approximations, and two real numbers should not be compared for exact equality.

Representing Characters: ASCII and Unicode

Each character is stored as a number called its code. ASCII uses 7 bits and defines 128 characters: the English letters, the digits, punctuation marks and control characters. In ASCII, 'A' is 65, 'a' is 97 and the digit '0' is 48.

Unicode assigns a unique code to the characters of almost all the world's writing systems, including Devanagari, and its first 128 codes are identical to ASCII.

Example 2.9: Finding the 2's complement

Problem: Represent −12 in 8-bit 2's complement and verify it with a program that displays a character code.

Solution:

  1. +12 in 8-bit binary is 00001100.
  2. Inverting every bit gives 11110011.
  3. Adding 1 gives 11110100, which represents −12.
  4. Check: 11110100 has place values −128 + 64 + 32 + 16 + 4 = −12.

The program below shows that a character is stored as its code.

#include <stdio.h>

int main(void)
{
    char ch = 'A';

    printf("Character %c has code %d\n", ch, ch);
    printf("Code %d is character %c\n", ch + 1, ch + 1);
    return 0;
}

Output:

Character A has code 65
Code 66 is character B

Result: −12 is stored as 11110100. The program confirms that 'A' is stored as the number 65 and that 66 is the code of 'B'.

Common mistakes

  • Forgetting to add 1 after inverting the bits when forming a 2's complement.
  • Reading a bit pattern without knowing which representation was used.
  • Expecting a value such as 0.1 to be stored exactly in floating-point form.

Key takeaways

  • All data is stored as bit patterns; the representation gives them meaning.
  • Signed integers use signed magnitude, 1's complement or 2's complement; 2's complement is the usual choice.
  • IEEE 754 single precision has 1 sign bit, 8 exponent bits and 23 fraction bits.
  • ASCII defines 128 characters in 7 bits; Unicode covers the world's scripts.

Practice questions

  1. Write +5 and −5 in 8-bit 1's complement.
  2. How many bits are used for the exponent in IEEE 754 single precision?
  3. What is the ASCII code of the character 'a'?

Answers to the practice questions

  1. +5 is 00000101 and −5 is 11111010.
  2. 8 bits.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit II