1/51
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
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)
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 —> int</p>](https://knowt-user-attachments.s3.amazonaws.com/27befde7-0b18-49d4-bf92-63c3465cfebf.png)
Describe the purpose of the function lower
Char —> int
maps single character to lower case for the ASCII character set

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)
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
What are some general rules when arithmetic conversions contain no unsigned operands?
If either operand is a long double convert the other to long double
OTHERWISE, If either operand is a double, convert the other to double
OTHERWISE if float, convert the other to float
OTHERWISE convert char and short to int
THEN if either is long, convert the other to long
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
Explain the complications that come with unsigned operand expressions

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

Describe what happens when a float converts to int
Truncation of any fractional part
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
When may type conversions occur?
Casting
Setting values to variables
Expressions
Argument of function call (expression)
In the absence of a function prototype…
char/short —> int
float —> double

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
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
Exercise 2-3 (pg 46 CProgramming)
Integral operands
char
short
int
long
(signed or unsigned)
Describe the difference between bitwise operators & and | from the logical operators && and ||
Logical operators use Left-to-right evaluation
Bitwise operators compare each bit

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
Exercise 2-6
Exercise 2-7
Exercise 2-8
Floating Point Numbers; Uses?
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
What are some of the issues when it comes to rounding floats
Describe the mathematical properties of addition, multiplication, and relational operators
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
Practice Problem 2.46 (CSApp)
2.45 (CSApp)
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
What are the three cases that occur when encoding a given bit representation?
Normalized Values
Denormalized Values
Special Values
What are the three fields of the bit representation of a floating-point number
Single sign bit s directly encodes the sign s
The k-bit exponent field exp = ek-1…e1e0 encodes the exponent E
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
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
exp is neither all zeros (numeric value 0) nor all ones (numeric value 255 for single precision, 2047 for double)

Steps or representing a Floating Point number in IEEE
Step III is dependent on if the machine is single precision or double precision


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
Explain how exponents work in IEEE floating point standard form (ranges?)

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)

Practice 2.47
Explain exponent Bias
2.48
2.49
List the four rounding modes
Round-to-even (aka round-to-nearest) = default
rounds .50 to an even number (ex. 1.5 and 2.5 —> 2)
Round-toward-zero
pos numbers round down, neg numbers round up
Round-down
pos and neg numbers go down
Round-up
pos and neg numbers go up
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
How is round-to-even rounding applied to binary fractional numbers?
2.51
2.52
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>](https://knowt-user-attachments.s3.amazonaws.com/4c6e82c5-4cd7-45c6-83cb-9dc2d6964ce0.png)
Explain the properties of floating-point addition
Commutative
Not associative
Monotonicity
if a>= b then x + a >= x +b
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
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
2.54