CS-1101 - DLD - FALL 2024 - Lect 4-9
CS-1101 - DLD - FALL 2024 - Lect 4-9
CHAPTER 2
1
TOPICS:
Decimal Numbers
Binary Numbers
Decimal-to-Binary Conversion
Hexadecimal Numbers
Binary Arithmetic
Complements of Binary Numbers
Binary Coded Decimal (BCD)
Digital Codes Concepts
Error Codes
Lecture # 4-9
DR. NAJEEB UR REHMAN
FROM ZERO TO ONE Number Systems
• Decimal
– Base 10
• Binary
– Base 2
• Hexadecimal
– Base 16
Chapter 1 <2>
Decimal Numbers (Base 10)
3
You are familiar with the decimal number system because you use
decimal numbers every day.
Weighted structure of Decimal Number systems ?
Decimal number system uses ten digits, 0 through 9 to represents a
certain quantity.
These ten digits do not limit you to express any Large or small
number
How you will Read (pronounce) 23 !
The position of each digit in a decimal
number indicates the magnitude of
the quantity represented and can be
assigned a weight.
FROM ZERO TO ONE Review: Decimal Numbers
• Base 10 (our everyday number system)
1000's column
10's column
1's column
100's column
537410 =
Base 10
Chapter 1 <4>
FROM ZERO TO ONE Review: Decimal Numbers
• Base 10 (our everyday number system)
1000's column
10's column
1's column
100's column
537410 = 5 × 103 + 3 × 102 + 7 × 101 + 4 × 100
five three seven four
thousands hundreds tens ones
Base 10
Chapter 1 <5>
Binary Numbers (Base 2)
6
Binarysystem has only two digits
(Base-two system)
The two binary digits (bits) are 1 and
0.
Theposition of a 1 or 0 in a
binary number indicates its
weight, or value within the
number, just as in decimal
number systems.
Theweights in a binary number
are based on powers of two.
The Weighting Structure of Binary Numbers
7
A binary number is a weighted number
Every Digit in Binary Number has some associated Weight.
The right-most bit is the LSB (least significant bit) in a binary whole
number
Weight of = 1
The weights increase from right to left by a power of two for each bit.
The left-most bit is the MSB (most significant bit)
its weight depends on the size (Length) of the binary number.
FROM ZERO TO ONE Decimal and Binary Numbers
• Base 10 (our everyday number system)
1000's column
10's column
1's column
100's column
537410 = 5 × 103 + 3 × 102 + 7 × 101 + 4 × 100
five three seven four
thousands hundreds tens ones
2's column
1's column
4's column
11012 =
Base 2
Chapter 1 <8>
FROM ZERO TO ONE Decimal and Binary Numbers
• Base 10 (our everyday number system)
1000's column
10's column
1's column
100's column
537410 = 5 × 103 + 3 × 102 + 7 × 101 + 4 × 100
five three seven four
thousands hundreds tens ones
2's column
1's column
4's column
11012 = 1 × 23 + 1 × 22 + 0 × 21 + 1 × 20 = 1310
one one no one
eight four two one
Base 2
Chapter 1 <9>
FROM ZERO TO ONE Powers of Two
• 20 = • 28 =
• 21 = • 29 =
• 22 = • 210 =
• 23 = • 211 =
• 24 = • 212 =
• 25 = • 213 =
• 26 = • 214 =
• 27 = • 215 =
Chapter 1 <10>
FROM ZERO TO ONE Powers of Two
• 20 = 1 • 28 = 256
• 21 = 2 • 29 = 512
• 22 = 4 • 210 = 1024
• 23 = 8 • 211 = 2048
• 24 = 16 • 212 = 4096
• 25 = 32 • 213 = 8192
• 26 = 64 • 214 = 16384
• 27 = 128 • 215 = 32768
• Handy to memorize up to 29
Chapter 1 <11>
FROM ZERO TO ONE Binary to Decimal Conversion
• Binary to decimal conversion:
– Convert 100112 to decimal
Chapter 1 <12>
FROM ZERO TO ONE Binary to Decimal Conversion
• Binary to decimal conversion:
– Convert 100112 to decimal
– 16×1 + 8×0 + 4×0 + 2×1 + 1×1 = 1910
Chapter 1 <13>
FROM ZERO TO ONE Decimal to Binary Conversion
• Division method:
– Repeatedly divide by 2, remainder goes in next most
significant bit
Chapter 1 <14>
Decimal To Binary Conversion
Begin by dividing Decimal Number by 2. 15
Then divide each resulting quotient by 2 until there is 0 whole-number
quotient.
The remainders generated by each division form the binary number.
Convert :
Convert , , ,
Home Work
16
FROM ZERO TO ONE Number Conversion - Example
• Binary to decimal conversion:
– Convert 111012 to decimal
Chapter 1 <17>
FROM ZERO TO ONE Number Conversion - Example
• Binary to decimal conversion:
– Convert 111012 to decimal
– 16×1 + 8×1 + 4×1 + 2×0 + 1×1 = 2910
Chapter 1 <18>
Hexadecimal Numbers
19
The hexadecimal number system has sixteen characters
It is used primarily as a compact way of displaying or writing binary numbers
(Shorthand)
long binary numbers are difficult to read and write
Imagine writing a sixteen bit instruction for a microprocessor system in 1s and 0s.
It is much more efficient to use hexadecimal or octal
Hexadecimal is widely used in computer and microprocessor applications.
Have you ever seen address of a variable?
The hexadecimal number system has a base of sixteen
Composed of 16 numeric and alphabetic characters.
Most digital systems process binary data in groups that are multiples of four
bits
Hexadecimal number very convenient because each hexadecimal digit represents a 4-
FROM ZERO TO ONE Hexadecimal Numbers
Hex Digit Decimal Equivalent Binary Equivalent
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
A 10
B 11
C 12
D 13
E 14
F 15
Chapter 1 <20>
FROM ZERO TO ONE Hexadecimal Numbers
Hex Digit Decimal Equivalent Binary Equivalent
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111
Chapter 1 <21>
Binary-to-Hexadecimal Conversion
22
Converting a binary number to hexadecimal is a straightforward procedure.
Simply break the binary number into 4-bit groups, starting at the right-most
bit and replace each 4-bit group with the equivalent hexadecimal symbol.
8 4 2 1
Hexadecimal-to-Binary Conversion
23
To convert from a hexadecimal number to a binary number, reverse the
process and replace each hexadecimal symbol with the appropriate four bits.
24
Hexadecimal-to-Decimal & Vice
Versa Conversion
• Class Assignment 1
1. Convert the following hexadecimal numbers to decimal:
(a) 1C16 (b) A8516
2. Convert the decimal number 650 to hexadecimal
FROM ZERO TO ONE Bits, Bytes, Nibbles…
• Bits 10010110
– msb: most significant bit most least
significant significant
– lsb: least significant bit bit bit
byte
Chapter 1 <25>
FROM ZERO TO ONE Bits, Bytes, Nibbles…
• Bits 10010110
– msb: most significant bit most least
significant significant
– lsb: least significant bit bit bit
byte
Chapter 1 <26>
27
BINARY ADDITION:
Binary BINARY SUBTRACTION:
In fourth case, a binary addition is creating a sum of (1 + 1 = 10) i.e. 0 is written in the
given column and a carry of 1 over to the next column.
Carry is left over bit
When there is a carry of 1, you have a situation in which three bits are being added (a bit
in each of the two numbers and a carry bit).
29
FROM ZERO TO ONE Binary Addition Examples
• Add the following 1
1001
4-bit binary
+ 0101
numbers
1110
111
• Add the following 1011
4-bit binary + 0110
numbers 10001
Overflow!
Chapter 1 <30>
FROM ZERO TO ONE Overflow
• Digital systems operate on a fixed number of
bits
• Overflow: when result is too big to fit in the
available number of bits
• See previous example of 11 + 6
111
1011
+ 0110 Software Bug
The Ariane 5 Disaster
10001
Overflow!
Chapter 1 <31>
Binary Arithmetic
Binary Subtraction:
32
The four basic rules for subtracting bits are as follows:
33
34
Class Assignment 1
Solve the Above questions for practice, and disuses with me if needed
35
Signed Numbers
Chapter 1 <38>
FROM ZERO TO ONE “Taking the Two’s Complement”
• Flips the sign of a two’s complement
number.
• Method:
1. Invert the bits
2. Add 1
• Example: Flip the sign of 310 = 00112
1. 1100
2. + 1
1101 = -310
Chapter 1 <39>
FROM ZERO TO ONE Two’s Complement Examples
Take the two’s complement of 610 = 01102
Chapter 1 <40>
41
42
1010 0010
-0010 1101
+1
1110
1000
1010
+1110
1000
43
32 42 1
Subtraction Using Two’s Complement
1.First, find the 2's complement of the negative number. 44
2.Add both the numbers
If subtrahend larger
1.Find the 2's complement of the result.
2. Add a negative sign before the number so that we can identify that it
is a negative number(Only For when If subtrahend larger).
Example: 1101 and -1001
Example: 1101 and -1110
2's complement of number 2's complement of Number
0110+1=0111 0001+1=0010
Add 1101+0111=1 0100 Add 1101+0010= 1111
Applications
Digital clocks, digital thermometers, digital meters, and other devices with seven-
segment displays typically use BCD code to simplify the displaying of decimal
numbers.
It is particularly useful if only limited processing is required, such as in a digital
thermometer.
Digital Device with Numeric Keypad
49
50
The Gray Code
Simply
bring it down
as it is
MSB
Simply
bring it down
as it is