UNIT NO.2
Number System
(Solved Exercise)
Short Questions
1. What is the primary purpose of the ASCII encoding scheme?
2. Explain the difference between ASCII and Unicode.
3. How does Unicode handle characters from different languages?
4. What is the range of values for an unsigned 2-byte integer?
5. Explain how a negative integer is represented in binary.
6. What is the benefit of using unsigned integers?
7. How does the number of bits affect the range of integer values?
8. Why are whole numbers commonly used in computing for quantities that cannot be negative?
9. How is the range of floating-point numbers calculated for single precision?
10. Why is it important to understand the limitations of floating-point representation in scientific computing?
Long Questions
1. Explain how characters are encoded using Unicode. Provide examples of characters from different languages and their corresponding Unicode code points.
Unicode is an attempt at mapping all graphic characters used in any of the world’s writing system. Unlike ASCII, which is limited to 7bits and can represent only 128 characters, Unicode can represent over a million characters through different forms of encodings such as, UTF-8, UTF-16, and UTF-32. UTF is an acronym that stands for Unicode Transformation Format.
Example:
The letter ‘A’ is Unicode, represented as, U+0041 in UTF-8 , is 01000001 in the binary format and occupies 8 bits or 1 byte.
Let’s look at how Urdu letters are represented in UTF-8:
The Urdu letter ‘ب ‘is represented in Unicode as U+0628; its binary format is 11011000 10101000, means it takes 2 bytes.
2. Describe in detail how integers are stored in computer memory.
Integers extend the concept of whole numbers to include negative numbers. In computer programming, we call them signed integers. The set of integers is represented as:
Z = {…, -3, -2,-1,0,1,2,3,…}
To store both positive and negative values, one bit is reserved as the sign bit (the most significant bit). If the sign bit is ON(1), the value is negative; otherwise, it is positive. Using this system, the maximum positive value that can be stored in a 1 byte signed integer is (01111111) , which is 12710 . As the bits available to stored a value is n-1, hence the maximum value will be 2n-1-1. We can use this formula to compute the maximum values for 2 and 4 bytes.
Negative values are stored using two’s compliment, explained in the following section.
Negative Values and Two’s Complement
To store negative values, computers use a method called two’s complement. To find the two’s complement of a binary number, follow these steps:
1. Invert all the bits (change 0s to 1s and 1s to 0s).
2. Add 1 to the Least Significant Bit (LSB).
Example: Let’s convert the decimal number -5 to an 8-bit binary number:
1. Start with the binary representation of 5: 000001012.
2. Invert all the bits: 111110102.
3. Add 1: 111110102 + 12 = 111110112.
So, -5 in 8-bit two’s complement is 111110112.
Minimum Integer Value
For an 8-bit integer, we switch on the sign bit for the negative value and turn all bits ON. resulting in 111111112. Except the first bit, we take two’s complement 2 and get 10000000 which is 12810. Thus minimum value in 1-byte signed integer is -128, i.e.,-27. The minimum value is computed using the formula -2n-1 , where n is the total number of bits.
• 2-Byte Integer (16 bits): Minimum value = -215 = -32,768
• 4-Byte Integer (32 bits): Minimum value = -231 = -2,147,483,648
3. Explain the process of converting a decimal integer to its binary representation and vice versa. Include examples of both positive and negative integers.
Conversion from Binary to Decimal.
1. Identify each binary digit’s place value.
2. Multiply each bit by 2𝑛(where 𝑛 is the position index, starting from 0 at the right).
3. Sum up the results.
Example: Convert binary number 1010011 to decimal number.
1×26+0×25+1×24+0×23+0×22+1×21+1×20
=(1×64)+(0×32)+(1×16)+(0×8)+(0×4)+(1×2)+(1×1)
=64+0+16+0+0+2+1
=83
Example: Convert negative binary number 10101101 (two’s compliment) to decimal number.
Take two’s compliment = 01010010 +1
= 01010011
Now convert it into decimal number by above method. Answer will be 83.
Conversion from Decimal to Binary
The following algorithm translate a decimal number to binary.
1. To convert decimal number to binary form, divide the decimal number by 2.
2. Record the remainder.
3. Divide the number by 2 until the quotient which is left after division is 0.
4. Meaning it is represented by the remainders and it’s read from the bottom to
the top of the binary number.
Example: Convert 83 to binary
83 / 2 = 41 remainder 1
41 / 2 = 20 remainder 1
20 / 2 = 10 remainder 0
10 / 2 = 5 remainder 0
5 / 2 = 2 remainder 1
2 / 2 = 1 remainder 0
1/ 2 = 0 remainder 1
If the remainders are read from bottom to top then it gives the required result in binary, which is 1010011.
Example: Covert -32 (Negative Integer) into binary.
First convert 32 into binary then take its two’s compliment that will be equal to -32.
83 = 010100112 8-bit binary number
-83 = 10101100 + 1
= 10101101
4. Perform the following binary arithmetic operations:
a. Multiplication of 101 by 11.
Solution:
101 x 11 ---------- 101 101x ----------- 1111
b. Division of 1100 by 10.
Solution:
110 ______ 10)1100 10 ------- 10 10 ------- 0
Answer: 110
6. Add the following binary numbers:
a)
101 +110 ------------ 1011
b)
1100 +1011 ------------ 10111
7. Convert the following numbers to 4-bit binary and add them:
(a) 7 + (-4)
Solution:
7=0111
4=0100
By Two’s compliment method
-4=1011+1
=1100
now adding
0111 +1100 ---------- 10011
Discard the carry bit
Answer = 0011
(b) -5 + 3
Solution:
5=0101
By Two’s compliment method
-5=1010+1
=1011
and
3=0011
now adding
1011 +0011 _________ 1110
Answer = 1110
8. Solve the following
(a) 11012 – 01002
By Two’s compliment method
-0100 = 1011+1
=1100
now adding
1101 +1100 ---------- 11011
Discard the carry bit
Answer = 10112
(b) 10102 – 00112
By Two’s compliment method
-0011 = 1100+1
=1101
now adding
1010 +1101 ---------- 10111
Discard the carry bit
Answer = 01112
(c) 10002 – 01102
By Two’s compliment method
-0110 = 1001+1
=1010
now adding
1000 +1010 ---------- 10010
Discard the carry bit
Answer = 00112
(d) 11102 – 1002
By Two’s compliment method
-0100 = 1011+1
=1100
now adding
1110 +1100 ---------- 11010
Discard the carry bit
Answer = 10102