Binary Representations and Storage of Integers and Floating-Point Numbers, Lecture notes of Computer science

The binary representation and storage of integers and floating-point numbers in computers. It covers the concept of unsigned and signed integers, the number of bytes and bits they occupy, and their approximate ranges. Additionally, it explains the scientific notation representation of floating-point numbers and their approximate maximum precision.

Typology: Lecture notes

2018/2019

Uploaded on 04/03/2019

mobeen_001
mobeen_001 🇵🇰

1 document

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1Adeel Israr | COMSATS Institute of Information Technology, Islamabad
Binary representations
We have already described binary numbers
On the computer, all integers are stored in binary
Thus, to store each of these numbers, we must store the
corresponding binary digits (bits):
3 11 2
42 101010 6
616 1001101000 10
299792458 10001110111100111100001001010 29
To store a googol (10
100
), we must store 333 bits:
10010010010011010110100100101100101001100001101111100111…
01011000010110010011110000100110001001100111000001011111…
10011100010101100111001000000100011100010000100011010011…
11100101010101011001001000011000010001010100000101110100…
01111000100000000000000000000000000000000000000000000000…
00000000000000000000000000000000000000000000000000000
2Adeel Israr | COMSATS Institute of Information Technology, Islamabad
Storage
Do we store as many bits as are necessary?
You could, but this would be exceedingly difficult to manage
Instead, each primitive data type has a fixed amount of storage
8 bits are defined as 1 byte
All data types are an integral number of bytes
Usually 1, 2, 4, 8or 16 bytes
Because we use binary, powers of 2 are very common:
Exponent Decimal Binary
2
0
11
2
1
210
2
2
4100
2
3
81000
2
4
16 10000
2
5
32 100000
2
6
64 1000000
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Binary Representations and Storage of Integers and Floating-Point Numbers and more Lecture notes Computer science in PDF only on Docsity!

1

Binary representations

-^ We have already described binary numbers^ –^ On the computer, all integers are stored in binary^ –^ Thus,^ to^

store^ each^ of^ these

numbers,^ we^

must^ store^ the corresponding binary digits (bits):^3

-^ To store a googol (

10010 ), we must store

333 bits: 10010010010011010110100100101100101001100001101111100111…01011000010110010011110000100110001001100111000001011111…10011100010101100111001000000100011100010000100011010011…11100101010101011001001000011000010001010100000101110100…01111000100000000000000000000000000000000000000000000000… 00000000000000000000000000000000000000000000000000000

Adeel Israr | COMSATS Institute of Information Technology, Islamabad

Storage

-^ Do we store as many bits as are necessary?^ –^ You could, but this would be exceedingly difficult to manage •^ Instead, each primitive data type has a fixed amount of storage^ –^ 8 bits are defined as 1 byte^ –^ All data types are an integral number of bytes^ •^ Usually

1 ,^2 ,^4 ,^8 or^16 bytes • Because we use binary, powers of 2 are very common: Exponent^ Decimal

Binary 0 21

3

Example

-^ Consider this program:^ #include // Function declarationsint main();// Function definitionsint main() {unsigned short a=42;unsigned int

b=207500;unsigned long^ c=299792458;printf(“%u\n”, (a + b + c) );return 0;}

Output:^300000000 Note:First,^ a^ is upcast to

unsigned int before to the first additionThen, the result is upcast to unsigned long^ before the second addition

Adeel Israr | COMSATS Institute of Information Technology, Islamabad

Example

-^ On the stack, an appropriate number of bytes are allocated to eachvariable^ #include // Function declarationsint main();// Function definitionsint main() {unsigned short a=42;unsigned int

b=207500;unsigned long^ c=299792458;printf(“%u\n”, (a + b + c) );return 0;}

7

Wasted space?

-^ If an integer does not use all the bytes, the remaining bits are never-the-less allocated until the variable goes out of scope^ –^ In general-purpose computing, this is often not a problem^ –^ This is a critical issue, however, in embedded systems^ •^ More memory:^ –^ Costs more^ –^ Uses more power^ –^ Produces more heat

Determining the Adeel Israr | COMSATS Institute of Information Technology, Islamabad

size^ of a type

-^ We have said^

short,^ int^ and^ long

are 2, 4 and 8 bytes

-^ This is true on most every general-purpose computer • Unfortunately, the C++ specification doesn’t require this –^ Fortunately, the

