Representation

About

1 byte comprises of 8 bits and any integer or character can be represented using bits in computers, which we call its binary form(contains only 1 or 0) or in its base 2 form.

Example:

1) 14 (Decimal form) = {1110} (Binary form)
= 1 * 2^3 + 1 * 2^2 + 1 * 2^1 + 0 * 2^0
= 14.

2) 20 (Decimal form) = {10100} (Binary form)
= 1 * 2^4 + 0 * 2^3 + 1 * 2^2 + 0 * 2^1 + 0 * 2^0
= 20.

Integers: Integers can be represented using binary numbers, where each bit represents a power of 2. For example, the decimal number 5 can be represented as 0000 0101 in binary (8 bits).

Characters: Characters can be represented using character encodings such as ASCII (American Standard Code for Information Interchange) or Unicode. In ASCII, each character is represented by a unique 7-bit binary number, allowing for 128 different characters to be represented. Unicode extends this to accommodate a much larger set of characters using variable-length encoding schemes, typically using 8, 16, or 32 bits per character.

In general, the binary number can be represented in two ways.

  1. Unsigned Binary Numbers

  2. Signed Binary Numbers

Unsigned Binary Numbers

Using unsigned binary number representation, only positive binary numbers can be represented. For n-bit unsigned binary numbers, all n-bits are used to represent the magnitude of the number.

For example, if we represent decimal 12 in 5- bit unsigned number form then (12)10 = (01100)2. Here all 5 bit are used to represent the magnitude of the number

Signed Binary Numbers

Using signed binary number representation both positive and negative numbers can be represented.

In signed binary number representation the most significant bit (MSB) of the number is a sign bit. For positive numbers, the sign bit is 0 and for negative number, the sign bit is 1.

There are three different ways the signed binary numbers can be represented.

  1. Signed Magnitude Form

  2. 1’s Complement Form

  3. 2’s Complement Form

Sign Magnitude Representation

In sign-magnitude representation, the Most Significant bit of the number is a sign bit and the remaining bit represents the magnitude of the number in a true binary form. For example, if some signed number is represented in the 8-bit sign-magnitude form then MSB is a sign bit and the remaining 7 bits represent the magnitude of the number in a true binary form.

Here is the representation of + 34 and -34 in a 8-bit sign-magnitude form.

Since the magnitude of both numbers is the same, the first 7 bits in the representation are the same for both numbers. For +34, the MSB is 0, and for -34, the MSB or sign bit is 1.

Using n-bits, the range of numbers that can be represented in Sign Magnitude Representation is from – (2^(n-1) – 1) to (2^(n -1) – 1).

  • Positive range: 0 to (2^(n-1) - 1)

  • Negative range: -1 to -(2^(n-1) - 1)

1’s Complement Representation

In 1’s complement representation, the representation of the positive number is same as the negative number. But the representation of the negative number is different.

For example, if we want to represent -34 in 8-bit 1’s complement form, then first write the positive number (+34). And invert all 1s in that number by 0s and 0s by 1s in that number. The corresponding inverted number represents the -34 in 1’s complement form. It is also called 1s complement of the number +34.

Using n-bits, the range of numbers that can be represented in 1’s complement form is from – (2^(n-1) – 1) to (2^(n -1) – 1).

2’s Complement Representation

In 2’s complement representation also, the representation of the positive number is same as 1’s complement and sign-magnitude form.

But the representation of the negative number is different. For example, if we want to represent -34 in 2’s complement form then

  1. Write the number corresponding to +34.

  2. Find 1’s complement of +34

  3. Add ‘1’ to the 1’s complement number

  4. The resultant is 2’s complement representation of -34

In other words, the binary representation of-K (negative K) as a N-bit number is 2^(N -1) - K.

Let's look at the 4-bit integer -3 as an example. If it's a 4-bit number, we have one bit for the sign and three bits for the value. We want the complement with respect to 2^3 , which is 8. The complement of 3 (the absolute value of -3) with respect to 8 is 5. 5 in binary is 101. Therefore, -3 in binary as a 4-bit number is 1101, with the first bit being the sign bit.

Comparison

Feature

Sign Magnitude Representation

1st Complement

2nd Complement

Representation of Sign

The most significant bit (MSB) represents the sign: 0 for positive, 1 for negative.

MSB represents the sign, similar to Sign Magnitude.

MSB represents the sign, similar to Sign Magnitude.

Positive Numbers

Represented as usual binary. Example: +5 in 4-bit: 0101.

Represented as usual binary. Example: +5 in 4-bit: 0101.

Represented as usual binary. Example: +5 in 4-bit: 0101.

Negative Numbers

Invert the MSB to 1, rest is unchanged. Example: -5 in 4-bit: 1101.

Invert all bits of the positive number. Example: -5 in 4-bit: 1010.

Invert all bits of the positive number and add 1. Example: -5 in 4-bit: 1011.

Zero Representation

Two representations: +0(0000) and -0 (1000).

Two representations: +0 (0000) and -0(1111).

Single representation: 0000.

Arithmetic Operations

Requires separate handling of sign and magnitude.

Simple addition/subtraction but requires end-around carry for correctness.

Simplifies addition/subtraction, no need for special rules.

Range of Values

For n bits: -2^(n-1) + 1 to 2^(n-1) - 1.

  • Positive: 0 to 2^(n-1) - 1.

  • Negative: -1 to -(2^(n-1) - 1).

For n bits: -2^(n-1) + 1 to 2^(n-1) - 1.

  • Positive: 0 to 2^(n-1) - 1.

  • Negative: -1 to -(2^(n-1) - 1).

For n bits: -2^(n-1) to 2^(n-1) - 1.

  • Positive: 0 to 2^(n-1) - 1.

  • Negative: -2^(n-1) to -1.

Example (4 bits)

  • Positive range: 0000 (0) to 0111 (+7).

  • Negative range: 1000 (-0) to 1111 (-7).

  • Range: -7 to +7 (but with two zeros, +0 and -0).

  • Positive range: 0000 (0) to 0111 (+7).

  • Negative range: 1111 (-0) to 1000 (-7).

  • Range: -7 to +7 (but with two zeros, +0 and -0).

  • Positive range: 0000 (0) to 0111 (+7).

  • Negative range: 1000 (-8) to 1111 (-1).

  • Range: -8 to +7 (no extra zeros).

Advantages

Simple to understand and implement.

Easy to compute negative values.

Ideal for arithmetic operations and eliminates ambiguity with zero.

Disadvantages

Complex arithmetic; two representations for zero.

Requires handling end-around carry.

Slightly more complex to compute negative values initially.

Last updated

Was this helpful?