CS计算机代考程序代写 compiler Java Microsoft PowerPoint – 04_BinaryRepresentation.pptx

Microsoft PowerPoint – 04_BinaryRepresentation.pptx

1

CS 2211
Systems Programming

Part Four:
Memory Maps

1 2

C is a HEAVILY TYPED language
computer stores ons and offs
the ons are 1 and offs are 0

BUT ! what data does this sequence of ons and offs represent ????

Individual bits

8 bits == 1 byte

0 1 0 0 0 0 1 0 0 0 1 1 1 1 1 1 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 1

3

BINARY (base 2) NUMBERIC SYSTEM

Direct translation of systems:

3 boxes

3

11 boxes

11

4

128 64 32 16 8 4 2 1

with this, you can decode any binary number (up to 255 [28] decimal)

10010010 = ?

BINARY (base 2) NUMBERIC SYSTEM

1 2

3 4

2

5

Computer

128 64 32 16 8 4 2 1

with this, you can decode any binary number (up to 255 [28] decimal)

10010010 = ?

128 64 32 16 8 4 2 1
1 0 0 1 0 0 1 0

6

Computer

128 64 32 16 8 4 2 1

with this, you can decode any binary number (up to 255 [28] decimal)

10010010 = 146

128 64 32 16 8 4 2 1
1 0 0 1 0 0 1 0

= 128 + 0 + 0 + 16 + 0 + 0 + 2 + 0

= 146

7

American Standard Code for Information Interchange (ASCII)

Binary representation of characters:

UNICODE: UTF-8 8 bits = 27= 128 possible characters.

8

UNICODE (UTF-8 UTF-16 UTF-32)

Binary representation of characters:

UNICODE: UTF-16 16 bits = 215 = 1,112,064 possible characters
UTF-32 32 bits = 232 = 2,147,483,648 possible characters

UNICODE is a computing industry standard for the consistent encoding,
representation, and handling of text expressed in most of the world’s writing systems.

5 6

7 8

3

9

Binary Representation

END OF PART 1

10

BINARY (base 2) NUMBERIC SYSTEM

Double Precision Floating Point Form
– fixed point model using Scientific Notation to represent values (64 bit form)

Single Precision Floating Point Form
– fixed point model using Scientific Notation to represent values (32 bit form)

Two’s Compliment Form (2’s compliment)
– negative represented by adding the values of 1 to the compliment of the
positive number

One’s Compliment Form (1’s compliment)
– negative represented by the compliment (flipping bits) of the positive number

Sign-Magnitude Form
– signed (positive and negative) whole numbers

Magnitude Form
– unsigned (positive only) whole numbers

11

BINARY (base 2) NUMBERIC SYSTEM

12

BINARY (base 2) NUMBERIC SYSTEM

Range:
0 – 255 (256)
Range:
0 – 255 (256)

9 10

11 12

4

13

BINARY (base 2) NUMBERIC SYSTEM

Range:
-127 – 127 (255)
Range:
-127 – 127 (255)

14

BINARY (base 2) NUMBERIC SYSTEM

Range:
-127 – 127 (255)
Range:
-127 – 127 (255)

15

BINARY (base 2) NUMBERIC SYSTEM

Range:
-128 – 127 (256)
Range:
-128 – 127 (256)

16

Binary Representation

END OF PART 2

13 14

15 16

5

17

BINARY (base 2) NUMBERIC SYSTEM

Fixed Point Form

method for storing real (fractional) numbers
for example: 18.375

18 -> whole number
375 -> fractional part

– can use negative powers of two
remember: 23 -> 8

2-3 -> 0.125

24 23 22 21 20 2-1 2-2 2-3
16 8 4 2 1 0.5 0.25 0.125

1 0 0 1 0 0 1 1 -> 18.375

can be written as: 10010.011 (this is FIXED POINT MODEL)
limited range of values due to forced position

18

BINARY (base 2) NUMBERIC SYSTEM

Floating Point Form

remember (in Decimal (base 10) )
18.375 = 1.8375 x 101 -> move it one (1) place

123.456 = 1.23456 x 102 -> move it two (2) places

we can do the same thing in Binary:
10010.011 = 1.0010011 x 24 -> move it four (4) places

this allows for FLOATING POINT representation

=> ignore the 2 (always present – assumed)
=> ignore the leading mantissa (number to the left of decimal)

just need to store the sign (- or +), the fractional part and the exponent

8 bits does not give enough to play with: assume 32 bits (4 bytes)
– store exponent using 8 bits – store fraction using 23 bits

