8/27 Pre Lecture Readings (CSApp 2.4, CProgramming 2.7, 2.9)

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/51

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 6:59 PM on 8/27/24
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

52 Terms

1
New cards

What is the general rule for type conversions

  • If there is a ‘wider’ operand with more information as opposed to a ‘narrower’ operand with less, convert the narrower one to the wider one so as not to lose information

  • Longer integer —> shorter may draw warning but not illegal

  • using a float as a subscript is disallowed (because expression doesn’t make sense)


2
New cards

What does s[i] - ‘0’ do in C

Gives the numeric value of the character stored in s[i] because the values of ‘0’, ‘1’, etc., form a contiguous increasing sequence

Basically, char —> int

<p>Gives the numeric value of the character stored in s[i] because the values of ‘0’, ‘1’, etc., form a contiguous increasing sequence</p><p>Basically, char —&gt; int</p>
3
New cards

Describe the purpose of the function lower

Char —> int

  • maps single character to lower case for the ASCII character set


<p>Char —&gt; int</p><ul><li><p>maps single character to lower case for the ASCII character set</p></li></ul><p></p>
4
New cards

Describe the purpose of <ctype.h>

Standard header that defines a family of functions

  • These functions provide tests and conversions that are independent of character set

  • Basically “portable replacement” for long arithmetic

    • like c + ‘a’ - ‘A’ which can be replaced with tolower(c)


5
New cards

When char is converted to an int, can it ever produce a negative integer?

Yes and no

Ultimately, the definition of C guarantees that any character in the machine’s standard printing character set will never be negative — Basically, in C, a machine’s standard printing character set will always be positive

BUT Arbitrary bit patterns stored in character variables may appear negative on some machines and positive on others

For portability, specify signed or unsigned if non-character data is to be stored in char variables

6
New cards

What are some general rules when arithmetic conversions contain no unsigned operands?

  1. If either operand is a long double convert the other to long double

  2. OTHERWISE, If either operand is a double, convert the other to double

  3. OTHERWISE if float, convert the other to float

  4. OTHERWISE convert char and short to int

  5. THEN if either is long, convert the other to long


7
New cards

What is one of the main reasons for using float

  • Save storage in large arrays

  • Save time on machines where double-precision arithmetic is particularly expensive

Floats in an expression are not automatically converted to double


8
New cards

Explain the complications that come with unsigned operand expressions

knowt flashcard image
9
New cards

Describe what might change when a Longer integer is converted to a char

Whenever a longer integer converts to a shorter one, the excess high-order bits are dropped

<p>Whenever a longer integer converts to a shorter one, the excess high-order bits are dropped</p>
10
New cards

Describe what happens when a float converts to int

Truncation of any fractional part

11
New cards

Describe what happens when a double is converted to a float

Result is implementation-dependent; Depending on the implementation, the value may or may not be rounded or truncated

12
New cards

When may type conversions occur?

  • Casting

  • Setting values to variables

  • Expressions

  • Argument of function call (expression)


13
New cards

In the absence of a function prototype…

char/short —> int

float —> double

<p>char/short —&gt; int</p><p>float —&gt; double</p>
14
New cards

Cast operator

(type-name) [expression]

[expression] is assigned to a variable of a specific type

ex. sqrt expects double, so we can do sqrt(double) n) if n is not a double

n itself is not altered, the cast itself produces a separate value of n in the proper type

15
New cards

How come sqrt can sometimes take in an argument that is not a double?

  • sqrt has this function prototype: double sqrt(double); meaning the argument is expected to be a double

  • Thus, root2 = sqrt(2) automatically produces 2.0 from the 2 without casting


16
New cards

Exercise 2-3 (pg 46 CProgramming)

17
New cards

Integral operands

  • char

  • short

  • int

  • long

(signed or unsigned)


18
New cards

Describe the difference between bitwise operators & and | from the logical operators && and ||

Logical operators use Left-to-right evaluation

Bitwise operators compare each bit


<p>Logical operators use Left-to-right evaluation</p><p>Bitwise operators compare each bit</p><p></p>
19
New cards

Logical and Arithmetic Shift only needs to be distinguished for which direction shift?

Right shift because moving right leaves the question of what to do with the MSB (signed bit)

Right shift leaves the left places to be either 1 or 0

20
New cards

Exercise 2-6

21
New cards

Exercise 2-7

22
New cards

Exercise 2-8

23
New cards

Floating Point Numbers; Uses?

24
New cards

How are numbers represented in the IEEE floating-point format?

The same way decimal numbers are separated by a ‘.’ and evaluated, binary numbers have a ‘.’ such that bits on the left of the ‘.’ are weighted by positive powers of 2, and those on the right are weighted by negative powers of two

25
New cards

What are some of the issues when it comes to rounding floats

26
New cards

Describe the mathematical properties of addition, multiplication, and relational operators

27
New cards

What is a downside to finite-length encodings when it comes to fractions

  • finite-length encodings cannot represent numbers such as 1/3 and 5/7 exactly


28
New cards

Practice Problem 2.46 (CSApp)

29
New cards

2.45 (CSApp)

30
New cards

Describe the IEEE floating-point standard form

V = (-1)s x M x 2E

  • sign s —> negative? (s=1) positive?(s=0)

  • Significand M —> fractional binary number (range: 1 to 2 - epsilon or 0 to 1 - epsilon)

  • Exponent E weights the value by a (possibly negative) power of 2


31
New cards

What are the three cases that occur when encoding a given bit representation?

  1. Normalized Values

  2. Denormalized Values

  3. Special Values


32
New cards

What are the three fields of the bit representation of a floating-point number

  1. Single sign bit s directly encodes the sign s

  2. The k-bit exponent field exp = ek-1…e1e0 encodes the exponent E

  3. The n-bit fraction field frac = fn-1…f1f0 encodes the significand M, but the value encoded also depends on whether the exponent field equals 0

Basically, these “fields” are sections of the bit representation

EXAMPLES

Single Precision (float in C)

  • number of bits for the sign s = 1,

  • Number of bits for the exponent field exp = 8

  • number of bits for the fraction field frac = 23


33
New cards

Normalized Values

The most common case bit representation of a floating-point representation.

  • Represents a signed integer in biased form - exponent value E = e - Bias (e is the unsigned number with its won bit representation) (Bias is a value equal to 2k-1-1)

  • frac represents a fractional value f (from 0, less than 1) —- THE BINARY POINT TO THE LEFT OF THE MSB (MANTISSA)

  • M is the SIGNFICAND —- implied leading 1 representation because the leading bit always equals 1

CONDITION

  1. exp is neither all zeros (numeric value 0) nor all ones (numeric value 255 for single precision, 2047 for double)



34
New cards
<p>Steps or representing a Floating Point number in IEEE </p>

Steps or representing a Floating Point number in IEEE

Step III is dependent on if the machine is single precision or double precision


<p>Step III is dependent on if the machine is single precision or double precision</p><div data-youtube-video=""><iframe width="640" height="480" allowfullscreen="true" autoplay="false" disablekbcontrols="false" enableiframeapi="false" endtime="0" ivloadpolicy="0" loop="false" modestbranding="false" origin="" playlist="" src="https://www.youtube.com/embed/8afbTaA-gOQ" start="0"></iframe></div><p></p>
35
New cards
<p>DeNormalized Values</p>

DeNormalized Values

CONDITION: exponent field is all zeros —→ the represented number is in denormalized form

  • implied zero to the left of the binary point

  • Can be used to represent numbers very close to 0 due to a property called gradual underflow (numeric values are spaced evenly near 0.0)

Ex. 0 is a denormalized number


36
New cards

Explain how exponents work in IEEE floating point standard form (ranges?)

37
New cards
<p>Special Values</p>

Special Values

0 is used for denormalized numbers, but 255 (max in 32-bit) is used for special values

Exponent field is all ones

  • Thus INFINITY, which can represent results that overflow (“NaN” which is Not a number)



<p>0 is used for denormalized numbers, but 255 (max in 32-bit) is used for special values</p><p>Exponent field is all ones</p><ul><li><p>Thus INFINITY, which can represent results that <em>overflow</em> (“NaN” which is Not a number)</p></li></ul><p></p><div data-youtube-video=""><iframe width="640" height="480" allowfullscreen="true" autoplay="false" disablekbcontrols="false" enableiframeapi="false" endtime="0" ivloadpolicy="0" loop="false" modestbranding="false" origin="" playlist="" src="https://www.youtube.com/embed/WZUPNXsusOA" start="0"></iframe></div><p></p>
38
New cards

Practice 2.47

39
New cards

Explain exponent Bias

40
New cards

2.48

41
New cards

2.49

42
New cards

List the four rounding modes

  1. Round-to-even (aka round-to-nearest) = default

    • rounds .50 to an even number (ex. 1.5 and 2.5 —> 2)

  2. Round-toward-zero

    • pos numbers round down, neg numbers round up

  3. Round-down

    • pos and neg numbers go down

  4. Round-up

    • pos and neg numbers go up


43
New cards

Why would we use Round-to-even instead of rounding every number up or down?

  • Rounding all numbers down skews a statistical average down

  • Likewise, rounding all numbers up skews a statistical average up

  • Round-to-Even always rounds numbers upward about 50% of the time and downward about 50% of the time, avoiding the statistical bias

  • Round to even works for non-whole-digit rounding

  • Works for binary fractional numbers


44
New cards

How is round-to-even rounding applied to binary fractional numbers?

45
New cards

2.51

46
New cards

2.52

47
New cards

How may rounding affect floating-point operations?

It may eliminate certain values do to the non-associative properties of Round(x [operation] y)

<p>It may eliminate certain values do to the non-associative properties of <code>Round(x [operation] y) </code></p>
48
New cards

Explain the properties of floating-point addition

  • Commutative

  • Not associative

  • Monotonicity

    • if a>= b then x + a >= x +b


49
New cards

Explain the properties of floating point multiplication

  • commutative

  • not assocciative

  • Does not distribute over addition

  • Monotonicity

    • if a >=b and c>=0 then Round(a * c) > = Round(b * c)

    • These monotonicity properties do not hold for unsigned or twos complement multiplication


50
New cards

When casting values between int, float and double, express the changes of the numeric values/bit representations (assuming 32-bit int)

int —> float

  • cannot overflow

  • May be rounded

Int/float —> double

  • Exact numeric value can be reserved bc double has a greater range (range of representable values) and precision (num of significant bits)

float/double —> int

  • rounded towards zero

  • Value may overflow


51
New cards

2.54

52
New cards