Review CAS CS210 Ying Ye Boston University

advertisement
Review
CAS CS210
Ying Ye
Boston University
Logical expressions
Truth table
input: A, B, C
output: D
A
B
C
D
0
0
0
1
1
0
0
0
1
1
0
0
1
1
1
1
0
1
0
0
0
1
1
0
0
0
1
0
1
0
1
1
(~A)(~B)(~C)
D = (~A)(~B)(~C)
+ ABC + A(~B)C
ABC
A(~B)C
Logical expressions
 Properties:
expression = NOT( NOT(expression) )
NOT( A AND B) = NOT(A) OR NOT(B)
NOT( A OR B) = NOT(A) AND NOT(B)
 Usage:
A AND B = NOT( NOT(A) OR NOT(B) )
A OR B = NOT( NOT(A) AND NOT(B) )
 Using constant
NOT(A) = A XOR 1
Operators
 int a , b = 2;
 1. a == ?
A. 2
B. 0
C. I don't know
 2. a = ++b, b++; a == ?
A. 3
B. 4
C. I don't know
 3. a = b += ++b; a == ?
A. 3
B. 6
C. 2
B. 3
C. 6
4. b == ?
A. 2
 5. a = b += b++; a == ?
A. 2
B. 4
C. 6
B. 6
C. 5
6. b == ?
A. 2
Floating-point numbers
Single precision IEEE floating-point format:
11000000010000000000000000000000
sign bit: 1
negative
exponent (biased 127): 10000000
value: (10000000)2 - 12710 = 110
fraction: 10000000000000000000000
value: 1.10000000000000000000000
floating-point number: -1 * (1.1) * 21
Floating-point numbers
 use a floating point representation with a sign bit in the
leftmost position, followed by a three-bit two’s complement
exponent, followed by a normalized three bit fraction in base 2.
A normalized fraction means a 1 to the right of the binary point,
for example .101 is normalized. Zero is represented by the bit
pattern 0000000. There is no hidden 1.
There are a total of seven bits in this floating point
representation, and there are 27 = 128 unique bit patterns. How
many of these bit patterns are valid?
Floating-point numbers
 +/- X * 2e
 Why normalized fraction?
.001 * 22 = .010 * 21 = 0.100 * 20
 If X = 0: only 1 valid number
 If X != 0: the most significant bit of X must be 1, leaving only 2
3 free
free bits for fraction
exponent
bits
1 free
sign bit
total valid number = 1
1 valid
for X = 0
+
2
*
4
*
2 free
fraction
bits
8
Practice exam
 Convert the following binary numbers to hexadecimal:
101111100; 1101; 1011001110011; 110100000100100
 Suppose a 7-bit representation. What are the decimal values of
the following two’s complement binary numbers? 0001110;
1111000; 1110101
Pointer
 download from http://cs-people.bu.edu/yingy/review.c
 replace /*TODO*/ with pointer operations, not allowed to use
variable a, b except in the first TODO
Pointer
 Output
a=1
b=2
++a = 2
a++ = 2
b=4
a+b=7
Pointer
Possible solution:
int *p1 = &a, *p2 = &b;
printf("a = %d\n", *p1);
printf("b = %d\n", *p2);
printf("++a = %d\n", ++(*p1));
printf("a++ = %d\n", (*p1)++);
*p2 = 4;
printf("b = %d\n", *p2);
printf("a + b = %d\n", *p1 + *p2);
Download