19

BINARY (base 2) NUMBERIC SYSTEM

Single Precision Floating Point Form

bit: 1 8 23
sign exponent (e) fraction (f)

position 31 30 … 23 22 … 0

20

BINARY (base 2) NUMBERIC SYSTEM

Double Precision Floating Point Form

bit: 1 11 53
sign exponent (e) fraction (f)

position 63 62 … 53 52 … 0

17 18

19 20

6

21

Binary Representation

END OF PART 3

22

TYPES in C

ALL DATA IS STORED AS A TYPE

characters – 1 byte
short – 2 bytes
int – 4 bytes
float – 4 bytes
double -8 bytes

23

C’s built-in data types that are similar to ones in Java:

C’s built-in data types that do not have an equivalent in Java:

24

C is a HEAVILY TYPED language
computer stores ons and offs
the ons are 1 and offs are 0

BUT ! what data does this sequence of ons and offs represent ????

Individual bits

8 bits == 1 byte

0 1 0 0 0 0 1 0 0 0 1 1 1 1 1 1 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 1

21 22

23 24

7

25

C is a HEAVILY TYPED language
computer stores ons and offs
the ons are 1 and offs are 0

BUT ! what data does this sequence of ons and offs represent ????

4 bytes [ 1 byte = 8 bits ]

0 1 0 0 0 0 1 0 0 0 1 1 1 1 1 1 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 1

charB : ? : s : Q

char
*very

short
integer

66 : 63 : 115 : 81

26

C is a HEAVILY TYPED language
computer stores ons and offs
the ons are 1 and offs are 0

BUT ! what data does this sequence of ons and offs represent ????

2 words [ 1 word = 16 bits ] [ 1 word = 2 bytes ]

0 1 0 0 0 0 1 0 0 0 1 1 1 1 1 1 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 1

short16,959 : 259,521

short
*UTF-16

㽂 : 关

27

C is a HEAVILY TYPED language
computer stores ons and offs
the ons are 1 and offs are 0

BUT ! what data does this sequence of ons and offs represent ????

1 double word [ 1 double word = 32 bits ] [ 1 double word = 4 bytes ]

0 1 0 0 0 0 1 0 0 0 1 1 1 1 1 1 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 1

integer1,111,454,545

float47.862613677978515625

28

C is a HEAVILY TYPED language
computer stores ons and offs
the ons are 1 and offs are 0

BUT ! what data does this sequence of ons and offs represent ????

1 quad word [ 1 quad word = 64 bits ] [ 1 quad word = 8 bytes ]

11010011 10001001 01011001 00101101 01000010 00111111 01110011 01010001

long-3,203,931,608,977,542,319

double-2.6437373164653993971898000075 E94

25 26

27 28

8

29

Binary Representation

END OF PART 4

30

#include

int main(int argc, char *argv[])
{

char a; /* 1 byte */
int b; /* 4 bytes */
float c; /* 4 bytes */
double d; /* 8 bytes */

a = ‘K’;
b = 37;
c = 2.5;
d = 75.3;

printf( “1st value of a is : %c \n” , a );
printf( “2nd value of b is : %d \n” , b );
printf( “3rd value of c is : %f \n” , c );
printf( “4rd value of d is : %lf \n” , d );

return 0 ;
}

SIMPLE C PROGRAM

OUTPUT:
1st value of a is : K
2nd value of b is : 37
3rd value of c is : 2.50000000
4th value of d is : 75.50000000

Variable declaration

Variable definition

31

TYPES in C

ALL DATA IS STORED AS A TYPE

characters – 1 byte
short – 2 bytes
int – 4 bytes
float – 4 bytes
double -8 bytes

For a computer to perform an arithmetic operation:

– the operands must usually be of the same size
i.e. (the same number of bits)

and

– be stored in the same way.
i.e. (int and float are both 4 bytes, but different usage of the 1’s and 0’s)

32

TYPES in C

ALL DATA IS STORED AS A TYPE

MIXED USE:

for example: adding an
int (4 bytes – double precision)

double (8 bytes floating point)

computer MUST convert to one type of data representation.

in C, the computer will always convert to the more complex representation
(variable promotion)

29 30

31 32

9

33

TYPES in C

ALL DATA IS STORED AS A TYPE

When operands of different types are mixed in expressions,
the C compiler may have to generate instructions that
change the types of some operands
so that hardware will be able to evaluate the expression.

If we add a 16-bit short and a 32-bit int,
the compiler will arrange for the short value to be converted to 32 bits.

