Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Data Types: Integers, Floats, and Base 2 Storage, Study notes of Programming Paradigms

How different data types, including integers and floats, are stored in binary format using the c compiler. It covers char, int, long int, float, and double precision types, discussing their bit representations and the range of values they can hold. The document also touches upon the limitations of precision and the importance of understanding base 2 for effective communication with other intelligent life forms.

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

eekanath
eekanath 🇺🇸

4.7

(18)

18 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download Understanding Data Types: Integers, Floats, and Base 2 Storage and more Study notes Programming Paradigms in PDF only on Docsity! Data Types and Representations Since we will be performing numerical calculations using the C compiler, it is useful to know how the different data types are stored. Because we have 10 fingers and 10 toes, we have been brought up expressing integers and real numbers in base 10. However, the computer doesn’t have fingers or toes. The easiest method of arith- metic operations for computers is in binary, base 2. We will represent integers and real numbers in the following notation: the number in parenthesis with the base as a subscript: (number)base or (yyy.xxxx)base. The main data types that we will be using will be integers, long integers, float, and double precision. char types Computers do numerical operations and store data in base 2. The smallest data type is a byte, which is 8 bits. Each bit can be a 0 or 1. The simplest data type is an unsigned char. The unsigned char type is one byte long, and the number it repre- sents are just the 8 bits in base 2. For example, (00000000)2 is zero, and (11111111)2 in base 2 is (255)10 in base 10. So the unsigned char is an integer between 0 and (255)10. The next simplest type is the char. The first bit represents the sign of the integer, with 0 being +, and 1 being -. The next 7 bits are the integer in base 2. The smallest integer is 11111111 which is (−12710 in base 10. The largest integer is 01111111, which is (+127)10 in base 10. So the char type is an integer between (−127)10 and (+127)10. int type The int type is stored in four bytes, or 32 bits. The first bit represents the sign of the integer, with 0 being + and 1 being -. The next 31 bits are the integer in base 2. The largest integer is 011 · · · 11 which is +231 − 1 = +(2147483647)10. The most negative integer is −(2147483648)10. So the int type between −(2147483648)10 and +(2147483647)10. The long int type also stores integers in four bytes, or 32 bits. Storage is similar 1 to the int type with the first bit representing the sign. The most negative integer is therefore 111 · · · 11 which is −(231 − 1) = −(2147483647)10. The most positive integer is 0111 · · · 111 = +(2147483647)10. So the long int type is an integer between −(2147483648)10 and +(2147483647)10. float type We are used to expessing real numbers in decimal notation: (0.1)10 = 1/10 = 10−1, (0.01)10 = 1/100 = 10−2, etc. We are not as familiar expressing real numbers in base 2. Using the same idea as in base 10, (0.1)2 = 1/2 = 2−1, (0.01)2 = 1/4 = 2−2. In general, real numbers can be expressed in base 2 notation as: (x2x1x0.x−1x−2 · · ·)2 = x22 2 + x12 1 + x0 + x −12 −1 + x −22 −2 · · · (1) Now we are ready to understand how the computer will store real numbers. The float data type uses 4 bytes, or 32 bits, to store a real number. The convention that has been chosen is to have the first bit represent the sign of the number, the next 8 bits represent the exponent in base 2, and the last 23 bits the mantisa of the number in base 2. More specifically: sc1c2 · · · c8f1f2 · · · f23 → (−1)s(1.f)2 2c−127 (2) where s, the ci, and the fi correspond to the ”zero” or ”one” value of the different 32 bits that make up the four bytes. Using this convention, we see that the largest value for the float type is when c = (11111111)2 = (255)10. This value for c yields an exponent of 2255−127 = 2128 ≈ 1038. So the largest real number that can be expressed in the float type is around 1038. Similarly, the most negative real number that can be expressed is around −1038. Between these two limits, there are only 231 − 1 ≈ 2 × 109 possible float numbers. The smallest non-zero number is also determined by the exponent c. If c = 0, then the exponent is 2−127 ≈ 6 × 10−39. Although the smalest float type number is 10−39, this doesn’t mean that we have precision to 39 significant figures. The number of significant figures is determined by the 23 bits in the mantissa (f) region. The number of digits after the decimal point 2
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved