Data Representation
1] Numbering Systems: +Example 1: (from Binary to decimal)
A) The Decimal Number System: • (1011.101)2
• Characteristics : 0 1 2 3 4 5 6 7 8 9 = (1x 23) + (0 x 22) + (1 x 21) + (1 x 20)
+ (1 x 2-1) + (0 x 2-2) + (1 x 2-3)
• Weight : based on powers of 10
= 8 + 0 + 2 + 1 + 0.5 + 0 + 0.125
• Example : (101)10 = (11.625)10
B) The Binary Number System: +Example 2: (from Hexadecimal to decimal)
• Characteristics : 0 1 • (2AF.3)16
• Weight : based on powers of 2 = (2 x 162) + (10 x 161) + (15 x 160) + (3 x 16-1)
• Example : (101)2 = 512 + 160 + 15 + 0.1875
= (687.1875)10
C) The Hexadecimal Number
+Example 3: (from Octal to decimal)
System:
• (254.7)8
• Characteristics : 0 1 2 3 4 5 6 7 8 9 = (2 x 82) + (5 x 81) + (4 x 80) +(7 x 8-1)
ABCDEF = 128 + 40 + 4 + 0.875
• Weight : based on powers of 16 = (172.875)10
• Example : (9AB5)16
B) Convert from Decimal:
D) The Octal Number System: • To convert any decimal number to
• Characteristics : 0 1 2 3 4 5 6 7 any other number system:
• Weight : based on powers of 8 1. Divide the number by base.
• Example : (706)8 2. Get the integer quotient for the next
iteration.
3. Get the remainder for the digit.
4. Repeat the steps until the quotient is
2] Conversion: equal to 0.
A) Convert to Decimal:
• Conversions to the decimal BIN
÷ base
number system depends on the base DEC HEX
of the number system you will OCT
convert from ( i.e. 2 in case of binary,
8 in case of oct and 16 in case of hex) BIN
× base
DEC Floating Point
HEX
OCT
BIN × weight
HEX DEC
OCT
1
+Example 1: (from decimal to binary) +Example 3: (from decimal to Hexadecimal)
• (37.375)10 • (423.78)10
(37)10 = (....)2 (423)10 = (....)16
Repeated division: Repeated division:
2 37 remainder 16 423 remainder
2 18 1 16 26 7
2 9 0 16 1 10 (A)
2 4 1 0 1
2 2 0 Read the result upward to give an answer
2 1 0 of (423)10 = (1A7)16
0 1
Read the result upward to give an answer (.78)10 = (....)16
of (37)10 = (100101)2 Repeated multiplication:
0.78 x 16 = 12.48 integer 12 (C)
(0.375)10 = (....)2 0.48 x 16 = 7.68 integer 7
Repeated multiplication: 0.68 x 16 = 10.88 integer 10 (A)
0.375 x 2 = 0.750 integer 0 Read the result downward to give an
0.750 x 2 = 1.500 integer 1 answer of (.78)10 = (.C7A)16
0.500 x 2 = 1.000 integer 1
Read the result downward to give an So (423.78)10 = (1A7.C7A)16
answer of (.375)10 = (.011)2
C) Convert to Binary:
So (37.375)10 = (100101.011)2 • Use the conversion table directly:
DEC HEX OCT BIN
+Example 2: (from decimal to Octal)
0 0 0 0000
• (23.68)10 1 1 1 0001
2 2 2 0010
(23)10 = (....)8 3 3 3 0011
Repeated division: 4 4 4 0100
8 23 remainder 5 5 5 0101
8 2 7 6 6 6 0110
0 2 7 7 7 0111
Read the result upward to give an answer 8 8 - 1000
of (23)10 = (27)8 9 9 - 1001
10 A - 1010
(.68)10 = (....)8 11 B - 1011
Repeated multiplication: 12 C - 1100
0.68 x 8 = 5.44 integer 5 13 D - 1101
0.44 x 8 = 3.52 integer 3 14 E - 1110
0.52 x 8 = 4.16 integer 4 15 F - 1111
Read the result downward to give an
• Represent each digit in
answer of (.68)10 = (.534)8
Hexadecimal by 4 bits to find the
So (23.68)10 = (27.534)8 equivalent binary number.
2
• Represent each digit in Octal by 3 +Example 1: (from binary to Hexadecimal)
bits to find the equivalent binary • (1110100110.011)2
number. = 0011 1010 0110 . 0110
Hexadecimal Octal
= 3 A 6 . 6
8 4 2 1 4 2 1
= (3A6.6)16
+Example 2: (from binary to Octal)
+Example 1: (from Hexadecimal to binary) • (1100100100.001)2
• (9F2.5)16 = 001 100 100 100 . 001
= 9 F 2 . 5
= 1001 1111 0010 . 0101 = 1 4 4 4 . 1
= (100111110010.0101)2 = (1444.1)8
+Example 2: (from Octal to binary) E) Conversion from HEX to OCT
• (72.5)8 and from OCT to HEX:
= 7 2 . 5
• To convert between Hexadecimal
= 111 010 . 101
= (111010.101)2 and Octal, use decimal or binary as a
step between them.
D) Convert from Binary:
• To Hexadecimal System: HEX
BIN
OCT
- value of each 4 digits represents by 1 DEC
digit in hexadecimal
- Start from the right before floating
point +Example 1: (from Hexadecimal to Octal)
- Start from the left after floating point • (A1)16
- If the latest digits smaller than 4
complete them by zero’s
-Using decimal:
(A1)16 = 10 x 161 + 1 x 160 = 160 + 1
• To Octal System: (A1)16 = (161)10
- value of each 3 digits represents by 1
digit in octal 8 161 remainder
- Start from the right before floating 8 20 1
point 8 2 4
- Start from the left after floating point 0 2
- If the latest digits smaller than 3 (161)10 = (241)8
complete them by zero’s So (A1)16 = (241)8
• Use the conversion table directly -Using binary:
( A 1 )16
= (1010 0001)2
= (010 100 001)2
=( 2 4 1 )8
= (241)8
3
+Example 2: (from Octal to Hexadecimal) B) The 2’s complement:
• (71)8 - Method #1 -
• Using ther rule : 2n -N
-Using decimal: where: n is number of bits , N is digit
(71)8 = 7 x 81 + 1 x 80 = 56 + 1
(71)8 = (57)10 +Example:
16 57 remainder If n= 4bits , The 2’s complement of
16 3 9 (0010)2 is .......
0 3 2n -N = 24 – 2 = 14
(57)10 = (39)16 2 14 remainder
So (71)8 = (39)16 2 7 0
2 3 1
-Using binary: 2 1 1
( 7 1 )8 0 1
= (111 001)2 (14)10 = (1110)2
= (0011 1001)2 so the 2’s complement of (0010)2 is (1110)2
=( 3 9 )16
= (39)16 - Method #2 -
• After the first 1 convert all 0 to 1
and all 1 to 0 (from right to left )
3] Negative Number
Representation : - Method #3 -
• There are two formats for • Convert all 0 to 1 and all 1 to 0 then
representing negative numbers in add one to the result
base-2 system:
1. Sign-magnitude - Weights -
2. 2’s complement • Weights of 2’s complement (3 bit)
are : -4 2 1
A) Sign-magnitude: • Weights of 2’s complement (4 bit)
• This type uses one bit for the sign are : -8 4 2 1
(0 = positive, 1 = negative) and the • Weights of 2’s complement (n bit)
remaining bits represent the are : -2n-1 2n-2 2n-3 … 21 20
magnitude of the number.
0 1
+ve number -ve number
example: example:
(0001)2 = +1 (1001)2 = -1
• Note:
(Sign mag.) = -5
1. (1101)2
(Unsigned mag.) = 13
2. (0100)2 (Sign mag.) = 4
4
+Example: C) Subtraction in Binary:
What are the maximum and minimum • Procedures:
values of a 4-bits binary number 1. The number of bits of the two number
represented in 2’s complement ? must be the same.
2’s complement 2. The first number don’t change.
( -ve representation ) 3. Get the 2’s complement of the second
of a binary number
number.
-8 4 2 1
4. Add the new 2 numbers.
5. If the number of digits for result > the
number of digits for 2 numbers (carry):
Min. Value: Max. Value: • Neglect the carry and the result is +ve.
put 1 in -ve bits put 0 in -ve bits 6. If the number of digits for result = the
put 0 in +ve bits put 1 in +ve bits number of digits for 2 numbers (no carry):
(1000)2 = -8 (0111)2 = +7 • Get the 2’s complement for the result
and the result is –ve.
4] Arithmetic operations: +Example 1: calculate ( 5 - 3 ) in binary
A) Addition in Binary: (5)10 = (101)2 = (0101)2
• Rules: (3)10 = (11)2 = (0011)2 its 2’s comp. is (1101)2
0+0=0 1 1
0+1=1
0 1 0 1
1 + 1 = 0 ( & carry 1 ) +
1 + 1 + 1 = 1 ( & carry 1 ) 1 1 0 1
1 0 0 1 0
+Examples: 1 1 extra bit appeared
Neglect it and
1 0 1 1 1 1 the result is +ve.
+ +
0 1 0 1 1 0
so (0101)2 - (0011)2 = (0010)2
1 1 1 1 1 0 1
B) Addition in Hexadecimal: +Example 2: calculate ( 11 - 5 ) in binary,
• Procedures: given that number of bits = 5
1. Add one column at a time. (11)10 = (1011)2 = (01011)2
2. Convert to decimal and add the numbers. (5)10 = (101)2 = (00101)2 its 2’s comp.
3. If the result of step two is 16 or larger, is (11011)2
subtract the result from 16 and carry 1 to
the next column. 1 1 1
4. If the result of step two is less than 16, 0 1 0 1 1
convert the number to hexadecimal. +
1 1 0 1 1
+Example: 1 0 0 1 1 0
1 0 0 1 extra bit appeared
Neglect it and
8 A 5 C
+ the result is +ve.
F 3 9 A
1 7 D F 6 so (01011)2 - (00101)2 = (00110)2
5
+Example 3: calculate ( 5 - 9 ) in binary 5] Notes:
(5)10 = (101)2 = (00101)2 • How many values can be
(9)10 = (1001)2 = (01001)2 its 2’s comp.
represented in n bits ??
is (10111)2
If n = 5 …
1 1 1 The rule is ( 2n )
0 0 1 0 1 So in 5 bits we can represent 25 = 32
+
1 0 1 1 1 values ( from 00000 to 11111 ) in
1 1 1 00 decimal is ( from 0 to 31 ).
no extra bit appeared
the result is -ve. • What’s the Largest and Smallest
number that can represented in n
so (00101)2 - (01001)2 = (11100)2 and it is a digits ?
negative number If n = 5 …
The rule of the largest value is ( 2n-1 )
- The Decimal value of a negative binary So in 5 bits the largest value is 25-1 =
number - (11111)2 in decimal (31)10
• To know the value of the negative
And always the Smallest value is 0
number (11100)2 use one of the following
methods : So the smallest value is (00000)2 in
decimal (0)10
First method: get the 2’s complement of
that negative number to know its positive • How many bits needed to
value then add negative to that positive represent x decimal value ?
value. If value (x) = 17 …
(11100)2 its 2’s complement is (00100)2
The rule is ( 2n-1 -1 < x < 2n -1 )
(00100)2 = 4
(11100)2 = -4 24 -1 < 17 < 25 -1
So the n bits can represent 17 is 5 bits
Second method: use Weights of 2’s where (17)10 = (10001)2
complement.
(11100)2 = (1 x -16) + (1 x 8) + (1 x 4) = -4