21
Young Won Lim 2/17/18 Type Cast (1A)

Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Young Won Lim2/17/18

Type Cast (1A)

Page 2: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Young Won Lim2/17/18

Copyright (c) 2010-2018 Young W. Lim.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".

Please send corrections (or suggestions) to [email protected].

This document was produced by using LibreOffice.

Page 3: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Type Cast 3 Young Won Lim2/17/18

Type Conversion

Implicit Type Conversion

m = (int) 1.25;

Explicit Type Conversion

Promotion

Demotion

1 / 2.0 1.0 / 2.0

int m;m = 1.25; m = 1; truncation problem

No warning message

Page 4: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Type Cast 4 Young Won Lim2/17/18

0 0 0 0

Type Conversion (int → double) – implicit one

Implicit Type Conversion Promotion

int m;m = 123;

double x;x = m;

0x7B

0 1 1 1 1 0 1 1

0 1 1 1 1 0 1 1

1 1 1 0 1 1 0 0

(0x7B / 2^6) * 2^6

implicit one

0 0 0 0

0 0

0xEC 00 00 00 …

Page 5: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Type Cast 5 Young Won Lim2/17/18

Type Conversion (int → double) – 16 hexa digits

Exponent : 11bitSign : 1bit = 4 * 3 → 3 hexa digits

6+ 1023 = 1029 → 0x405

0x405 EC 00 00 00 00 00 0

(0x7B / 2^6) * 2^6

0xEC 00 00 00 00 00 0

Fraction : 52bit = 4 * 13 → 13 hexa digits

excess 1023 code

0x40 5E C0 00 00 00 00 00

Page 6: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Type Cast 6 Young Won Lim2/17/18

Type Conversion (int → double) – Little Endian

0x40 5E C0 00 00 00 00 00

LSBMSB

00

00

00

00

00

C0

5E

40

0x0000

0x0001

0x0002

0x0003

0x0004

0x0005

0x0006

0x0007 MSB

LSB

1-byte

Little Endian:LSByte First

Page 7: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Type Cast 7 Young Won Lim2/17/18

Type Casting

double x = 123;int *p;double *q;

00

00

00

00

00

C0

5E

40

0x0000

0x0001

0x0002

0x0003

0x0004

0x0005

0x0006

0x0007

0x0008

0x0009

0x000A

0x000B

0x000C

0x000D

0x000E

0x000F

p q

Page 8: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Type Cast 8 Young Won Lim2/17/18

Type Casting

double x = 123;int *p;double *q;

q = &x;

00

00

00

00

00

C0

5E

40

0x0000

0x0001

0x0002

0x0003

0x0004

0x0005

0x0006

0x0007

0x0008

0x0009

0x000A

0x000B

0x000C

0x000D

0x000E

0x000F

q

*q → 123.0

Page 9: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Type Cast 9 Young Won Lim2/17/18

Type Casting

double x = 123;int *p;double *q;

p = (int *) &x;

00

00

00

00

00

C0

5E

40

0x0000

0x0001

0x0002

0x0003

0x0004

0x0005

0x0006

0x0007

0x0008

0x0009

0x000A

0x000B

0x000C

0x000D

0x000E

0x000F

p

*p → 0

Page 10: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Series:2. Pointers

10 Young Won Lim2/17/18

Pointer Type Cast

Page 11: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Series:2. Pointers

11 Young Won Lim2/17/18

Changing the associated data type of an address

long a;

int * p;

short * q;

char * r;

address of a long value

address of an int value

address of a short value

address of a char value

Page 12: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Series:2. Pointers

12 Young Won Lim2/17/18

Pointer Type Casting

long a;

int * p;

short * q;

char * r;

&a

q = (short *) &a

r = (char *) &a

p = (int *) &a

address of a long value

address of an int value

address of a short value

address of a char value

Page 13: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Series:2. Pointers

13 Young Won Lim2/17/18

Re-interpretation of memory data – case I

long a;

int *p;

short *q;

char *r; 80

0x7080

0x50607080

0x1020304050607080

&a

q = (short *) &a

r = (char *) &a

p = (int *) &a

Page 14: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Series:2. Pointers

14 Young Won Lim2/17/18

Pointer Type Cast

long a;

int *p;

short *q;

p

char *r;

0x10203040 0x50607080

p = (int *) &a

starting address

&a

*p 0x50607080

Page 15: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Series:2. Pointers

15 Young Won Lim2/17/18

Integer Pointer Types

long a;

int *p;

short *q; q

char *r;

0x10200x30400x50600x7080

q = (short *) &a

&a

*q 0x7080

Page 16: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Series:2. Pointers

16 Young Won Lim2/17/18

Integer Pointer Types

long a;

int *p;

short *q;

char *r; r

10 20 30 40 50 60 70 80

r = (char *) &a

&a

*r 0x80

Page 17: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Series:2. Pointers

17 Young Won Lim2/17/18

Re-interpretation of memory data – case II

10 20 30 40 50 60 70 80

&c

q = (short *) &c

a = (long *) &c

p = (int *) &cint *p;

short *q;

char *r;

long *a;

char c;

r = &c

Memory alignment constraint is not met

In this case, the memory alignment constraint can be broken

Page 18: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Series:2. Pointers

18 Young Won Lim2/17/18

Pointer Type Cast

char c;

&c

10 20 30 40 50 60 70 80

10 20 30 40 50 60 70 80

10 20 30 40 50 60 70 80

10 20 30 40 50 60 70 80

10 20 30 40 50 60 70 80

long *a;

Page 19: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Series:2. Pointers

19 Young Won Lim2/17/18

Pointer Type Cast

char c;

&c

10 20 30 40 50 60 70 80

10 20 30 40 50 60 70 80

10 20 30 40 50 60 70 80

10 20 30 40 50 60 70 80

10 20 30 40 50 60 70 80

int *p;

Page 20: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Series:2. Pointers

20 Young Won Lim2/17/18

Pointer Type Cast

char c;

&c

10 20 30 40 50 60 70 80

10 20 30 40 50 60 70 80

10 20 30 40 50 60 70 80

10 20 30 40 50 60 70 80

10 20 30 40 50 60 70 80

short *q;

Page 21: Type Cast (1A) · 2/17/2018  · Type Cast 5 Young Won Lim 2/17/18 Type Conversion (int → double) – 16 hexa digits Exponent: 11bit Sign: 1bit = 4 * 3 → 3 hexa digits 6+ 1023

Young Won Lim2/17/18

References

[1] Essential C, Nick Parlante[2] Efficient C Programming, Mark A. Weiss[3] C A Reference Manual, Samuel P. Harbison & Guy L. Steele Jr.[4] C Language Express, I. K. Chun[5] http://www.stackoverflow.com