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

CS111-2020-Lab4-Numbering System - Binary System

Uploaded by

qwer353666
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)
10 views

CS111-2020-Lab4-Numbering System - Binary System

Uploaded by

qwer353666
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/ 11

CS111: Fundamentals of CS

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:

In case of hexadecimal and octal, we divide by 16 and 8 respectively.

● 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

Converting from hexadecimal to binary is as easy as converting from binary to hexadecimal.


Simply look up each hexadecimal digit to obtain the equivalent group of four binary digits.

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:

0*20 +0*21 +0*22 +0*23 +1*24 +1*25 +1*2-1 +1*2-2 +


0*2-3+0*2-4 +1*2-5 = 48.78125

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

2. Convert the following decimal numbers to binary, hexadecimal and octal:


a) 172.25
Convert decimal to binary
Number base Reminder
172 2 0 0.25 2 0.5

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

Convert decimal to octal


Number base Reminder
172 8 4
21 8 5
2 8 2
0

0.25 8 2.0
Octal = 254.2

Convert decimal to hexadecimal


Number base Reminder
172 16 C
10 16 A
0

.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

Binary 0001 1101 1101 0011 . 1100 1000


Hexadecimal 1 D D 3 . C 8

b) 1111100100000.11001
Binary 001 111 100 100 000 . 110 010

Octal 1 7 4 4 0 . 6 2

Binary 0001 1111 0010 0000 . 1100 1000

Hexadecimal 1 F 2 0 . C 8

6
CS111: Fundamentals of CS
Labs Manual
2020/2021
Cairo University
Faculty of Computers and Artificial intelligence

Part 2: Binary System


Problems
1) Add the following numbers without converting to decimal:

(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

2) Subtract the following numbers without using decimal:

(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

3) How many different numbers can be represented with 16 bits?

Solution: 216 = 65536

4) What is the largest unsigned 32-bit binary number?

Solution: 232 - 1 = 2147483647

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

6) Perform the following additions of unsigned binary numbers. Indicate whether or


not the sum overflows an 8-bit result:

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

7) Convert the following decimal numbers to 8-bit two’s complement numbers or


indicate that the decimal number would overflow the range:

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

Floating Point Representation

Sign bit 3 bit exponent 4 bit mantissa

The rules for converting a decimal number into floating point are as follows:

Step 1: Convert the absolute value of the number to binary,

Step 2: Normalize the number to get the mantissa.

Step 3: Find the exponent and express it as a 3 bits excess notation

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 2: normalize the number to get mantissa

10.01=0.1011*22 mantissa: 1011

Step3: Find the exponent and express it as a 3 bits excess notation

Exponent = +2 → 110 (3-bit excess notation)

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

Step 1: 6.125 110.001 (binary)

Step 2: 110.001 =01.110001 truncation error

Mantissa: 1100

Step 3: exponent = +3 → 111 (3-bit excess notation)

Step 4:

s e e e m m m m
1 1 1 1 1 1 0 0

3) 0.0625

Step 1: 0.0625 🡪 0.0001 (binary)

Step 2: 0.0001= 0.1 *2-3 mantissa = 1000

step 3 : exponent = -3 → 001 (3-bit excess notation)

Step 4:

s e e e m m m m
0 0 0 1 1 0 0 0

11

You might also like