0% found this document useful (0 votes)
19 views

Lesson-6-Codes

The document provides an overview of coding, focusing on binary code and its various types, including weighted and non-weighted codes, Binary Coded Decimal (BCD), and alphanumeric codes like ASCII. It explains the advantages and disadvantages of these coding systems, their applications, and methods for code conversion. Additionally, it highlights the importance of binary code in computer programming and data representation.

Uploaded by

Monalicious69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lesson-6-Codes

The document provides an overview of coding, focusing on binary code and its various types, including weighted and non-weighted codes, Binary Coded Decimal (BCD), and alphanumeric codes like ASCII. It explains the advantages and disadvantages of these coding systems, their applications, and methods for code conversion. Additionally, it highlights the importance of binary code in computer programming and data representation.

Uploaded by

Monalicious69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Codes

Codes
Introduction
Code powers everything from laptops to vending machines to cars. But how
does it work? In this chapter, you'll learn what code is, why so many coding
languages exist, and how they are matched to achieve specific tasks. Most
importantly, you’ll learn why having a basic understanding of code could be
beneficial for you.
Codes
Topic 1-Binary Code
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.
Codes
Topic 1-Binary Code
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
Codes
Topic 1-Binary Code
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.

0+0+2+0 = 2 0+4+0+0 = 4
Codes
Topic 1-Binary 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 −
Codes
Topic 1-Binary Code
Example
0 0 0 0
0 0 1 1
=================
0 0 1 1

1 1
0 0 0 1
0 0 1 1
=================
0 1 0 0
Codes
Topic 1-Binary 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.
Codes
Topic 1-Binary 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.
Codes
Topic 1-Binary Code
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.
Codes
Topic 1-Binary 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..
Codes
Topic 1-Binary Code
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.
Codes
Topic 1-Binary Code
Alphanumeric codes
• 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.
Codes
Topic 1-Binary Code
Codes Conversion
There are many methods or techniques which can be used to convert code
from one format to another. We'll demonstrate here the following
• Binary to BCD Conversion
• BCD to Binary Conversion
• BCD to Excess-3
• Excess-3 to BCD
Binary to BCD Conversion
Steps
• Step 1 -- Convert the binary number to decimal.
• Step 2 -- Convert decimal number to BCD.
Codes
Topic 1-Binary Code
Codes Conversion
Example − convert (11101)2 to BCD.
Step 1 − Convert to Decimal
Binary Number − 111012
Calculating Decimal Equivalent −
Step Binary Number Decimal Number
Step 1 111012 ((1 × 24) + (1 × 23) + (1 × 22) + (0 × 21) + (1 × 20))10
Step 2 111012 (16 + 8 + 4 + 0 + 1)10
Step 3 111012 2910
0+0+2+0 = 2
Result 2 9
8+0+0+1 = 9
(11101)2 = (0010 1001)BCD
Codes
Topic 1-Binary Code
Codes Conversion
BCD to Binary Conversion
Steps
• Step 1 -- Convert the BCD number to decimal.
• Step 2 -- Convert decimal to binary.
Example − convert (0010 1001)BCD to Binary.
Step 1 - Convert to BCD
BCD Number − (0010 1001)BCD
Calculating Decimal Equivalent. Convert each four digit into a group and get
decimal equivalent for each group.
Codes
Topic 1-Binary Code
Codes Conversion
BCD to Binary Conversion

Step BCD Number Conversion


Step 1 (0010 1001)BCD 00102 10012
Step 2 (0010 1001)BCD 210 910
Step 3 (0010 1001)BCD 2910

BCD Number − (0010 1001)BCD = Decimal Number − 2910


Step 2 - Convert to Binary
Use long division method for decimal to binary conversion.
Decimal Number − 2910
Codes
Topic 1-Binary Code
Codes Conversion
BCD to Binary Conversion
Step Operation Result Remainder
Step 1 29 / 2 14 1
Step 2 14 / 2 7 0
Step 3 7/2 3 1
Step 4 3/2 1 1
Step 5 1/2 0 1

As mentioned in Steps 2 and 4, the remainders have to be arranged in the reverse order so that
the first remainder becomes the least significant digit (LSD) and the last remainder becomes the
most significant digit (MSD).
Decimal Number − 2910 = Binary Number − 111012
Result: (0010 1001)BCD = (11101)2
Codes
Topic 1-Binary Code
Codes Conversion
BCD to Excess-3
Steps
• Step 1 -- Convert BCD to decimal.
• Step 2 -- Add (3)10 to this decimal number.
• Step 3 -- Convert into binary to get excess-3 code.
Example − convert (0110)BCD to Excess-3.
Step 1 − Convert to decimal
(0110)BCD = 610 0+4+2+0 = 6

