data representation overflow limits no good explanations in the books try to understand in class and...

63
Data R Data R epresentation epresentation Overflow Overflow Limits Limits No good explanations in the books No good explanations in the books Try to understand in class and from the Try to understand in class and from the slides slides

Upload: brook-tucker

Post on 27-Dec-2015

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Data RData RepresentationepresentationOverflowOverflow

LimitsLimits

No good explanations in the booksNo good explanations in the books

Try to understand in class and from the slidesTry to understand in class and from the slides

Page 2: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Representation of DataRepresentation of Data

All data in a the computer’s memory and files are represented as a sequence of bits

Bit : unit of storage, represents the level of an electrical charge. Can be either 0 or 1

Byte: another unit of storage that occupies 8 bits.

A bit sequence can represent many different things:– We will see that a bit string (such as 10000000111000001011111110100010)

can mean several different things depending on the representation that is agreed upon.

So, how should we represent integers, characters, real numbers, strings, structures, in terms of bits?– Representations must be efficent and convenient

– We will see some of them

Page 3: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

CharactersCharacters

In C/C++, characters are actually integers of length one byte, with special meaning as characters

- the ASCII mapping

char c = 'a'; //stores the code corresponding to letter ‘a’, but

//prints the character a when printed

ASCII Standard– American Standard Code for Information Interchange– dates back 1960's– 256 different codes (0 . . . 255) and corresponding characters– The characters with codes 0 . . 31, and 127 are control characters

• At these times, standardizing communication related and telegraphic codes was important. That is why most of the control characters are for this purpose and now obselete. Though, some OSs implement some of the control chars.

– Extended ASCII (128 – 255): there are different conventions and interpretations

Page 4: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

ASCIIASCII

| 0 NUL| 1 SOH| 2 STX| 3 ETX| 4 EOT| 5 ENQ| 6 ACK| 7 BEL|| 8 BS | 9 HT | 10 LF | 11 VT | 12 FF | 13 CR | 14 SO | 15 SI || 16 DLE| 17 DC1| 18 DC2| 19 DC3| 20 DC4| 21 NAK| 22 SYN| 23 ETB|| 24 CAN| 25 EM | 26 SUB| 27 ESC| 28 FS | 29 GS | 30 RS | 31 US || 32 SP | 33 ! | 34 " | 35 # | 36 $ | 37 % | 38 & | 39 ' || 40 ( | 41 ) | 42 * | 43 + | 44 , | 45 - | 46 . | 47 / || 48 0 | 49 1 | 50 2 | 51 3 | 52 4 | 53 5 | 54 6 | 55 7 || 56 8 | 57 9 | 58 : | 59 ; | 60 < | 61 = | 62 > | 63 ? || 64 @ | 65 A | 66 B | 67 C | 68 D | 69 E | 70 F | 71 G || 72 H | 73 I | 74 J | 75 K | 76 L | 77 M | 78 N | 79 O || 80 P | 81 Q | 82 R | 83 S | 84 T | 85 U | 86 V | 87 W || 88 X | 89 Y | 90 Z | 91 [ | 92 \ | 93 ] | 94 ^ | 95 _ || 96 ` | 97 a | 98 b | 99 c |100 d |101 e |102 f |103 g ||104 h |105 i |106 j |107 k |108 l |109 m |110 n |111 o ||112 p |113 q |114 r |115 s |116 t |117 u |118 v |119 w ||120 x |121 y |122 z |123 { |124 | |125 } |126 ~ |127 DEL|

Special control characters.Most of them are now obselete. Some OSs implement some of them and meanings may change from OS to OS

These are the ASCII codes – of course you are not expected to memorize them, just know that there are codes for special characters and that numbers and letters of a case are consecutive (so one can do '1'+5 to get code of '6', or

subtract 32 to go from lowercase to get corresponding uppercase)

See http://www.asciitable.com/ and See http://www.asciitable.com/ and http://en.wikipedia.org/wiki/ASCII for more infohttp://en.wikipedia.org/wiki/ASCII for more info

Blue control characters are

the ones important for

Windows/DOS

Page 5: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Integer Number RepresentationInteger Number Representation

Sign-Magnitude Representation

1s complement Representation

2s complement Representation

Comparison of different representations

Page 6: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

NumberNumber Representation Representation

Fundamental problem:– Fixed-size representation (e.g. 4 bytes for integers) can’t encode all numbers

– Usually sufficient in most applications, – But a potential source of bugs: overflow

– need to be careful of it

Other problems:– How to represent negative numbers, floating points?

– Historically, many different representations.

– How to do subtraction effectively?

Page 7: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Base 2 – Base 2 – unsignedunsigned numbers numbers

0 0 0 0 0 0 0 0 0 //8-bit binary representation of positive integers

0 0 0 0 0 0 0 1 1

0 0 0 0 0 0 1 0 2

