CS111-2020-Lab4-Numbering System - Binary System
CS111-2020-Lab4-Numbering System - Binary System
Labs Manual
2020/2021
Cairo University
Faculty of Computers and Artificial intelligence
Lab 4
Part 1: Numbering System
Common numbering systems are:
● Binary [ base = 2]:
Uses 2 symbols: 0 and 1. Sample numbers: 10, 101, 1101.
● Decimal [ base = 10]:
Uses 10 symbols: 0, 1, 2…, 9. Sample numbers: 20, 35, 999.
● Hexadecimal [ base = 16]:
Uses 16 symbol: 0, 1, 2, …, 9, A, B, C, D, E, F. Sample numbers: 2A, C2, 37.
● Octal [ base = 8]:
Uses 8 symbols: 0, 1, 2, …, 7. Sample numbers: 77, 12, 25.
Computers uses the binary system {0,1} to represent numbers.
Conversions Summary:
To
Decimal Binary Octal Hexadecimal
Decimal - Repeated / Repeated / Repeated /
(whole (whole (whole
numbers) numbers) numbers)
Or * Or * Or * (fractions)
(fractions) (fractions) Weight 16
From Weight 2 Weight 8
Binary Add weights of - Convert every Convert every
1s 3 digits 4 digits
Octal Add digit*weight Split every - Through
Weight 8 digit to 3 Binary
Hexadecimal Add digit*weight Split every Through -
Weight 16 digit to 4 Binary
1
CS111: Fundamentals of CS
Labs Manual
2020/2021
Cairo University
Faculty of Computers and Artificial intelligence
● Decimal → Binary/Hex/Octal:
● Binary ↔ Hex/Octal:
Converting from octal to binary is as easy as converting from binary to octal. Simply look up
each octal digit to obtain the equivalent group of three binary digits.
Octal = 3 4 5
Binary = 011 100 101 = 011100101 binary
Hexadecimal = A 2 D E
Binary = 1010 0010 1101 1110 = 1010001011011110 binary
Possible conversions between numbering systems can be summarized in the following:
2
CS111: Fundamentals of CS
Labs Manual
2020/2021
Cairo University
Faculty of Computers and Artificial intelligence
● Binary/Hex/Octal → Decimal:
Problems:
1. Convert the following numbers to decimal:
a) Binary:
a) 10100101.10111
Solution:
1*20 +0*21 +1*22 +0*23 +0*24 +1*25 +0*26 +1*27 +1*2-1 +0*2-2 +
1*2-3+1*2-4 +1*2-5 =165.71875
b) 110000.11001
Solution:
b) Octal:
a) 2360.156
Solution:
= 0*80 + 6*81 + 3*82 + 2*83 + 1*8-1 + 5*8-2 +6*8-3
= 0 +48+192 + 1024 +.125+.078125 + 0.1171875 = 1264.21484375
b) 2314.106
Solution:
=4*80 + 1*81 + 3*82 + 2*83 + 1*8-1 + 0*8-2 +6*8-3
=4 + 8 + 192 + 1024+ 0.125+ 0 + 0.1171875 = 1228.13671875
3
CS111: Fundamentals of CS
Labs Manual
2020/2021
Cairo University
Faculty of Computers and Artificial intelligence
c) Hexadecimal:
a) 40C.18
Solution:
=12 *160 +0*161+ 4 *162 +1*16-1+8*16-2 =1036.09375
b) 94F.B5
Solution:
=15*160 + 4*161 + 9*162 +11*16-1 + 5*16-2 =2383.70703125
86 2 0 0.5 2 1.0
43 2 1
21 2 1
10 2 0
5 2 1
2 2 0
1 2 1
0
Binary = 10101100. 01
4
CS111: Fundamentals of CS
Labs Manual
2020/2021
Cairo University
Faculty of Computers and Artificial intelligence
0.25 8 2.0
Octal = 254.2
.25 16 4.0
Hexadecimal= AC.4
3. Convert the following binary number to octal and Hexadecimal (don’t use
decimal):
a) 1110111010011.11001
Binary 001 110 111 010 011 . 110 010
Octal 1 6 7 2 3 . 6 2
5
CS111: Fundamentals of CS
Labs Manual
2020/2021
Cairo University
Faculty of Computers and Artificial intelligence
b) 1111100100000.11001
Binary 001 111 100 100 000 . 110 010
Octal 1 7 4 4 0 . 6 2
Hexadecimal 1 F 2 0 . C 8
6
CS111: Fundamentals of CS
Labs Manual
2020/2021
Cairo University
Faculty of Computers and Artificial intelligence
(1110110.10011)2 + (1.1001)2
Solution
1 1 1 0 1 1 0.1 0 0 1 1
+ 1.1 0 0 1
_______________________
1 1 1 1 0 0 0.0 0 1 0 1
(1011011.10101) – (11.10101)
Solution:
1 0 1 1 0 1 1.1 0 1 0 1
1 1.1 0 1 0 1
______________________
1 0 1 1 0 0 0.0 0 0 0 0
5) What is the largest and smallest (most negative) 16-bit binary number that can be
represented with:
a) Unsigned numbers?
Solution:
Rule: 0 to 2n -1
Smallest number = 0
Largest number = 216 -1 =65535
7
CS111: Fundamentals of CS
Labs Manual
2020/2021
Cairo University
Faculty of Computers and Artificial intelligence
b) b) Sign/Magnitude numbers?
Solution:
Rule: -2n-1 to +2n-1
Smallest number = -215 = -32768
Largest number = +215 = 32768
a) 10011001 +01000100
Solution:
10011001
01000100
ــــــــــــــــــــــــــــــــــ
1 1 0 1 1 1 0 1 not overflow
a) 11010010 +10110110
Solution:
11010010
+ 10110110
______________
1 1 0 0 0 1 0 0 0 Overflow
a) 42 10
128 64 32 16 8 4 2 1
Binary 0 0 1 0 1 0 1 0
two’s 0 0 1 0 1 0 1 0
complement
8
CS111: Fundamentals of CS
Labs Manual
2020/2021
Cairo University
Faculty of Computers and Artificial intelligence
b) -12810
128 64 32 16 8 4 2 1
Binary 1 0 0 0 0 0 0 0
two’s 1 0 0 0 0 0 0 0
complement
8) Convert the following decimal numbers to 6-bit two’s complement binary numbers
and add them. Indicate whether or not the sum overflows a 6-bit result:
a) 1610 +910
b) -4 +19
9
CS111: Fundamentals of CS
Labs Manual
2020/2021
Cairo University
Faculty of Computers and Artificial intelligence
The rules for converting a decimal number into floating point are as follows:
Step 4: Set the sign bit, 1 for negative, 0 for positive, according to the sign of
the original number then write the exponent number in next 3 bit then write
mantissa in next 4 bit.
Examples:
1) 2.75
Step 1: convert 2.75 to binary
(2.75)10 ( 10.11)2
Step 4:
10
CS111: Fundamentals of CS
Labs Manual
2020/2021
Cairo University
Faculty of Computers and Artificial intelligence
s e e e m m m m
0 1 1 0 1 0 1 1
2) -6.125
Mantissa: 1100
Step 4:
s e e e m m m m
1 1 1 1 1 1 0 0
3) 0.0625
Step 4:
s e e e m m m m
0 0 0 1 1 0 0 0
11