Step 2 − Add 3 to decimal


(6)10 + (3)10 = (9)10
Codes
Topic 1-Binary Code
Codes Conversion
BCD to Excess-3
Example − convert (0110)BCD to Excess-3.
Step 3 − Convert to Excess-3
(9)10 = (1001)2
Result
(0110)BCD = (1001)XS-3
Codes
Topic 1-Binary Code
Codes Conversion
Excess-3 to BCD Conversion
Steps = 3 in decimal

• Step 1 -- Subtract (0011)2 from each 4 bit of excess-3 digit to obtain the
corresponding BCD code.
Example − convert (10011010)XS-3 to BCD. 1001=9 1 0 10 = 10
deduct -3 deduct -3
Given XS-3 number = 1 0 0 1 1 0 1 0 ---------------- ----------------
Subtract (0011)2 =10010111 =6 =7
-------------------- Convert = 0110 0111
BCD = 0 1 1 0 0 1 1 1
Result = 0110 0111
Result: (10011010)XS-3 = (0110 0111)BCD
Codes
Topic 1-Binary Code
Codes Conversion
Other forms of binary code
The bit string is not the only type of binary code: in fact, a binary system in
general, is any system that allows only two choices such as a switch in an
electronic system or a simple true or false test.
Braille
Braille is a type of binary code that is widely used by the blind to read and write
by touch, named for its creator, Louis Braille. This system consists of grids of six
dots each, three per column, in which each dot has two states: raised or not
raised. The different combinations of raised and flattened dots are capable of
representing all letters, numbers, and punctuation signs.
Codes
Topic 1-Binary Code
Coding Systems
ASCII code
The American Standard Code for Information Interchange (ASCII), uses a 7-bit
binary code to represent text and other characters within computers,
communications equipment, and other devices. Each letter or symbol is
assigned a number from 0 to 127. For example, lowercase "a" is represented by
1100001 as a bit string (which is "97" in decimal).
Codes
Topic 1-Binary Code
Coding Systems
Binary-coded decimal
Binary-coded decimal (BCD) is a binary encoded representation of integer
values that uses a 4-bit nibble to encode decimal digits. Four binary bits can
encode up to 16 distinct values; but, in BCD-encoded numbers, only ten values
in each nibble are legal, and encode the decimal digits zero, through nine. The
remaining six values are illegal and may cause either a machine exception or
unspecified behavior, depending on the computer implementation of BCD
arithmetic.
BCD arithmetic is sometimes preferred to floating-point numeric formats in
commercial and financial applications where the complex rounding behaviors
of floating-point numbers is inappropriate.[16]
Codes
Topic 1-Binary Code
Coding Systems
How to Write in Binary Code Step by Step
To understand computer programming, you need to know coding languages.
You also need to know the importance of binary numbers, which determine the
way you store information on your computer. Knowing binary code can give you
another way to communicate important information related to the company
you work for.
In this article, we define what binary is, why binary code is important, examples
of binary letter codes and how to write in binary.
Codes
Topic 1-Binary Code
Coding Systems
What is binary?
Binary is a numeric system that is comprised of digits zero and one. A computer
uses these digits to place data within the computer. The digits can represent
letters used to convey important messages. For example, zero means that you
have no electrical flow within your computer's hardware systems, whereas one
means that you have enough electrical flow for it to run functionally.
Additionally, each binary code must be produced in a physical form, so it can be
properly stored.
Codes
Topic 1-Binary Code
Coding Systems
Why is binary important?
Binary code is important because it can use the digits of zero and one to solve
complex problems in regards to the mechanical functions of computers. Binary
numbers also make it easier to create designs for computers and make it more
cost-effective as well. These numbers have allowed organizations to maximize
the energy output of these systems, in addition to working on reliable systems
that increase their performance in the workplace.
Codes
Topic 1-Binary Code
Coding Systems
Binary letter codes
You write binary codes in arbitrary ways using the UTF-8 binary coding system.
This coding system is a collection of systems, which gives a fixed binary number
to all letters of the alphabet. They also incorporate numbers and symbols, and
they're used by organizations worldwide to build a universal standard that
improves the performance of all products using this coding system.
Let's look at binary codes for all letters of the English alphabet to give you an
idea of how to write functions in code:
Codes
Topic 1-Binary Code
Coding Systems
Binary letter codes
A: 01000001 N: 01001110 a: 01100001 n: 01101110
B: 01000010 O: 01001111 b: 01100010 o: 01101111
C: 01000011 P: 01010000 c: 01100011 p: 01110000
D: 01000100 Q: 01010001 d: 01100100 q: 01110001
E: 01000101 R: 01010010 e: 01100101 r: 01110010
F: 01000110 S: 01010010 f: 01100110 s: 01110011
G: 01000111 T: 01010010 g: 01100111 t: 01110100
H: 01001000 U: 01010101 h: 01101000 u: 01110101
I: 01001001 V: 01010110 i: 01101001 v: 01110110
J: 01001010 W: 01010111 j: 01101010 w: 01110111
K: 01001011 X: 01011000 k: 01101011 x: 01111000
L:01001100 Y: 01011001 l: 01101100 y: 01111001
M:01001101 Z: 01011010 m: 01101101 z: 01111010
Codes
Codes
Topic 1-Binary Code
How to write in binary
1. Use the American Standard Character for Information Interchange (ASCII)
to write binary code
If you're in a technical profession, you need to know the ASCII code for
characters in these computers. This is the primary method to convert code into
letters. This code is represented by 128 characters seen as numbers, which give
commands to computers to transfer data between systems. You can ask the
interviewer if the company you're interviewing for abides by this system. The
UTF-8 binary code matches with this code for lettering. The binary code is
different from lowercase letters as well.
Codes
Topic 1-Binary Code
How to write in binary
2. Calculate the code to find out the letter it corresponds to
The zeros and ones used in binary coding are viewed as an on and off system.
When you see zeros (0), this is referred to as "off", so you do not factor in the
zero in the calculation. The one (1) is "on" and it is the value you use to find the
number that corresponds with the binary code you used.
For example:
If you used the binary code 01001111, you're only using the ones to make the
calculation. You need to square all the numbers from the right to left using the
digit 2^n. Let's make the calculation starting with the correspondence of
numbers with the value:
Codes
Topic 1-Binary Code
How to write in binary
2. Calculate the code to find out the letter it corresponds to
For example: 01001111
1 = 2^0 Let’s make the calculation: 1 = (1 x 2^0) = 1
1 = 2^1 1 = (1 x 2^1) = 2
1 = 2^2 1 = (1 x 2^2) = 4
1 = 2^3 1 = (1 x 2^3) = 8
0 = 2^4 0 = Since zeros are off, calculations always equal zero.
0 = 2^5 0 = zero here too
1 = 2^6 1 = (1 x 2^6) = 64
0 = 2^7 0=0
If you add, 64, 8, 4, 2 and 1 together, then you would get 79. On the ASCII chart, that
resembles the letter “O”.
Codes
Topic 1-Binary Code
How to write in binary
3. Use the code to formulate words
Use these codes to combine letters to make words. These words
communication the functions the computer needs to take to keep it operational
for the user.
For example:
We wanted to get the word OK, we know that the code for “O” is 01001111.
Let's test the code 01001011 to see if this matches up with the letter “K” on the
ASCII chart. The total calculation we need is 75. Let's check it out below.:
Codes
Topic 1-Binary Code
How to write in binary
3. Use the code to formulate words
For example: 01001011
1 = 2^0 The final calculation is: 1 = (1 x 2^0) = 1
1 = 2^1 1 = ( 1 x 2^1) = 2
0 = 2^2 0=0
1 = 2^3 1 = (1 x 2^3) = 8
0 = 2^4 0=0
0 = 2^5 0=0
1 = 2^6 1 = (1 X 2^6) = 64
0 = 2^7 0=0
64 + 8 + 2 + 1 = 75 = “K”
Binary calculation: 01001111 + 01001011 = OK
Codes
Topic 1-Binary Code
Professionals that need to use binary code
Check out a list of job titles that require the knowledge of binary code to be successful
with the organization they work for:
• Computer programmers: Computer programmers use a variety of programming
languages to make software applications on a computer or a smartphone. They must
borrow from thousands of coding libraries that assist them with creating programs in a
timely fashion.
• Web developers: A web developer may need to use binary code to improve the
functionality of their client's current website. Code is geared towards enhancing the
user's experience to increase their chance of buying and selling products directly from
the website.
• Software engineers: A software engineer works with other developers to design
software in addition to building models that make application codes for the
application they're working with.
Codes
Topic 2- Integers on Computer
Integers are stored in binary format. Integer types in the C language come in
different sizes. On our computer we have:
char 8 bits
short 16 bits
int 32 bits
long 64 bits
We can also add the word unsigned before each type. Example: the definition
unsigned char x;
tells the C compiler to treat the 8 bits as a positive number. If we leave off the
word unsigned then by default each type is signed. This means some of the bit
patterns stored in the variable will be treated as positive and some will be
treated as negative. How are negative numbers stored?
Codes
Topic 2- Integers on Computer
There is no negative sign! The only thing stored are bits (0 or 1). The rule: if the
number is signed and the left-most bit is 1, then the number is negative. Note
the left-most bit is called the leading bit.
Two's Complement Format.
Signed positive numbers have leading bit equal to zero. We can find the value
of a positive number by converting it as talked about on the previous page. For
instance, consider the char
00101110
128 64 32 16 8 4 2 1
So, the decimal equivalent is 0 0 1 0 1 1 1 0
0 0 32 0 8 4 2 0 = 46
0+0+32+0+8+4+2+0 = 46
Codes
Topic 2- Integers on Computer
Two's Complement Format.
Now consider the char 128 64 32 16 8 4 2 1
1 1 0 1 0 0 1 0
11010010 -128 64 0 16 0 0 2 0 = -46
This has the decimal value -46. You can check this by doing binary addition:
111 1 11 1
11010010
+00101110
Since a char is only 8 bits, the carry out from the 8th column, the 1,
--------------
is lost and we are left with 8 zero bits. Since the second bit pattern
100000000
represents the number 46, the first bit pattern represents the
number -46! We say that these two bit patterns are two’s
complements of each other because they add to zero. This works
for the other sizes of integers too. Let's work some more examples.
Codes
Topic 2- Integers on Computer
Two's Complement Format.
Problem: Given the char bit pattern 10001101, what decimal number is it?
The number is negative because its type is signed (default) & the leading bit is 1.
Strategy:
Compute the two’s complement. Find its decimal value. Then put a minus in
front.
The two’s complement is a bit pattern that added to the given one makes zeros.
10001101
+???? ????
-------------
00000000
Codes
Topic 2- Integers on Computer
Two's Complement Format.
Problem: Given the char bit pattern 10001101, what decimal number is it?
Working from right to left. We need to get a 0 in the right-most column of the
result:
10001101
The question mark above is either 0 or 1. The 0 does not work.
+ ? The 1 gives us 1+1 which is 10 in binary. The 0 is written down
------------- and the 1 is carried:
0
1
10001101 We are now working on the second column. We need two 1's in
+ ?1 the second column to get the result 10. Since we already have
one 1, we make the question mark above a 1 to get two 1's. Now
-------------
we have:
0
Codes
Topic 2- Integers on Computer
Two's Complement Format.
Problem: Given the char bit pattern 10001101, what decimal number is it?
11
10001101 In the third column we already have two 1's so we make the
+ ?11 question mark above be a 0. So the result is 10 and we have a
carry of 1 into the fourth column:
-------------
00
111 1111 11111 111111 1 11 1 1 1 1
10001101 10001101 10001101 10001101 10001101
+ ?011 + ?0011 + ?10011 + ?110011 +01110011
------------- ------------- ------------- ------------- -------------
000 0000 00000 000000 00000000
Codes
Topic 2- Integers on Computer
Two's Complement Format.
Problem: Given the char bit pattern 10001101, what decimal number is it?
So the two’s complement is 01110011. This is a positive number. We convert to
decimal: 0+64+32+16+0+0+2+1 = 115.
So our original number has a decimal value of -115.
Codes
Topic 2- Integers on Computer
Two's Complement Format.
Problem: a char value has a bit pattern of 11110000, what is its decimal value?
First the number is negative because it is signed and the leading bit is 1. So we
find the two’s complement:
111
11110000 11110000
+???????? +00010000
-------------- --------------
00000000 00000000

So the two’s complement is 00010000 which is 16 in decimal. So the decimal


value of the original number is -16.
Codes
Topic 2- Integers on Computer
Two's Complement Format.
Problem: Given the decimal number -94. Find the signed char bit pattern for the
number. We'll do this in two steps: we will get the bit pattern for 94, then we'll
take the twos complement of that pattern.
So 94 = 64+16+8+4+2 so the 8-bit pattern is:
01011110
Now we find the two’s complement as before. The result is
10100010
This is the bit pattern for -94.

You might also like