This is the second lesson of Computer and Network Technology subject of BCS HEQ Certificate Level exam.
Subject: Computer and Network Technology (CNT)
Chapter: Fundamentals
Lesson: Data Representation in Computers
This lesson discuss about how integers, floating point numbers and characters are ha...
This is the second lesson of Computer and Network Technology subject of BCS HEQ Certificate Level exam.
Subject: Computer and Network Technology (CNT)
Chapter: Fundamentals
Lesson: Data Representation in Computers
This lesson discuss about how integers, floating point numbers and characters are handled by modern computers.
For more lessons please visit https://www.bcsonlinelectures.com website.
Size: 543.29 KB
Language: en
Added: Oct 17, 2018
Slides: 29 pages
Slide Content
Computer and Network Technology (CNT) Chapter: Fundamentals Lesson: Data Representation in Computers Representation of Integers Lecturer: Susantha Herath PGD in IT (MBCS), PGD in Marketing (Uni. Of Kln ) Lecture 02 bcsonlinelectures.com
Few basic things to know Most Significant B it (MSB) also called as Most Significant Digit (MSD). This is indeed the first digit (from left) in a binary number. More accurately the digit with the highest radix (base) value. Least Significant Bit (LSB) or Least Significant Digit (LSD). This is the digit of a binary number with the least radix value, probably the last digit. In binary number one digit is called as a bit. Computer memory is a place that store data and it has a fixed length of bits. Generally 8bit, 16bit, 32bit and 64bit in length. 1001100100111 2 12 (MSD) 2 (LSD)
Data Representation Computer uses a fixed number of bits to represent a single piece of data. Generally computers use 8bit, 16bit, 32bit and 64bit in size of binary numbers to represent different type and size of data. N-number of bit memory (location) can store 2 n of different types of values. For an example 8bit memory can store 256 of different data values. 3bit memory can store 8 distinct values (patterns) 000, 001, 010, 011, 100, 101, 110, or 111 When we interpret these values, we need to know exactly what represent with these values. If we know that these represent decimal numbers, then we know it is from 0-7 then it is a different value. So this interpretation of a binary pattern called data representation or encoding.
Integer Representation Integers are whole numbers or fixed point numbers. The radix point is fixed after the LSB. Example: 3774 10 is a fixed point number (integer) 2949.94 10 is a floating point number (real number) 54533. 4573.55 LSB Radix Point Fixed Radix Point Moved from LSB
Integer Representation In a computer integers and floating point numbers are treated differently and processed separately. Floating point numbers are processed in a separate processor called floating point processor. Computers use fixed number of bits to represent integers. Commonly use bit lengths are 8bit, 16bit, 32bit and 64bit. There are two types of integers Unsigned integers: can represent zero and positive numbers only Signed integers: can represent zero, positive and negative numbers. There are three schemas (ways) to represent signed integers. Sign Magnitude representation 1’s complement representation 2’s complement representation
Integer Representation We learned about two different types of Integers; Unsigned Integers – that can represent only zero and positive numbers. The value of an unsigned integer interpreted as the “magnitude of underlying binary pattern or the normal way we convert binary to decimal. Example: 8bit binary number 0100 0010B (B = Binary) 0100 0010B = 2x2 1 + 2x2 6 = 4+64 = 68D (D = Decimal) 8bit = 0000 0000 16bit = 0000 0000 0000 0000 32bit = 0000 0000 0000 0000 0000 0000 0000 0000 64bit = 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 All above numbers represent zero in different bit systems. The bit length of each system is different.
n-bit Unsigned Integers An n-bit pattern can represent 2 n distinct integers. This range from 0 to (2 n )-1 n-bit Minimum 2 n – 1 Maximum 8bit 2 8 – 1 255 16bit 2 16 – 1 65,535 32bit 2 32 – 1 4,294,967,295 64bit 2 64 – 1 18,446,744,073,709,551,615 When you are programming you need to select the correct and appropriate bit size to store integers. If you want to store small numbers less than 255, you can select 8bit number, selecting 32bit length to store small numbers is a waste of resources.
Signed Integers Signed integers can represent zero, positive and negative numbers. The MSB (Most Significant Bit) – Normally the first bit from left, represent the sign of the integer. Zero (0) to represent positive (+) sign and One (1) to represent negative (-) sign. The magnitude of the integer represent differently in different schemas. There are three schemas to represent signed integers. Sign Magnitude Representation 1’s Complement Representation 2’s Complement Representation Example of Signed Integers: +2344 is a positive integer -23434 is a negative integer
n-bit Sign Magnitude Representation Most Significant Bit (MSB) is the sign bit. 0 representing positive and 1 representing negative signs. The remaining (n-1) bits represent the magnitude / absolute value of the integer interpreted according to its binary pattern. Examples: n=8 (8bit number) and binary number is 01000001 Sign bit (MSB) is 0 this is a positive number Absolute value 1000001 65 01000001B = +65D B = Binary, D = Decimal
n-bit Sign Magnitude Representation Examples: n=8 (8bit number) and binary number is 00000000 Sign bit (MSB) is 0 this is a positive number Absolute value 0000000 0 00000000B = +0D n=8 (8bit number) and binary number is 10000000 Sign bit (MSB) is 1 this is a negative number Absolute value 0000000 0 10000000B = -0D
n-bit Sign Magnitude Representation Problems (Drawbacks / Limitations) of Sign Magnitude Representation: There are two representation for the number zero Even the number zero need to have a sign, since it is not necessary nor accurate and may leads to confusions. Positive and negative numbers need to process separately.
n-bit 1’s Complement Representation In this representation, the MSB is the sign bit 0 represent positive and 1 represent negative numbers. The remaining n-1 bits represent the magnitude (value) of the integer. In positive integers, the value of the integer is the binary pattern (value) of the n-1 bit magnitude. Same as Sign Magnitude Representation. For negative integers, the absolute value is the n-1 binary pattern value of the inverse magnitude (complement). Example: 1 000 0001B (sign bit = 1, negative integer) Complement of 000 0001 is 111 1110 = 126D 1 000 0001B = -126D
n-bit 1’s Complement Representation In the 1’s Complement Representation, there are two different representation for zero; 0000 0000B and 1111 1111B hence +0 and -0 Since the processing method of positive and negative integers are different, computer need to process the positive and negative numbers separately.
n-bit 2 ’s Complement Representation In 2’s Complement Representation also the MSB is the sign bit 0 represent positive and 1 represent negative integers. For positive integers, the value of the integer equal to the magnitude of n-1 binary pattern For negative integers, the absolute value of the integer equal to the magnitude of the complement of n-1 binary pattern plus one. Example: 1 000 0001B (MSB = 1 is a negative number) Complement of 000 0001 = 111 1110 Let’s add 1 to the complement = 111 1111 = 127 1 000 0001B = -127D
n-bit 2 ’s Complement Representation Advantages of 2’s Complement Representation There is only one representation for the number 0 In binary addition and subtraction, both positive and negative numbers can treated together (process together) Subtraction can use the addition logic Computers use 2’s Complement Representation for processing integers.
n-bit 2 ’s Complement Representation Addition, Subtraction, Overflow and Underflow Addition of positive integers Example: n=8, 65D + 5D = 70D 65D 0100 0001B 5D 0000 0101B 65D + 5D 0100 0110B OK 70D within the range of -128 to +127 Subtraction is the addition of positive and negative integers Example: n=8, 65D – 5D = 60D 65D + (-5D) 65D 0100 0001B -5D 1111 1011B 65D + (-5D) 0011 1 100B 60D OK within the range of -128 to +127
n-bit 2 ’s Complement Representation Addition, Subtraction, Overflow and Underflow Overflow and underflow happens when the result is not within the acceptable range Example of Overflow: n=8, 127D + D = 129D (not within -128 and +127, exceed maximum value) 127D 0111 1111B 2 D 0000 0010B 127D + 2D 1000 0001B -127D wrong answer Example of underflow: n=8, -125D – 5D = -130D (not within -128 and +127, below the minimum value) -125D 1000 0011B -5D 1111 1011B -125D + (-5D) 0111 1110B +126D wrong answer
n-bit 2 ’s Complement Representation Range of n-bit 2’s complement signed integers An n-bit 2’s complement integer can represent integers from -2 (n-1) to +2 (n-1) -1. This can represent all the integers within the range without missing any intergers . 8bit from -128 to +127 16bit from -32,768 to +32,767 32bit from -2,147,483,648 to + 2,147,483,647 64bit from -9,223,372,036,854,775,888 to + 9,223,372,036,854,775,887
n-bit 2 ’s Complement Representation decoding 2’s complement numbers Check the sign bit first. If sign bit = 0, the number is positive and the absolute value if the binary value of remaining n-1 integers. If the sign bit = 1, the integer is negative. To get the absolute value; Invert remaining n-1 bits and add 1 or Scan from left until the first occurrence of 1. Flip all values from that point to the left. Binary value of this pattern gives the absolute value of the negative integer. Example: N=8 bit pattern = 1 100 0100 B Negative number Scan from right, until the first occurrence of 1, flip all values from that point to left 011 1 100 B = 60 D Represented negative value is -60 D
Big Endian vs. Little Endian Modern computers store one byte of memory in single memory location / address. 1 byte = 8 bits. Therefore in 32bit integer stores in 4 memory locations. Term “Endian” means the order of storing bytes in memory. In Big Endian standard – the most significant byte stores first in the lowest memory location. In Little Endian standard, the least significant byte (LSB) stores in the lowest memory location.
Floating Point Number Representation A floating point number (also called as a real number) is a number with a decimal point. A floating point number can represent a very large positive or negative number as well as a very small negative or positive number. A real number has a range of -1.23x10 88 to 1.25x10 88 including zero. Scientifically a floating point number represent as a number with a fraction and exponent of a certain radix. F x r E
Floating Point Number Representation Same floating point number can represent in different ways. Consider 5.456D We can represent this number in following ways: 54.532 x 10 5.4532 x 10 1 0.54532 x 10 2 and so on…. Since there are many ways to represent the same floating point number, we need to define a standard, so every floating point number should be in this standard. To make it standard, we can normalized the fraction part. In normalized form, there is only single none zero digit represent before the radix point. In above example 5.4532 x 10 1 is the normalized form of 54.532 number.
Floating Point Number Representation In computers, there is an issue of “Loss of Precision” in storing floating point numbers. An n-bit system can only stores 2 n distinct finite numbers. Even within a small range 0.00 – 0.01, there are infinite number of decimal numbers. But the number of bits in a computer system is limited, only a the approximate number used, resulting in loss of accuracy. Processing of a real number (floating number arithmetic) is very much less efficient and slower than processing of integers. In computers, real numbers uses fraction (F) and exponent (E) with radix (r) of 2. Both F and E can be negative or positive. In modern computing uses IEEE 754 standard to represent floating point numbers. There are two representation schemes 32 bit single precision and 64 bit double precision
IEEE-754 32-bit Single-Precision Floating-Point Numbers In 32-bit single precision floating point number representation, decimal number represent in following structure: The most significant bit is the sign bit. Zero for positive numbers and One for negative numbers. Next following 8bit represent the exponent (E) Remaining 23bit represent the fraction (F) You may wonder why radix (r) is not represent here. Because radix is always 2 for binary numbers (in computers).
IEEE-754 64-bit Double-Precision Floating-Point Numbers 64-bit double precision floating point number representation is similar to 32-bit single precision floating point number representation. Only difference is assignment of bit range for exponent and fraction. The most significant bit is the sign bit. Zero for positive numbers and One for negative numbers. Next following 11-bit represent the exponent (E) Remaining 52-bit represent the fraction (F)
Character Encoding In computer memory, characters are represented (encode) using character schemes called as “character set”, “charset”, “character map” or “code page”. There are several standard schemes such as ASCII, EBCDIC, UTF, etc. 7-bit ASCII Code ( aka US-ASCII, ISO/IEC 646, ITU-T T.50 ) ASCII (American Standard Code for Information Interchange) is one of the earlier character coding schemes. ASCII is originally a 7-bit code. It has been extended to 8-bit to better utilize the 8-bit computer memory organization. (The 8th-bit was originally used for parity check in the early computers). This is called ASCII-8 Code. EBCDIC Code This is stands for “Extended Binary Code Decimal Interchange Code”. This is a 8-bit code scheme without parity. With this up to 256 characters can be coded.
Character Encoding Unicode (aka ISO/IEC 10646 Universal Character Set ) Before Unicode, no single character encoding scheme could represent characters in all languages . Unicode aims to provide a standard character encoding scheme, which is universal, efficient, uniform and unambiguous. Unicode standard is maintained by a non-profit organization called the Unicode Consortium (@ www.unicode.org ). Unicode is an ISO/IEC standard 10646. Unicode is backward compatible with the 7-bit US-ASCII and 8-bit Latin-1 (ISO-8859-1). That is, the first 128 characters are the same as US-ASCII; and the first 256 characters are the same as Latin-1. Unicode originally uses 16 bits , which can represent up to 65,536 characters.
Summary Inside the computer memory – integer 1, floating point 1.0, character 1 and string 1 are totally different. To write good and high performance programs, you need to know these differences and use correct data type to store data in memory and later on in databases as well. In 8-bit signed integer , integer number 1 is represented as 00000001B. In 8-bit unsigned integer , integer number 1 is represented as 00000001B. In 16-bit signed integer , integer number 1 is represented as 00000000 00000001B. In 32-bit signed integer , integer number 1 is represented as 00000000 00000000 00000000 00000001B. In 32-bit floating-point representation , number 1.0 is represented as 0 01111111 0000000 00000000 00000000B, i.e., S=0, E=127, F=0. In 64-bit floating-point representation , number 1.0 is represented as 0 01111111111 0000 00000000 00000000 00000000 00000000 00000000 00000000B, i.e., S=0, E=1023, F=0 .