0 0 0 0 0 0 1 1 3

...

1 1 1 1 1 1 1 1 255

– Representation: an n-bit number in base has decimal value =• di is the coefficient of the ith bit.

• Bit 0 is the LSB and bit n-1 is the MSB.

– Example for base 2 (binary): 10112 = 1 x 20 + 1 x 21 + 0 x 22 + 1 x 23 = 1110

1

0

ni

ii

d

LSB – Least Significant Bit

MSB – Most Significant Bit

Page 8: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Sign/Magnitude representationSign/Magnitude representation (also called “signed representation”)(also called “signed representation”)

– use one of the bits (the first bit = Most Significant Bit) as a sign bit.

– use the rest for magnitude

– e.g.000 = +0001 = +1010 = +2 positive numbers011 = +3100 = -0101 = -1110 = -2 negative numbers111 = -3

– range: -(2 (n-1)-1) to (2 (n-1) -1), where n is the total number of bits For n = 4, [ -(23-1) . . . 23-1 ] [ -7 . . . 7 ]

Page 9: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Alternative representationAlternative representationss

Most computers don’t use a “sign and magnitude” representation– Drawbacks of the Sign-Magnitude representation:

1. two 0s: one positive one negative

2. addition and subtraction involving negative numbers are complicated

Alternatives?– 1's complement representation

– 2's complement representation: today's standard

These two representations seem very similar in approach, but they differ in:– Representation of negative numbers (positives are the same in all 3

representations) and

– Ease of arithmetic operations involving negative numbers

Page 10: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Signed numbers: Signed numbers: 1’s complemen1’s complementt– Positive numbers: first bit is 0, and the rest is the binary equivalent of the

number.

– Negative numbers: represented by the 1’s complement of the corresponding positive number

• 1’s complement: invert all the bits (0's become 1; 1's become 0)

e.g +8 = 0000 1000 (0 for + sign, and 000 1000 for 8)

- 8 = 1111 0111

– So, effectively the first bit is used for sign, but negative numbers show a distinction from those of the sign-magnitude representation.

– How about 0?

Page 11: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Number 1’s-complement+7 0111+6 0110 +5 0101+4 0100+3 0011+2 0010+1 0001+0 0000 -0 1111-1 1110-2 1101-3 1100 -4 1011-5 1010-6 1001-7 1000

As in the signed representation, there is a + and - 0

Range: [-(2n-1-1) . . 2n-1-1] For n = 4, [-(23-1) . . . 23-1 ] [-7 . . . 7]

Page 12: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Signed Signed numbers: numbers: 2’s complement2’s complementSigned 2’s complement is the common representation for signed numbers used in computers

– For positive numbers, use 0 first and the remaining bits are the binary equivalent of the magnitude.

– Negative numbers are represented by the 2’s complement of the corresponding positive number.

• 2s complement: invert all bits and add 1

• Alternative (easier) method: copy all the bits from right to left until and

including the first 1, invert the rest)

Ex : +20 = 0001 0100

-20 = 1110 1100