sizeof^ operator gives you this information #include <stdio.h>int main();int main() { printf("An 'unsigned short' occupies %d bytes\n”, sizeof ( unsigned short );printf("An 'unsigned int' occupies %d bytes\n", sizeof ( unsigned int );printf("An 'unsigned long'^ occupies %d bytes\n" sizeof ( unsigned long ); return 0;}^ Output on :^ An 'unsigned short' occupies 2 bytesAn 'unsigned int'

occupies 4 bytes An 'unsigned long'

occupies 8 bytes

9

Memory and initial values • Question: – What happens if the initial value cannot be stored? #include <stdio.h>int main();int main() {unsigned short c=299792458;printf("The speed of light is %ud m/s\n", c);return 0;}

Adeel Israr | COMSATS Institute of Information Technology, Islamabad

Memory and initial values • Fortunately, you get a warning: example.cpp: In function 'int main()':example.cpp:6:31:^ warning:^ narrowing

conversion^ of^ â299792458â

from^ 'int'^ to

'short unsigned int' inside { } [-Wnarrowing]unsigned short c=299792458;

^

warning: large integer implicitly truncated to unsignedtype [-Woverflow] • It still compiles and executes:^ The speed of light is 30794 m/s.

13

Memory and arithmetic • What happens if the sum, difference or product of two integersexceeds what can be stored? #include <stdio.h>int main() {unsigned short smallest=0, largest=65535;printf("Smallest:\t%u\n",smallest);printf("Largest:\t%u\n",largest);smallest--;largest++;printf("Smallest:\t%u\n",smallest);printf("Largest:\t%u\n",largest);return 0;} Output:

Smallest:^0 Largest:^65535 Smallest minus 1: 65535Largest plus 1:

Adeel Israr | COMSATS Institute of Information Technology, Islamabad

Memory and arithmetic • Important: All unsigned integers arithmetic is performed:^16 modulo^2 for^ unsigned short^32 modulo^2 for^ unsigned int^64 modulo^2 for^ unsigned long • This is similar to all clock arithmetic being performed modulo 12

15

Summary so far

-^ We have the following:^ –^ Unsigned integers are stored as either 1, 2, 4 or 8 bytes^ –^ The value is stored in the binary representation^ Type^^ –^ You should not memorize the exact ranges

Bytes^ Bits^ Range

Approximate Range unsigned char^

8 – 1^ 0, …, 255

unsigned short^

16 – 1^ 0, …, 65535

unsigned int^

32 – 1^ 0, …, 4.3 million unsigned long^

64 – 1^ 0, …, 1.8 trillion

Adeel Israr | COMSATS Institute of Information Technology, Islamabad

Signed types

-^ A better solution: •^ Note that^ –1 + 1 = 0, but also 11 + 1 = 0–5 + 2 = –3, but also 7 + 2 = 9, which we are equating to –

19

Scientific notation

-^ Recall from secondary school scientific notation that allows us towrite numbers clearly and succinctly:^ Conventional notation

Scientific notation

–116.67408 × 10

8 2.99792458 × 10

0.0^000000000000000000000000

6.626070040 × 10

1.6021766208 × 10

  • 8.^

0 8.3144598 × 10

3.14159265358979323 × 10

0

6.67408^ ×^10

Mantissa

Exponent

Base

Adeel Israr | COMSATS Institute of Information Technology, Islamabad

Scientific notation

-^ The number of decimal digits used is the precision:

Scientific notation

Precision –11^ 6.67408 × 10

8 2.99792458 × 10

–346.626070040 × 10

–191.6021766208 × 10

0 8.3144598 × 10

3.14159265358979323 × 10

21

Scientific notation

-^ Without^ going

into^ detail,^ each

data^ type^ has^

an^ approximate

maximum precision it can store • There is generally only one situation where

float^ has acceptable

precision for engineering applications:^ –^ Computer graphics

Real WorldExample^ Ruler^ Screw gauge Data type

Approximatemaximum precision(decimal digits) float^

double^

Adeel Israr | COMSATS Institute of Information Technology, Islamabad

Scientific notation

-^ How could you store a floating-point number?^ –^ Store the exponent and mantissa separately, and assume a decimalpoint comes after the first digit^ Scientific notation

**Representation

float**^

double –11^ 6.67408 × 10

-11 6674080^

8 2.99792458 × 10

+08 2997925^

–34 6.626070040 × 10

–19 1.6021766208 × 10

0 8.3144598 × 10

+00 8314460^

+000 +3141592653589793 * In reality, these are stored in binary