NUMBER SYSTEMS
SEE2301 EMBEDDED SYSTEMS
Lesson 03 B
Swapna Premasiri
INTRODUCTION TO NUMBER SYSTEMS
• Traditional number systems we know are positional number
systems
• Examples: decimal, binary, hexadecimal, octal
• Positional number systems are also known as weighted
number systems
• The position of each digit in a weighted number system has a
weight
• This weight is assigned based on the base ( or radix) of the system
INTRODUCTION TO NUMBER SYSTEMS
• In a number system with base (radix), r , each digit
can be represented with different digits ranging from
0 to r -1
• For example, each digit in the number 24.36 can have
values ranging between 0 and 9
• Unweighted number systems don’t have positional
weights for digits
Find 2 types of unweighted number systems and their purpose
DECIMAL NUMBERS
• Decimal numbers can be expressed as the sum of the
products of each digit times the column value for that
digit
• For example, the number 24.36 is of the form d0d1 d- ⚫️
d-2
1
• The value is: (d0 x 101 ) + (d1 x 100 ) (d-1 x 10-1) + (d-2 x 10-2
⚫️
)
• Here, the base is 10
• And each digit d can have values ranging between 0 and 9
DECIMAL NUMBERS
•? Based on the example, how can the number 9240
be expressed?
!
• (9 x 103 ) + (2 x 102 ) + (4 x 101) + (0 x 100)
OR
(9 x 1000) + (2 x 100) + (4 x 10) + (0 x 1)
BINARY NUMBERS
• The binary number system is used in digital systems
• The radix (base) is 2
• Values are represented using 0 and 1 only
• For example, the number 13 can be represented as
1101 because:
23 22 21 20
Decima
(8) (4) (2) (1)
l
Numbe
r
BINARY NUMBERS
• Binary counting sequence for numbers 0 to 15:
Binary Number Binary Number
Decima 23 22 21 20 Decima 23 22 21 20
l (8) (4) (2) (1) l (8) (4) (2) (1)
Numbe Numbe
r r
0 0 0 0 0 8 1 0 0 0
1 0 0 0 1 9 1 0 0 1
2 0 0 1 0 10 1 0 1 0
3 0 0 1 1 11 1 0 1 1
4 0 1 0 0 12 1 1 0 0
5 0 1 0 1 13 1 1 0 1
6 0 1 1 0 14 1 1 1 0
7 0 1 1 1 15 1 1 1 1
BINARY ARITHMETIC
BINARY ADDITION
• Rules for binary addition:
Sum Carr
y
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
BINARY ARITHMETIC
BINARY ADDITION (CONTD.)
? Activity:
(a) Add the binary numbers 00111 and 10101.
(b) Convert these numbers to decimal and add them.
(c) Show that the result from (b) is the decimal equivalent of the
result from (a)
! 2 4
2 3
22
21
20
0 0 1 1 1 →
7
1 0 1 0 1 → 2
1
1 1 1 0 0 → 2
BINARY ARITHMETIC
BINARY SUBTRACTION
• Rules for binary subtraction:
Subtrac Borro
tion w
0 0 0 0
0 1 1 1
1 0 1 0
1 1 0 0
BINARY ARITHMETIC
COMPLEMENT OF A BINARY NUMBER
Type 1’s Complement 2’s complement
Inverse all digits. Inverse all digits and then
Method i.e. convert all 0’s to 1’s and add 1 to the LSB (Least
1’s to 0’s Significant Bit)
1011 0100
+
Exampl
1011 0100 0001
e
0101
⚠️ 1’s complement and 2’s complement are important because they permit
the representation of negative numbers
SIGNED BINARY NUMBERS
• There are several ways to represent signed binary
numbers
1. Sign-Magnitude Form (least used)
2. 1’s complement
3. 2’s complement (most used)
• In all cases, the MSB in a signed number is the sign bit
that tells you if the number is positive or negative.
• 0 indicates (+)ve number
• 1 indicates (-)ve number
SIGNED BINARY NUMBERS
• Computers use a modified 2’s complement for signed
numbers
• Positive numbers are stored in true form, with 0 for the
signed bit
• Negative numbers are stored in complement form, with 1
Sign-Magnitude
for the signed bit 1’s Complement 2’s complement
Sum all decimal Invert all bits, sum Sum the weights where
•Metho
d
weights where the weights where there there are 1s while giving
Decimal values ofbitnegative
magnitude
(binary) numbers
are 1s, and give (-) sign a (-)ve value to weight of
positions are 1. in place of signed bit signed bit
1111 0011 → 0000
1111 0100 → 0000 1011
1000 1100 → (23) 1100
Example magnitude bit + 0000
+ (22) positions
(8-bit 0001
register) = –
0000
RANGE OF SIGNED INTEGERS
• Number of all possible combinations for n-bits: 2n
• Range for 2’s complement: -(2n-1) to + (2n-1-1)
• For example, if n = 4, the range is -(2-3) to + (23-1) → -8 to
+7
ARITHMETIC OPERATIONS WITH SIGNED NUMBERS
• Using signed number notation in 2’s complement form
simplifies addition and subtraction when negative
numbers are involved
• If the result of an arithmetic operation exceeds the
number of bits allocated, overflow will occur.
• This happens only when both numbers have the same sign
• The overflow will be indicated by a sign bit of the result
(different from the sign bit of the two numbers being added)
HEXADECIMAL NUMBERS
• Hexadecimal (hex) numbers use 16 characters to
represent numbers
• Numbers: 0 to 9 and
• Alphabetic characters:
Binary Number A to F Binary Number
Decimal Hexadecim 23 22 21 20 Decimal Hexadecim 23 22 21 20
Number al Number (8) (4) (2) (1) Number al Number (8) (4) (2) (1)
0 0 0 0 0 0 8 8 1 0 0 0
1 1 0 0 0 1 9 9 1 0 0 1
2 2 0 0 1 0 10 A 1 0 1 0
3 3 0 0 1 1 11 B 1 0 1 1
4 4 0 1 0 0 12 C 1 1 0 0
5 5 0 1 0 1 13 D 1 1 0 1
6 6 0 1 1 0 14 E 1 1 1 0
7 7 0 1 1 1 15 F 1 1 1 1
HEXADECIMAL CONVERSIONS
Convers
Method Example
ion
For each character, write Note: (B)16 → (11)10
Hex →
its corresponding 4-bit
Bin binary value (3B)16 → 0011 1011
Group by 4 bits and write Note: (1101)2 → (13)10 → (D)16
Bin →
their equivalent hex
Hex character 1101 0010 → (D2)16
Multiply the decimal
Hex → equivalent of each number
by its weight, given that (3B)16 → (3×16 ) + (11×16 ) = (59)10
1 0
Dec
the base 16.
(7562)10 / 16 → int = 7552, rem = 10 →
hex: A
(472)10 / 16 → int = 29, rem = 08 →
HEXADECIMAL ARITHMETIC
• Hexadecimal Addition
• Same as adding decimal numbers
• It might be convenient to first convert [A, B, C, D, E, F] to
decimal
• Examples:
Hex Dec Hex Binary Dec
27 26 25 24 23 22 21 20
23 → 2 3
+1 DF → 1 1 0 1 1 1 1 1 → 223
→ 1 6
39 ⃪ 1 1 0 0 0 1 0 1 1 ⃪ 395
+AC → 1 0 1 0 1 1 0 0 → 172
39 ⃪ 3 9
6
HEXADECIMAL ARITHMETIC
• Hexadecimal subtraction
• Same as as decimal subtraction
• Difference: when borrowing, borrowed "1" that
represents 16 decimal (not 10 decimal).
• Can use 2’s complement to perform subtraction
HEXADECIMAL ARITHMETIC
• Hexadecimal subtraction (continued)
• For example:
Hex
E 5 7 → ED 54+16 77+16
3 9 ⃪ 7
+6 B E → 6 B E
9 9
OR
HEXADECIMAL ARITHMETIC
• Hexadecimal subtraction (continued)
• For example:
Hex Bin
E 5 111 010 011
→ 0 1110 0101 0111
7 1 1
-6 B 011 101 111
→ 0 12’s 0
1100 0100 0001
E complement
1001 0100 0001
1
7 9
OTHER NUMBER SYSTEMS
• What other number systems do you know of?
• Octal
• Binary Coded Decimal
• Can you find applications where these number
systems are used?
BASIC I/O
SEE2301 EMBEDDED SYSTEMS
Lesson 03 B
Swapna Premasiri
BASIC I/O
7 6 5 4 3 2 1 0
P2
Logic '1' 3.3V
Logic '0' 0V
uC
OUTPUT
7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 1
0
P2=0000 0001
0000
P2=0x01
P2=0x00
7 6 5 4 3 2 1 0
1 1 1 1 1 1 1 1
P2=1111 1111
P2=0xFF
7 6 5 4 3 2 1 0
1 0 1 0 1 0 1 0
P2=1010 1010
P2=0xAA
BIT MASKING WHY?
Multiple devices on the same
port
7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 1
BIT MASKING WHY?
Switch the motor on - set bit 7 to
a77'1'66 55 44 33 22 11 00
10 00 00 00 00 00 00 01
P2=1000 0000
P2=0x80
What's happened to the light on
bit '0'
TO SWITCH BITS ON
• OR (|) with mask 10000000 (0x80)
7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 1 P2 Current
1 0 0 0 0 0 0 0 status
| with Mask
1 0 0 0 0 0 0 1 Result
• Allows individual selection of bits
7 6 5 4 3 2 1 0
0 0 1 0 1 1 0 1 P2 Current
1 0 1 0 1 0 0 0 status
| with Mask
1 0 1 0 1 1 0 1 Result
TO SWITCH BITS OFF
• AND (&) with mask 01111111 (0x7f)
7 6 5 4 3 2 1 0
1 0 0 0 0 0 0 1 P2 Current
0 1 1 1 1 1 1 1 status
& with Mask
0 0 0 0 0 0 0 1 Result
• Allows individual selection of bits
7 6 5 4 3 2 1 0
0 0 1 0 1 1 0 1 P2 Current
0 1 0 1 0 1 1 1 status
& with Mask
0 0 0 0 0 1 0 1 Result
INPUT MASKING
7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0
READ STATUS OF INDIVIDUAL BITS
• AND (&) with mask 00001000 (Ox08)
• Output either Zero or non Zero
7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0 P2 Current
0 0 0 0 1 0 0 0 status
& with Mask
0 0 0 0 0 0 0 0 Result
7 6 5 4 3 2 1 0
0 0 0 0 1 0 0 0 P2 Current
0 0 0 0 1 0 0 0 status
& with Mask
0 0 0 0 1 0 0 0 Result
READ STATUS OF
HEXADECIMAL INDIVIDUAL BITS
ARITHMETIC
• AND (&) with mask 00001000 (Ox08)
• Output either Zero or non Zero
7 6 5 4 3 2 1 0
1 1 0 0 0 1 1 1 P2 Current
0 0 0 0 1 0 0 0 status
& with Mask
0 0 0 0 0 0 0 0 Result
7 6 5 4 3 2 1 0
1 1 0 0 1 1 1 1 P2 Current
0 0 0 0 1 0 0 0 status
& with Mask
0 0 0 0 1 0 0 0 Result
THANK YOU