






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 12
This page cannot be seen from the preview
Don't miss anything!







1
-^ 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
-^ 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
-^ Consider this program:^ #include
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
-^ On the stack, an appropriate number of bytes are allocated to eachvariable^ #include
b=207500;unsigned long^ c=299792458;printf(“%u\n”, (a + b + c) );return 0;}
7
-^ 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
-^ We have said^
-^ 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
Adeel Israr | COMSATS Institute of Information Technology, Islamabad
conversion^ of^ â299792458â
from^ 'int'^ to
'short unsigned int' inside { } [-Wnarrowing]unsigned short c=299792458;
^
13
Smallest:^0 Largest:^65535 Smallest minus 1: 65535Largest plus 1:
Adeel Israr | COMSATS Institute of Information Technology, Islamabad
15
-^ 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^
unsigned short^
unsigned int^
32 – 1^ 0, …, 4.3 million unsigned long^
64 – 1^ 0, …, 1.8 trillion
Adeel Israr | COMSATS Institute of Information Technology, Islamabad
-^ 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
-^ Recall from secondary school scientific notation that allows us towrite numbers clearly and succinctly:^ Conventional notation
Scientific notation
0
Adeel Israr | COMSATS Institute of Information Technology, Islamabad
-^ The number of decimal digits used is the precision:
Scientific notation
Precision –11^ 6.67408 × 10
21
-^ Without^ going
Real WorldExample^ Ruler^ Screw gauge Data type
Approximatemaximum precision(decimal digits) float^
double^
Adeel Israr | COMSATS Institute of Information Technology, Islamabad
-^ 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