Thursday, 15 February 2024

weighted and unweighted codes

In the coding, when numbers, letters or words are represented by a specific group of symbols, it is said that the number, letter or word is being encoded. The group of symbols is called as a code. The digital data is represented, stored and transmitted as group of binary bits. This group is also called as binary code. The binary code is represented by the number as well as alphanumeric letter.

Advantages of Binary Code

Following is the list of advantages that binary code offers.

  • Binary codes are suitable for the computer applications.

  • Binary codes are suitable for the digital communications.

  • Binary codes make the analysis and designing of digital circuits if we use the binary codes.

  • Since only 0 & 1 are being used, implementation becomes easy.

Classification of binary codes

The codes are broadly categorized into following four categories.

  • Weighted Codes
  • Non-Weighted Codes
  • Binary Coded Decimal Code
  • Alphanumeric Codes
  • Error Detecting Codes
  • Error Correcting Codes

Weighted Codes

Weighted binary codes are those binary codes which obey the positional weight principle. Each position of the number represents a specific weight. Several systems of the codes are used to express the decimal digits 0 through 9. In these codes each decimal digit is represented by a group of four bits.

Weighted Code

Non-Weighted Codes

In this type of binary codes, the positional weights are not assigned. The examples of non-weighted codes are Excess-3 code and Gray code.

Excess-3 code

The Excess-3 code is also called as XS-3 code. It is non-weighted code used to express decimal numbers. The Excess-3 code words are derived from the 8421 BCD code words adding (0011)2 or (3)10 to each code word in 8421. The excess-3 codes are obtained as follows −

Excess-3 code

Example

BCD to Excess-3 code

Gray Code

It is the non-weighted code and it is not arithmetic codes. That means there are no specific weights assigned to the bit position. It has a very special feature that, only one bit will change each time the decimal number is incremented as shown in fig. As only one bit changes at a time, the gray code is called as a unit distance code. The gray code is a cyclic code. Gray code cannot be used for arithmetic operation.

Gray code

Application of Gray code

  • Gray code is popularly used in the shaft position encoders.

  • A shaft position encoder produces a code word which represents the angular position of the shaft.

Binary Coded Decimal (BCD) code

In this code each decimal digit is represented by a 4-bit binary number. BCD is a way to express each of the decimal digits with a binary code. In the BCD, with four bits we can represent sixteen numbers (0000 to 1111). But in BCD code only first ten of these are used (0000 to 1001). The remaining six code combinations i.e. 1010 to 1111 are invalid in BCD.

BCD code

Advantages of BCD Codes

  • It is very similar to decimal system.
  • We need to remember binary equivalent of decimal numbers 0 to 9 only.

Disadvantages of BCD Codes

  • The addition and subtraction of BCD have different rules.

  • The BCD arithmetic is little more complicated.

  • BCD needs more number of bits than binary to represent the decimal number. So BCD is less efficient than binary.

Alphanumeric codes

A binary digit or bit can represent only two symbols as it has only two states '0' or '1'. But this is not enough for communication between two computers because there we need many more symbols for communication. These symbols are required to represent 26 alphabets with capital and small letters, numbers from 0 to 9, punctuation marks and other symbols.

The alphanumeric codes are the codes that represent numbers and alphabetic characters. Mostly such codes also represent other characters such as symbol and various instructions necessary for conveying information. An alphanumeric code should at least represent 10 digits and 26 letters of alphabet i.e. total 36 items. The following three alphanumeric codes are very commonly used for the data representation.

  • American Standard Code for Information Interchange (ASCII).
  • Extended Binary Coded Decimal Interchange Code (EBCDIC).
  • Five bit Baudot Code.

ASCII code is a 7-bit code whereas EBCDIC is an 8-bit code. ASCII code is more commonly used worldwide while EBCDIC is used primarily in large IBM computers.

Error Codes

There are binary code techniques available to detect and correct data during data transmission.

Error Code Description

Error Detection and Correction

Error detection and correction code techniques

addition and subtraction of unsigned and signed numbers

The direct method of subtraction taught in elementary schools uses the borrow concept. In this method we borrow a 1 from a higher significant position when the minuend digit is smaller than the corresponding subtrahend digit. This seems to be easiest when people perform subtraction with paper and pencil. When subtraction is implemented with digital hardware, this method is found to be less efficient than the method that uses complements.

The subtraction of two n-digit unsigned numbers M - N (N * 0) in base r can be done as follows:

1. Add the minuend M to the r's complement of the subtrahend N. This performs M + (r' - N) = M - N + r'.

2. If M "" N, the sum will produce an end carry r' which is discarded, and what is left is the result M - N.

3. If M < N, the sum does not produce an end carry and is equal to r' - (N - M), which is the r's complement of (N - M). To obtain the answer in a familiar form, take the r' s complement of the sum and place a negative sign in front.

Consider, for example, the subtraction 72532 - 13250 = 59282. The lO's complement of 13250 is 86750. Therefore:

M = 72532

lO's complement of N = +86750

