Presentation is loading. Please wait.

Presentation is loading. Please wait.

12302010004火雨辰 homeWork1.

Similar presentations


Presentation on theme: "12302010004火雨辰 homeWork1."— Presentation transcript:

1 火雨辰 homeWork1

2 Q1 binary calculation It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by step! Or you can’t get marks of this question. 0011 * x * y = (a) 0100 * x * y = (b)

3 Q1 binary calculation 3x+4y=9 4x+3y=8 x=5/7,y=12/7

4 Q1 binary calculation Solution1: (b)–(a) 0001*x+1111*y=1111 (c)
(c)* *x+1110*y= (d) (c)+(d) 0011*x+1101*y=1101 (e) (e) –(a) 1001*y=  y=0100 0011*x=  x=0011

5 Q1 binary calculation Solution2: x=abcd(2) y=efgh(2) (a)  (a+b+e)(b+c+h)(a+d)d = 1001 (b)  (e+f+c)(f+g+d)(g+h)h = 1000 From right to left solve the function

6 Q1 binary calculation Solution3: (3x+4y) mod 16= 9 (4x+3y) mod 16= 8 3x + 4y = a 4x + 3y = b x,y ϵ [0,15] x = 3, y = 4

7 Q2 Operations Given A and B with hexadecimal expression 0xDC and 0xE3 respectively.Calculate the values of the following expressions: A & B 0xC0 A && B 0x01 A | B 0xFF A || B 0x01 ~ A |~ B 0x3F ! A || B 0x01 A&&~ B 0x01

8 X>>3 (Arithmetic)
Q2 Operations X X<<4 X>>3 (Logical) X>>3 (Arithmetic) Hex 0xF3 0x30 0x1E 0xFE 0x0E 0xE0 0x01 0xCD 0xD0 0x19 0xF9 0xA9 0x90 0x15 0xF5

9 Q3 Endian Consider the following calls to show_bytes() from Figure2.4 page 76 (English text book) int val = 0x1234ABCD; byte_pointer valp = (byte_pointer) &val; show_bytes(valp,1); /* A. */ show_bytes(valp,2) /* B. */ show_bytes(valp,3) /* C. */

10 Q3 Endian Indicate which of the values would be printed by each call on a little-endian and on a big-endian machine: A.Little endian: _CD_____; Big endian: __12____; B.Little endian: _CDAB___; Big endian: __1234_; C.Little endian: _CDAB34_; Big endian: _1234AB;

11 Q3 Endian Try to write an C function to determine whether the machine running this code is big endian or small endian. The function should have no more than 2 lines. (It’s okey to do this step by step, ignore the restriction when you start) Int BIG_small(){ _________(1)___________; _________(2)___________; } // return 0 if small, 1 for big

12 Q3 Endian Int i = 1; Return !(*((char *)&i));

13 Q3 Endian Fault 1: Byte_pointer y = &0x01000000; return y[0]; Fault 2:
Char *p = &0x1100; return p;

14 Q3 Endian Fault 3: Unsigned char* x = 0x100; Return (int)x[0];
Int a = 0x0100; Return ((char *)&a)[0];

15 Q3 Endian Fault 5: Int x = 0xFF; Return !(&x)[0]; Fault 6:
Unsigned short x = 0x ; Return (unsigned char)x;

16 Q3 Endian Fault 7: Unsigned int x =1; Return (int)((char*)&x);
Int x = 0x01; Return !(x ^ show_byte(x,3));

17 Q3 Endian Fault 9: Unsigned char c = 0x1200; Retur c[0]; Fault 10:
Short int a = 0x1122; Return (char)a == 0x11;

18 Q3 Endian Fault 11: Int x = 0x12345678; Return (&x)[0]==0x12;
Return ((char*) &x)[0];

19 Q4 Encodings Expression 2’s-complement Signed 8-bit Decimal 3+2
Suppose all the numbers are 8‐bit long. Give the result of the following expressions of C language in full 8‐bit 2’s complement form and signed decimal form. Expression 2’s-complement Signed 8-bit Decimal 3+2 5 99/2 (-23)/2 round to zero -127-1 125+3 125>>3

20 Q5 Sign vs. unsign So easy (unsigned)-7 -8 -7 (unsigned)-8

21 Q6 casting The following C code pieces are executed on a typical 32‐bit big endian machine with 2’s complement encoding. Please give the output. int main() { int x = 257; char y = -10; int z = 128; char a = (char)x; short b =(short)y; unsigned short ud = (unsigned short)b; char c = (char)z; unsigned int w = (c > 0) ? 0 : 1; printf("a=%d,b=%d,ud=%x,c=%d,w=%d\n",a,b,ud,c,w); }

22 Q6 casting Correct: A=1, b=-10, ud=0xfff6,c =-128,w=1 Incorrect:
Ud = 0xfffffff6 What about little endian?

23 Q7 integer Arithmetic easy

24 Q8 Float num Normalized: V = (-1)sign * (1.fraction) * 2e-bias
Where bias = 2K-1-1 Denormalized: V = (-1)sign * (0.fraction) * 2E where E = 1 – (2K-1-1)

25 Q8 Float num Convert decimal fraction into binary form
* 2 = * 2 = 0.375 * 2 = 0.75 * 2 = 0.5 * 2 = = ( )2

26 Q8 Float num (12.625)10 = ( )2 = * 23 (0x4c18)16 = ( )2 = * 27

27 Q8 float num Sum = * * 23 = * 27 = * 27 = 0x4C50 Mul = * * 210 = * 210 = * 210 = 0x5350 or = * 134 = = 0x5350


Download ppt "12302010004火雨辰 homeWork1."

Similar presentations


Ads by Google