If we add an int and a float,
the compiler will arrange for the int to be converted to float format.

IMPLICIT CONVERSION

34

TYPES in C

Generally takes place when in an expression more than one data type is present.

In such condition type conversion (type promotion) takes place to avoid lose of data.

All the data types of the variables are upgraded to the data type of
the variable with largest data type.

IMPLICIT CONVERSION

35

TYPES in C

// An example of implicit conversion
#include
int main()
{

int x = 10; // integer x
char y = ‘a’; // character c

// y implicitly converted to int.
// value of ‘a’ is 97
x = x + y;

// x is implicitly converted to float
float z = x + 1.0;

printf(“x = %d, z = %f”, x, z);
return 0;

}

IMPLICIT CONVERSION

implicit conversion is automatic
it requires no user intervention

36

TYPES in C

TOP to BOTTOM
(anything above will converted to the
type below it)

IMPLICIT CONVERSION

char ta = ‘b’;
long tb = 343437;
long tc;
double td;

tc = ta + tb;

td = tc / ta;

printf(“value of ta: %c\n”,ta);
printf(“value of ta: %d\n”,ta);
printf(“value of tb: %d\n”,tb);
printf(“value of tc: %d\n”,tc);
printf(“value of td: %lf\n”,td);
printf(“\n\n”);

value of ta: b
value of ta: 98
value of tb: 343437
value of tc: 343535
value of td: 3505.00000000000

33 34

35 36

10

char c;
short int s;
int i;
unsigned int u;
long int l;
unsigned long int ul;
float f;
double d;
long double ld;

i = i + c; /* c is converted to int */
i = i + s; /* s is converted to int */
u = u + i; /* i is converted to unsigned int */
l = l + u; /* u is converted to long int */
ul = ul + l; /* l is converted to unsigned long int */
f = f + ul; /* ul is converted to float */
d = d + f; /* f is converted to double */
ld = ld + d; /* d is converted to long double */

37

TYPES in C

IMPLICIT CONVERSION

38

TYPES in C

ALL DATA IS STORED AS A TYPE

When operands of different types are mixed in expressions,
the C compiler may have to generate instructions that
change the types of some operands
so that hardware will be able to evaluate the expression.

EXPLICIT CONVERSION

Basically, the programmer forces an expression to be of a specific type.

Explicit type conversion is also called type casting.

The general format of explicit type conversion is as follows:

(data_type)(expression);

39

TYPES in C

notice we are forcing the computer to cast
a higher complexity type (double)
to a lower complexity (int)

EXPLICIT CONVERSION

// C program to demonstrate explicit type casting
#include

int main()
{

double x = 1.2;

// Explicit conversion from double to int
int sum = (int)x + 1;

printf(“sum = %d”, sum);

return 0;
}

40

TYPES in C

Casts are sometimes necessary to avoid overflow:

long i;
int j = 1000;

i = j * j; /* overflow may occur */
Using a cast avoids the problem:

i = (long) j * j;
The statement

i = (long) (j * j); /*** WRONG ***/

wouldn’t work, since the overflow would already have occurred by the time of the cast.

EXPLICIT CONVERSIONC regards ( type-name ) as a unary operator.
Unary operators have higher precedence than
binary operators,

so the compiler interprets

(float) dividend / divisor
as

((float) dividend) / divisor

37 38

39 40

11

41

Binary Representation

END OF PART 5

42

TYPE DEFINITIONS in C

Bool can now be used in the same way as the built-in type names.
Example:

BOOL flag; /* same as int flag; */

The #define directive can be used to create a “Boolean type” macro:

#define BOOL int

There’s a better way using a feature known as a type definition:

typedef int BOOL;

typedef (known data type) (alias to be used instead of)

is nothing more than creating an ‘alias’

typedef float Dollars // Dollars now is an alias for float

Dollars cash_in, cash_out;

// is more informative than float cash_in, cash_out;

43

TYPE DEFINITIONS in C

Type definitions can also make a program easier to modify.
To redefine Dollars as double, only the type definition need be changed:

typedef double Dollars;

Without the type definition, we would need to locate all float variables that store
dollar amounts and change their declarations.

Type definitions are an important tool for writing portable programs.
One of the problems with moving a program from one computer to another is that

types may have different ranges on different machines.

If i is an int variable, an assignment like

i = 100000;
is fine on a machine with 32-bit integers, but fails on a machine with 16-bit integers.

44

Binary Representation

END OF PART 6

41 42

43 44