Sum = 159282

Discard end carry 10' = -100000

Answer = 59282

Now consider an example with M < N. The subtraction 13250 - 72532 produces negative 59282. Using the procedure with complements, we have

M = 13250

lO's complement of N = +27468

Sum = 40718

There is no end carry.

Answer is negative 59282 = 10's complement of 40718

Since we are dealing with unsigned numbers, there is really no way to get an unsigned result for the second example. When working with paper and pencil, we recognize that the answer must be changed to a signed negative number. When subtracting with complements, the negative answer is recognized by the absence of the end carry and the complemented result.

Subtraction with complements is done with binary numbers in a similar manner using the same procedure outlined above. Using the two binary numbers X = 1010100 and Y = 1000011, we perform the subtraction X - Y and Y - X using 2's complemenfs:

X= 1010100

2' s complement of Y = +0111101

Sum = 10010001

Discard end carry 27 = - 10000000

Answer: X - Y = 0010001

y = 1000011

2's complement of X = +0101 100

Sum = 1101111

There is no end carry

Answer is negative 0010001 = 2's complement of 1101111

Signed and Unsigned Binary Numbers

 Understanding Number Representation Techniques

  1. Integers can be represented in signed and unsigned ways.
  2. Signed numbers use a sign flag to distinguish between positive and negative values.
  3. Unsigned numbers store only positive numbers.
  4. Techniques include Binary, Octal, Decimal, and Hexadecimal.
  5. Binary Number System is a popular technique used in digital systems.
  6. Binary System represents binary quantities with two possible states.
  7. Binary numbers are indicated by an 0b prefix or a 2 suffix.
  8. Unsigned binary numbers lack a sign bit, while signed binary numbers use a sign bit to distinguish between positive and negative numbers.


 1.Sign-Magnitude form

 

Sign-magnitude is one way to represent signed numbers in digital logic. In this form, a fixed number of bits are dedicated to representing the sign and the remaining bits represent the magnitude (absolute value) of the number. Here's a breakdown:

Key points:

  • Sign bit: The most significant bit (MSB) is used to represent the sign. 0 indicates positive, and 1 indicates negative.
  • Magnitude representation: Remaining bits represent the absolute value of the number, using the same format as unsigned numbers.
  • Range: For n bits, the representable range is - (2^(n-1) - 1) to + (2^(n-1) - 1), meaning both positive and negative numbers can be represented within the same format.

Example (8-bit representation):

  • +43: 00101011
  • -43: 10101011

Limitations:

  • Inefficient: Two representations exist for zero (positive 0 and negative 0), wasting space.
  • Complex arithmetic: Addition and subtraction require different logic depending on the signs, making them more complex than other methods like 2's complement.
  • Overflow detection: Detecting overflow conditions is more challenging compared to other representations.

Comparison with other forms:

  • 1's complement: Similar to sign-magnitude but uses an inverted version of the magnitude for negative numbers. Less complex addition/subtraction but suffers from negative zero and overflow issues.
  • 2's complement: Adds 1 to the 1's complement representation of negative numbers. Eliminates negative zero, simplifies arithmetic, and offers efficient overflow detection. This is the most common representation in modern digital systems.

Applications:

While not widely used in modern digital logic due to its limitations, sign-magnitude has some historical significance and niche applications:
  • Simple educational tool to understand signed number representation.
  • Specialized applications where simplicity is valued over efficiency (e.g., low-power systems).

Addition

A number is represented inside a computer with the purpose of performing some calculations using that number. The most basic arithmetic operation in a computer is the addition operation. That’s why a computer can also be called as an adder.

When adding two numbers with the same signs, add the values and keep the common sign. 

Example 1

Add the numbers (+5) and (+3) using a computer. The numbers are assumed to be represented using 4-bit SM notation.

             111  <- carry generated during addition              0101 <- (+5) First Number            + 0011 <- (+3) Second Number              1000 <- (+8) Sum                 

Let’s take another example of two numbers with unlike signs.

Example  2

Add the numbers (-4) and (+2) using a computer. The numbers are assumed to be represented using 4-bit SM notation.

              000 <- carry generated during addition

              1100 <- (-4) First number

           +  0010 <-(+2) Second Number

              1110 <- (-2) Sum

Here, the computer has given the wrong answer of -6 = 1110, instead of giving the correct answer of -2 = 1010.

 

1's Complement

 By inverting each bit of a number, we can obtain the 1's complement of a number. The negative numbers can be represented in the form of 1's complement. In this form, the binary number also has an extra bit for sign representation as a sign-magnitude form.

2's Complement

 By inverting each bit of a number and adding plus 1 to its least significant bit, we can obtain the 2's complement of a number. The negative numbers can also be represented in the form of 2's complement. In this form, the binary number also has an extra bit for sign representation as a sign-magnitude form

Advertisement

Follow US

Join 12,000+ People Following

Notifications

More

Results

More

Java Tutorial

More

Digital Logic design Tutorial

More

syllabus

More

ANU Materials

More

Advertisement

Top