– single 0– addition and subtraction complexities simplified– note the range (one more negative as compared to 1's complement): -2 (n-1) ... (2 (n-1) -1)– current standard for representing signed integers

Range: [-2n-1 . . . 2n-1-1]For n = 4, [-23 . . . 23-1 ] [-8 . . . 7 ]

Page 13: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Number 2’s-complement+7 0111+6 0110 +5 0101+4 0100+3 0011+2 0010+1 0001 0 0000 -1 1111 -2 1110 -3 1101 -4 1100 -5 1011 -6 1010 -7 1001 -8 1000

There is only one zero

There is one more negative numberas compared to positives

Page 14: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Sign Magnitude: One's Complement Two's Complement

000 = +0 000 = +0 000 = +0001 = +1 001 = +1 001 = +1010 = +2 010 = +2 010 = +2011 = +3 011 = +3 011 = +3100 = -0 100 = -3 100 = -4101 = -1 101 = -2 101 = -3110 = -2 110 = -1 110 = -2111 = -3 111 = -0 111 = -1

Notice: Positive numbers are represented the same way (same bit strings) in all representations! So for all three representations, representation of a positive number is directly decimal to binary / binary to decimal conversion.

Possible RepresentationsPossible Representations: : summarysummary

Page 15: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Decimal Conversion for NegativesDecimal Conversion for Negatives

If you are given a bit string representing a nagative number, you can find the decimal equivalent depending on the number representation used.

– if sign/magnitude representation is used:• If MSB is 1, that means the number is negative• but this bit has no contribution to the magnitude. Convert the remaining bits to decimal

for the magnitude.• For example, 10010010 is equivalent to –18 (- (1x16 + 1x2))

– if 1s complement representation is used:• If MSB is 1, that means the number is negative• To find the magnitude:

– invert all bits (i.e. negate): 10010010 => 01101101– find the positive number corresponding to the negated string

» 1x64 + 1x32 + 1x8 + 1x4 + 1 = 109– 10010010 is equivalent to –109– note that this is the reverse operation of what we would do if we wanted to find

the bit representation of –109 (find the bit rep. of 109, take 1s complement)

Page 16: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Decimal Conversion for Negatives– ctd.Decimal Conversion for Negatives– ctd.

– if 2s complement representation is used:• If MSB is 1, that means the number is negative.• To find the magnitude:

– invert all bits 1001 0010 => 0110 1101– add 1 => 0110 1110– this is the negated value– find the positive number corresponding to the negated string (01101110)

» 1x64 + 1x32 + 1x8 + 1x4 + 1x2 = 110– 10010010 is equivalent to –110

– note that this is the reverse operation of what we would do if we wanted to find the bit representation of –110 (find the bit rep. of 110, take 2s complement)

Page 17: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Alternative decimal conversion – Alternative decimal conversion – 2s comp.2s comp.

Hence: 100100102 = -1x27 + 1x24 + 1x21 = -11010

-128 64 32 16 8 4 2 1

-27

26 25 24 23 22 21 201 0 0 1 0 0 1 0

You can also directly/quickly find the decimal equivalent of a 2s complement number:

• use the usual binary to decimal conversion, using at the most significant bit the negative for the coefficient

Page 18: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

conversion to decimal with conversion to decimal with 32 bit32 bit numbers – numbers – 2s comp.2s comp.

Same idea as 8 bit 2s complement integers, but the most significant bit is –231.

-2,147,483,648 64 32 16 8 4 2 1

27 26 25 24 23 22 21 20-231

-231 230 ... 26 ... 20

Page 19: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Converting n bit numbers into numbers with more than n bits:

– copy the most significant bit (the sign bit) into the other bits

– Example: 4-bit to 8-bit

0010 -> 0000 0010 (both has decimal value 2)

1010 -> 1111 1010 (both has decimal value -6 in 2's

complement)

– This method is valid for both 1's and 2's complement representations

A very important noteA very important note

Page 20: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Subtraction Subtraction

a-b can always be represented as a+(-b). Doing the arithmetic in this way causes wrong results in sign-multitude and 1's complement representations, but not in 2's complement. We will see an example now.

Consider 3 - 2 which is the same as 3 + (-2)

In sign-magnitude representation using 4 bits:

3 + (-2) should give 1, but instead we get -5 !

0 011 = +310

1 010 = -210

+---------

1 101 = -510 which is a wrong result

To remedy this, the operation can take special notice of the sign bits and perform a subtraction instead. This complicates the implementation; we have a better solution using 2's complement (next slide).

Page 21: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Subtraction Subtraction

In 1's complement representation using 4 bits:

3 + (-2) becomes 0 !

0 011 = +310

1 101 = -210

+---------

1 0 000 = 010 which is a wrong result

But two's complement addition results in the correct sum without hassle.

0 011 = +310

1 110 = -210

+---------

1 0 001 = 110 which is the correct result!

We got rid of it automatically since it does not fit

We got rid of it automatically since it does not fit

Page 22: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

There is only one zero. Range for negative numbers is one more than the other representations

Subtraction can be implemented as addition (a - b = a + -b). Thus no borrowing logic needs to be implemented. Let's us give two 8-bit examples.

97 - 120 =? -51 – 70 = ? 01100001 97 11001101 -51 10001000 -120 10111010 -70+------------- +------------- 11101001 -23 110000111 -121

Due to fixed width of the registers, carry overflow is lost automatically.

Why Why 2's Complement? 2's Complement?

Page 23: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Negating a two's complement number is simple:

1. Start at least significant bit. Copy through the first “1”; after that, invert each bit.

• Example: 0010101100

1101010100

2. Alternatively, invert all bits and add one to the least significant bit

If you negate twice, you will arrive to the same number:

0011 3

1101 -3

0011 3

Two's Complement – Two's Complement – NegationNegation

Page 24: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Important NoteImportant Note on Terminology on Terminology!!

"2's complement" (or "two’s complement") does not mean a negative number!

2's complement is a representation used to represent all integers, not just negative integers!

So 2's complement is a format specification, but we also use the term "2's complement of a number" as its negation

e.g. when we want to negate a number, either from positive to negative or negative to positive, we may say "take its 2's complement".

Page 25: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Overview of Built-in Types and Overview of Built-in Types and Their RangesTheir Ranges

Page 26: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Built-in Types in C++Built-in Types in C++

The types which are part of the C++ language; not implemented as a class– char– int, long (a.k.a.. long int), short (a.k.a. short int)– float, double– Mostly for numeric data representation– There are signed (which is the default one) and unsinged versions

for integer/char storage– Signed integer representation uses 2's complement

Now we are going to see some characteristics of these types and their limits. Some of these discussions are not new to you (discussed in CS201), but after learning data representation and 2's complement, they will mean more to you now.

Page 27: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

charcharThe type char is known to store an ASCII character, but actually it stores a signed one

byte integer number (2's complement representation).– Since there is no other one-byte integer type in C++, char is widely used as integers as well– Of course, it is also used to store a character (as seen at the beginning of this

ppt file)char ch;ch = 'A'; //validch = 99; //valid

The type char is by default "signed" in Visual Studio– The range is -128 . . 127ch = -25; //validch = 135; //out of range, but not a syntax error. Compiler gives a warning

– 135 is out of range but still fits in 8-bits. When you have cout << ch;

– Output is the character for which the ASCII code is 135. However, when you have cout << (int) ch;

– Output is signed integer (2's complement) representation of 135 (in 32 bits).13510 = 11111111 11111111 11111111 100001112 which is -121 in 2's complement representation. Thus you see -121 as the output

Page 28: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

unsigned charunsigned charYou can change the dafault behaviour to unsigned by changing the project properties

– Open the project's Property Pages dialog box. – Click on "C/C++"– Click on "Command Line"– Add /J compiler option.

You can also explicitly specify a char variable unsigned by putting the keyword unsigned before char. – For non-negative one-byte integers. Since there are no negatives, no need to

use 2's complement• The range is 0 . . 255.

– For ASCII interpretation, signed and unsigned do not make a difference• The ASCII character corresponding to the binary representation

unsigned char ch;ch = 200; //valid; the ASCII character with code 200ch = -25; //out of range, but not a syntax error. Compiler gives a warning

– -25 is out of range but can be represented in 2's complement as 11100111 in binary and the unsigned interpretation of this bit string is 231. Thus:cout << ch;

– displays the character for which the ASCII code is 231.

Page 29: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

int, short, long, long longint, short, long, long longThe "signed" integer types of C++

Uses 2's complement representation

int– Mostly used signed integer type of C++– Typically the number of bytes used is the word size of the processor

• So in 32 bit computers it is 4 bytes, but for 64-bit computers it should be 8 bytes

– However, Visual Studio fixed it to 4 bytes: thus, in CS204 we can assume that int always uses 4 bytes

• But if you port your code to another platform using another compiler, do not trust that int uses 4 bytes.

– Range:INT_MIN to INT_MAX (these are defined in limits.h or climits header file)-2n-1 . . . 2n-1-1 where n is the number of bits used 32 bits (our case): -231 . . . 231-1 -2,147,483,648 . . . 2,147,483,64764 bits: -263 . . . 263-1 -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

long (can also be used as long int)long num; //can also be defined as long int num;

– Signed integer that always use 4 bytes– The range is the same as 32-bit int

Page 30: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

int, short, long, long longint, short, long, long longlong long (can also be used as long long int)

long long wow; //can also be defined as long long int wow;– Microsoft specific 64-bit signed integer (always 64-bits)– Do not use it for codes to be ported to other platforms, it won't work.– Range: LLONG_MIN to LLONG_MAX ( -263 . . . 263-1 )

short (can also be used as short int)short count; //can also be defined as short int count;– Always 2 bytes– Signed integer that always use 2 bytes– Range:

SHRT_MIN to SHRT_MAX (these are defined in limits.h or climits header file)-215 . . . 215-1 -32768 . . . 32767

count = 31500; //validcount = 35000;//out of range, but not a syntax error. Compiler gives a warning– So, what is the output of cout << count; ?– It displays -30536, why?– Write 35000 in binary in 16-bits and interpret this bit string as a 2's complemented signed

number

3500010 = 1000 1000 1011 10002 = 8 + 16 + 32 + 128 + 2048 – 32768 = -30536

Page 31: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

unsignedunsigned integers integersIn order to store only non-negative values, char, int, short, long, long long can

be defined as unsigned by putting unsigned keyword before the type name.

unsigned int mynum;unsigned short cinekop; // same as unsigned short int cinekop; unsigned long lufer; //same as unsigned long int lufer; unsigned long long kofana; // same as unsigned long long int kofana;

In unsigned representation there is no sign bit; most significant bit is part of the magnitude. Thus we do not need 2's complement.

– In this way, we can use the full range (2, 4 or 8 bytes) for zero and positive values.

The ranges become (note that the positive range is almost doubled as compared to signed integers):

– 16-bit: 0 to USHRT_MAX (defined in limits.h or climits header file)0 . . . 216-1 0 . . . 65535

– 32-bit: 0 to UINT_MAX (defined in limits.h or climits header file)0 . . . 232-1 0 . . . 4,294,967,295

– 64-bit: 0 to ULLONG_MAX (defined in limits.h or climits header file)0 . . . 264-1 0 . . . 18,446,744,073,709,551,615

Page 32: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

unsignedunsigned integers integersUnsigned numbers does not store negatives, but nothing can stop us to assign a

negative value to an unsigned variable

unsigned short num;num = -25;cout << num;

-25 is negative so it is represented using 2's complement. The resulting bit string is then interpreted as an unsigned number since it is assigned to unsigned number (implicit type casting).

-2510 = 1111 1111 1110 01112 = 6551110

So the output becomes 65511

Of course, it is not a normal programmer behavior to assign a negative value to an unsigned variable, but such things may unintentionally occur.

If you use a literal or constant at the right-hand-side of assignment, then compiler may warn you (depending on the warning level). However, if rhs is an expression, then the problem occurs at run-time and compiler cannot see that problem.

Thus, you have to know what happens in such situations to locate the problem easily.

Page 33: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

LimitsLimits

You can include limits.h which defines the ranges of integers (depending on your platform/computer)

#include <limits.h>

OR

#include <climits>

Tip: Type #include <limits.h> (or any other filename) in your program, then go to that line, and right click on the file name and choose “Open Document”. That will bring you this header file.

You can do this in general and it will save you the effort lo locate the file.

Page 34: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Typecasting between signed and unsinged numbersTypecasting between signed and unsinged numbers

Typecasting may be done explicitly or sometimes it happens implicitly (e.g. When you assing an unsigned variable to a signed one, or vice versa)– So, you should know how it executes

Signed to unsigned typecasting– Represent to signed number using 2's complement format.

– Interpret this bit string as unsinged• If MSB is 0, then the signed and unsigned are the same• If MSB is 1, then signed is negative. For unsigned conversion, MSB is not

considered as the sign bit, it is interpreted as part of the magnitude.

– 2 slides ago, we had an example, but let us give another one.short ints = -30000;

unsigned short intus = ints; //implicit typecasting

cout << intus;

Output is 35536, the same bit representation as -30000

Page 35: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Typecasting between signed and unsinged numbersTypecasting between signed and unsinged numbers

Unsigned to signed typecasting– Represent the unsigned number as bit string.– Interpret this bit string as signed

• If MSB is 0, then the unsigned and signed are the same• If MSB is 1, then interpret the bit string as a 2's complemented negaitve value

– I do not mean to take 2's complement.– But you can take 2's complement to understand the magnitude of this negative

number

– Examplesunsigned short usnum = 30000;

cout << (short) usnum; • Output is 30000, MSB of usnum is 0.

unsigned short usnum = 63000;

cout << (short) usnum; • Output is -2536, MSB of usnum is 1.

Moral of the story behind typecasting: They are all the same bit strings; the only thing that changes is how to interpret it

Page 36: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Some tips about selecting integer typeSome tips about selecting integer typeYou may consider to use an unsigned variable if you will store a non-negative

number.

Well, if you are too close to 0, this is a bit risky. Consider the following loop:

unsigned int j;

for (j = 5; j >= 0; j--)

cout << j << endl;

This loop is infinite. When j is 0, it is decremented and you expect to have -1. Actually it is -1 as the bit string representation (a bit string with all 1's in it). This bit string is the largest unsigned integer number when you typecast into unsigned int. Thus it is >=0.

Moral of the story: use unsigned only if you make sure that the value of the variable will never go below 0. Otherwise use signed integers.

Page 37: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Some tips about selecting integer typeSome tips about selecting integer typeDo not mix signed and unsigned numbers in an expression. Some strange things may

happen. Consider the following code:

unsigned int a = 5;int b = -10;if (a+b < 0)

cout << "hede" << endl;else

cout << "hodo" << endl;

You expect the output to be "hede", but it displays "hodo". Why?

In C++, there is a rule saying that built-in operands of an operator must be of the same type. If they are different, one of them is implicitly typecasted to the other before evaluating the expression.

– Typical case: if you add an integer to a double, integer is converted to double before the operation.

– In the example above, signed (b) is typecasted to unsigned. -10 is a binary number with lots of 1's in 2's complement format. Thus as unsigned it is a big number. When added 5, the sum gets bigger and can never be less than 0.

RULE: If there is a signed and an unsigned number in an expression, signed is automatically converted to unsigned by the compiler before the evaluation.

Page 38: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Some tips about selecting integer typeSome tips about selecting integer typeWhich integer size to use?

Of course, this depends on the possible range of values you want to store in this variable.

– Using the largest one all the time may cause unnecessary usage of memory. This is not good for efficiency.

– But on the other hand, allow yourself some margin to proactively defend against some unanticipated problems.

If you want to store a constant or literal bigger than the capacity, the compiler warns you (at compile time)

short num = 45000; //compiler warns

However, if you assign an expression that goes beyond the capacity, then compiler cannot see this and cannot warn you. This is a big problem and technically called as "overflow" and we are going to see overflow today (if time permits, otherwise in the next lecture).

Page 39: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Wrap-up: Wrap-up: NumberNumber Representations Representations

– Unsigned : for non-negative integers

– Two’s complement : for signed integers (zero, negative or positive)

Unless otherwise noted (as unsigned), always assume that numbers we consider are in 2's complement representation.

– IEEE 754 floating-point : for real numbers (float, double)

– We did not add anything for float and double on top of what you know from CS201. At the end of these slides you can find how IEEE 754 floating-point representation works, but we will not talk about this and you are not responsible.

Page 40: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Arithmetic OverflowArithmetic Overflow

Page 41: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

OverflowOverflowIn this subsection, we will see the related topic of overflow, which basically means that after

an operation such as addition or subtraction the result is not correct due to the fact that the result cannot be represented in the allocated space.

There is overflow in the following piece of code since a +b goes beyond the range covered by c

unsigned char a = 200;

unsigned char b = 255;

unsigned char c = a + b;

– We will also give the rule about determining the value of c after the overflow.• This may not look essential since the result is already wrong, but getting into that deep may

help us to find out logic error during debugging.

We will start with small cases where the storage is 4-bits to understand the basics and then later we will generalize to built-in types of C++

We will give the basics of overflow on addition and subtraction with two operands– Other arithmetic operations and expressions with multiple operations may also cause

overflow. We will generalize to this case at the end

Page 42: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Having carry out of MSB?– Arithmetic overflow is not always understood by having a carry out of the MSB

– If there is a carry out of the MSB, then we say that there is a "carry overflow", but this may or may not mean there is "arithmetic overflow" and the result is wrong.

E.g. 7-6 = 7 + (-6)

0111 ( 7)+ 1010 (-6)

1 0001 ( 1) There is a carry out of MSB; it is discarded and the result is correct!

E.g. -7-6 = -7 + (-6)

1001 (-7)+ 1010 (-6)

1 0011 ( 3) There is a carry out of MSB; it is discarded and the result is wrong!

How Can We Detect Arithmetic Overflow?How Can We Detect Arithmetic Overflow?

x

x

Page 43: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Having no carry out of MSB?– Does not always mean that there is no arithmetic overflow. We may have arithmetic

overflow even if there is no carry out of MSB

E.g. 7+1

0111 ( 7)+ 0001 ( 1)

1000 (-8) There is no carry out of MSB, but the result is incorrect!

E.g. 2-3 = 2 + (-3)

0010 ( 2)+ 1101 (-3)

1111 (-1) There is no carry out of MSB and the result is correct!

How Can We Detect Arithmetic Overflow?How Can We Detect Arithmetic Overflow?

Page 44: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Overflow and 8 bit additionOverflow and 8 bit addition

01111000+ 01111000 11110000

11 1 1

It fits, but it’s stilloverflow!

120+120 -16

Reminder:Max 2s complement range with 8 bits: -128 to +12701111000 = 1x64 + 1x32 + 1x16 + 1x8 = 12010

11110000 = -1x128 + 1x64 1x32 + 1x16 = -1610

Overflow!

Page 45: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Overflow – definition & detectionOverflow – definition & detection

Overflow means that the right answer don’t fit !

If you think in decimal and know the ranges, it is easy to detect. 120 +120 = 240 and the range of signed 8-bit integer is -128 . . 127

240 is not in this range, so there is overflow

More formally, there is arithmetic overflow when

the sign of the numbers is the same -AND-

the sign of the result is different than the sign of the numbers

Page 46: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

There can’t be an overflow when adding a positive and a negative number– Why? Basically because the magnitude of the number gets smaller without

changing sign

There can’t be an overflow when signs are the same for subtraction – Why? Same as above since arithmetically this is adding a positve to a

negative.

Overflow occurs when the value affects the sign:1. overflow when adding two positives yields a negative 2. or, adding two negatives gives a positive3. or, subtract a negative from a positive and get a negative (similar to 1)4. or, subtract a positive from a negative and get a positive (similar to 2)

Of course, this rule is for signed integers; for unsigned, we will see later

Detecting OverflowDetecting Overflow

Page 47: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Let us visualize the reason of overflow on 4-bit case for signed integers (2's complement)

Start with the first operand and – (circularly) go up by the second

operand for subtraction– (circularly) go down by the

second operand for addition– Overflow occurs if our arithmetic

operation causes to pass this red line (in any direction)

Wrapping around ( 0 -1 or -1 0) does not mean overflow

VisualizingVisualizing Overflow Overflow Number 2’s-complement0 0000+1 0001 +2 0010+3 0011+4 0100+5 0101+6 0110+7 0111----------------------------------8 1000-7 1001-6 1010-5 1011 -4 1100-3 1101-2 1110-1 1111

Page 48: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

VisualizingVisualizing Overflow Overflow for char and short for char and short char

Number 2’s-complement0 0000 0000+1 0000 0001+2 0000 0010. . . . . .+124 0111 1100+125 0111 1101+126 0111 1110+127 0111 1111----------------------------------128 1000 0000-127 1000 0001-126 1000 0010. . . . . .-4 1111 1100-3 1111 1101-2 1111 1110-1 1111 1111

shortNumber 2’s-complement0 0000 0000 0000 0000+1 0000 0000 0000 0001+2 0000 0000 0000 0010. . . . . .+32764 0111 1111 1111 1100+32765 0111 1111 1111 1101+32766 0111 1111 1111 1110+32767 0111 1111 1111 1111------------------------------------------------32768 1000 0000 0000 0000-32767 1000 0000 0000 0001-32766 1000 0000 0000 0010. . . . . .-4 1111 1111 1111 1100-3 1111 1111 1111 1101-2 1111 1111 1111 1110-1 1111 1111 1111 1111

Page 49: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

The rule of detecting the change of sign in the result applies to all signed integer types of C++.– But only for simple addition and subtraction– What about more complex operations?

No simple formula for that; apply these steps– Simply calculate using decimal arithmetic and see if it fits in the range.– If does not fit, then there is overflow– Convert the overflowed result in binary and truncate as it fits to n-bits (where

n is the number of bits in the corresponding type)– Interpret the truncated bit string in 2's complement logic– Examples

char d = 3*200+21;• 621 is not between -128 . . 127, so there is overflow. • 62110 = 10 0110 11012 Discard the most significant two bits since they do not fit

in 8 bits (storage for char). Resulting bit string is 0110 1101 which is 109 (decimal)

char d = 2*200+15;• 415 is not between -128 . . 127, so there is overflow. • 41510 = 1 1001 11112 Discard the most significant bit since it does not fit in 8 bits

(storage for char). Resulting bit string is 1001 1111 which -97 is (2's compl.)

Detecting OverflowDetecting Overflow – Complex Expressions – Complex Expressions

Page 50: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Let us visualize the overflow case on 4-bit unsigned signed integers

We have two red lines here– There is overflow if you go beyond 0 and

beyond 15– If you add 1 to 15 you end up 10000 in

binary and when you discard the overflow bit, the resulting value becomes 0.

– Similarly subtracting 1 from 0 yields 15

Generalization of this case to n-bit unsigned integers is trivial– Max value is 2n-1 and number of bits in

binary is n

Evaluation of complex expression is similar to signed case– Do the operation, convert to binary, discard

the overflowed bits– But this time interpret as unsigned number

Detecting Detecting OverflowOverflow - unsigned integers - unsigned integersDec. Number Binary number----------------------------------------0 0000+1 0001 +2 0010+3 0011+4 0100+5 0101+6 0110+7 0111+8 1000+9 1001+10 1010+11 1011 +12 1100+13 1101+14 1110+15 1111----------------------------------------

Page 51: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

So far we have not talked much about automatic ways of detecting overflows in programs – Only detecting the change in the sign bit for addition and subtraction of

signed integers

Unfortunately, there is no other silver bullet for detecting overflows once it occured

Better if overflows are avoided– To do so, you may use simple expressions and check the values of the

operands to see if they are small enough not to cause overflow– For example suppose a and b are two unsigned ints, a*b does not overflow if

b < UINT_MAX / a

Detecting OverflowDetecting Overflow in Programs in Programs

Page 52: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Floating Point RepresentationFloating Point Representation

SKIP – Not Covered in CS204

You may read if you are curious

Page 53: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Floating Point (a brief look)Floating Point (a brief look)

We need a way to represent:

– numbers with fractions, e.g., 3.1416

– very small numbers, e.g., .000000001

– very large numbers, e.g., 3.1 x 1020

Solution: A floating (decimal) point representation

IEEE 754 floating point representation is the standard:

- ------------------------------ --------- ≡ +/- ……………….. X 2 -------

sign mantissa exponent

– single precision: 1 bit sign, 23 bit significand (mantissa), 8 bit exponent

– more bits for significand gives more accuracy

– more bits for exponent increases range

Range approximately: 10–44 to 1038

Page 54: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

IEEE Floating Point Std. - IEEE Floating Point Std. - DetailsDetails

The MantissaThe mantissa, also known as the significand, represents the precision bits of

the number.

To find out the value of the implicit leading bit, consider that any number can be expressed in scientific notation in many different ways. For example, the number five can be represented as any of these:

– 5.00 × 100 – 0.05 × 102 – 5000 × 10-3

In order to maximize the quantity of representable numbers, floating-point numbers are typically stored in normalized form. This basically puts the radix point after the first non-zero digit. In normalized form, five is represented as 5.0 × 100.

Page 55: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Floating PointFloating Point – what floats? – what floats?For simplicity, let’s use a decimal representation and assume we have 1 digit for sign, 8 digits for the mantissa and 3 digits for the exponent:

+/- - - - - - - - - - - -

We will “illustrate” the format for the number 0.000000000023

-10

0.000000000023 = . 23 0 0 0 0 0 0 x 10 - - -

So it will be stored as . 2 3 0 0 0 0 0 0 - 1 0 mantissa exponent

The actual IEEE Floating point representation follows this principle, but differs from this in details:

- normalization (floaing point comes after the first nonzero digit) - binary instead of decimal - exponent (not sign/magnitude but biased)

Page 56: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Bias – why?Bias – why?

Since we want to represent both positive and negative exponents, e.g. 1011 and 10-11, we can do two things:

1. Reserve a separate sign bit for the exponent

2. Use only positive exponents, together with a bias– The bias (e.g. 127) is subtracted from whatever is stored in the exponent, to find

the real exponent

Stored exponent= 0 real exponent= 0 – 127 = -127

Stored exponent=227 real exponent= 227 – 127 = 100

Page 57: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Bias of the ExponentBias of the Exponent

The Exponent

The exponent field needs to represent both positive and negative exponents. To do this, a bias is added to the actual exponent in order to get the stored exponent. – For IEEE single-precision floats, this value is 127.

Thus, – if the real exponent is zero, 127 is stored in the exponent field.

– if 200 is stored in the exponent field, it actually indicates a real exponent of

(200-127), or 73.

Exponents of -127 (all 0s) and +128 (all 1s) are reserved for special numbers (NaN, Infnty)

Page 58: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

IEEE 754 floating-point standardIEEE 754 floating-point standard: summary: summary

Leading “1” bit of significand is implicit

Exponent is “biased” to make sorting easier– all 0s is smallest exponent, all 1s is largest– bias of 127 for single precision (note addition of the bias while storing, subtracting of

the bias while converting to decimal)

– Decimal equivalent: (–1)sign significand) 2exponent - bias

Example:– decimal: -.75 = - (0.5 + 0.25)– binary: -.11– canonical form: -1.1 x 2-1 (note: shifting the radix point by k is same as multip./dividing by radixk)

– stored exponent = 126 = 01111110– Resulting IEEE single precision representation:

1 10000000000000000000000 01111110 sign mantissa exponent

Page 59: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

A more complex exampleA more complex exampleLet us encode the decimal number −118.625 using the IEEE 754 system.

– First we need to get the sign, the exponent and the fraction. Because it is a negative number, the sign is "1".

– Now, we write the number (without the sign; i.e. unsigned, no two's complement) using binary notation. The result is 1110110.101 (notice how we represent .625)

– Next, let's move the radix point left, leaving only a 1 at its left: 1110110.101 = 1.110110101 × 26. This is the normalized floating point number. The

mantissa is the part at the right of the radix point, filled with 0 on the right until we get all 23 bits. That is 11011010100000000000000.

– The exponent is 6, but we need to bias it and convert it to binary (so the most negative exponent is stored as 0, and all exponents are non-negative binary numbers). For the 32-bit IEEE 754 format, the bias is 127 and so the stored exponent is 6 + 127 = 133. In binary, this is written as 10000101.

Putting them all together:

This example is from wikipedia.

Page 60: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

IEEE Floating Point: IEEE Floating Point: RangesRanges

+ .00000000....0 000000001 = + (0+1) x 21-127 = + 1.0 x 2-126

= 0 1 23 bits mantissa 8 bits exponent

Note1: Exponent “00000000” is reserved for special numbers, so min is “00000001”Note2: Approx. conversion between 2s powers and 10s powers: Ex. 2-149 = 10-44.85 since 23.3 = 10 and 149/3.3 = 45

Explanation for minimum positive (just a sign chg. for negative):

Page 61: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

IEEE Floating Point RangesIEEE Floating Point Ranges

+ .111.....1 11111110 = + (1- 2-23 +1) x 2254-127 = + 1.0 x 2127

=1- 2-23 = 254

23 bits mantissa 8 bits exponent

Note1: Since it represents the part after the radix point, “.111111…1” = 1-2-23 , just as “.11” = 1-2-2

Note2: 11111111 as exponent is reserved for special numbers, so max is 11111110

Explanation for maximum positive (just change sign for negative):

Page 62: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

SummarySummary

Computer arithmetic is constrained by limited precision

Bit patterns have no inherent meaning but standards do exist– two’s complement

– IEEE 754 floating point

Computer instructions determine “meaning” of the bit patterns

http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html

Page 63: Data Representation Overflow Limits No good explanations in the books Try to understand in class and from the slides

Floating Point ComplexitiesFloating Point Complexities

• In addition to overflow we can have “underflow”

• A number that is smaller than what is representable (e.g. < 2-126)

• Accuracy can be a big problem

• IEEE 754 keeps two extra bits, guard and round

• four rounding modes

• positive divided by zero yields “infinity”

• zero divide by zero yields “not a number”

• other complexities…