Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - The relationship between the number of bytes occupied by data types and their range of values in C language
The relationship between the number of bytes occupied by data types and their range of values in C language
Data types in C language can be simply divided into integer and floating-point types. The relationship between the number of bytes occupied and the range of values is actually a problem of integer coding and floating-point coding. The three ways of integer coding are original code, complement and complement. The encoding format of floating-point code is well understood by adopting IEEE754 encoding.

1, integer coding takes character type as an example.

The character type accounts for 1 byte, and ***8 bits are binary bits, so there are 2 8 = 256 encoding ways to arrange the number of combinations. If it represents an unsigned character type, it generally represents 256 digits 0,255.

If it represents a signed char type, if it represents a positive number, and the highest symbol is 0, then the largest positive integer that can be represented is:

0-11111,which is 127 in decimal. Similarly, the symbol 1 represents a negative number. The smallest negative number is

1-000 0000, converted into decimal number is-128, so the range represented by signed char type is [- 128, 127].

Generally speaking, it is assumed that the integer data type occupies n binary digits. If it represents an unsigned integer, the value range is [0,2n-1]; If it represents a signed integer, it is -2 (n- 1), 2 (n- 1)- 1.

2, floating-point type

Floating-point number coding generally adopts the coding rules of IEEE754. This coding format mainly points out the binary bits occupied by the significant bits, exponent and sign bits of floating-point numbers. Simply summarized as:

Format? Length? Sign bit index bit? Mantissa number? Effective digits? Exponential offset? Mantissa description

Single precision 32 1? 8 ? 23 ? 24 127 ? There is a hidden bit.

Double precision 64 1? 1 1 ? 52 ? 53 ? 1023 ? There is a hidden bit.

Extended double precision 80 1? 15 ? 64 ? 64 ? 16383 ? No implied bit

Note: the extended double-precision format has no hidden bits, so its effective digits are the same as the mantissa digits, while both single-precision and double-precision formats have a hidden bit, so its effective digits are more than the mantissa digits 1.

Generally, the range of floating-point numbers is rarely calculated manually. You can use the following program to calculate.

# Contains? & ltstdio.h & gt

typedef? struct? FP_SINGLE

{

Not signed? __int32? Scores? :? 23;

Not signed? __int32? Experience:? 8;

Not signed? __int32? Sign? :? 1;

}? fp _ single

typedef? struct? FP_DOUBLE

{

Not signed? __int64? Scores? :? 52;

Not signed? __int64? Experience:? 1 1;

Not signed? __int64? Sign? :? 1;

}? fp _ double

typedef? struct? Floating-point double precision

{

Not signed? __int64? Scores;

Not signed? __int32? Experience:? 15;

Not signed? __int32? Sign? :? 1;

}? fp _ ex _ double

int? Master ()

{

Floating? x;

Fp _ Single? *? fp_s? =? (fp_single? *)& amp; x;

FP _ s-& gt; Sign? =? 0;

FP _ s-& gt; exp? =? 0xfe

FP _ s-& gt; Scores? =? 0x7fffff

printf? ("floating? Maximum quantity: %le\n ",(double)x); x);

FP _ s-& gt; Sign? =? 0;

FP _ s-& gt; exp? =? 0x 1;

FP _ s-& gt; Scores? =? 0x0

printf? ("floating? Minimum quantity: %le\n ",(double)x); x);

FP _ s-& gt; Sign? =? 0;

FP _ s-& gt; exp? =? 0;

FP _ s-& gt; Scores? =? 0x 1;

printf? ("floating? Minimum weak canonical number: %le\n\n ",(double)x); x);

Double? y;

fp_double? *? fp_d? =? (fp_double? *)& amp; y;

FP _ d-& gt; Sign? =? 0;

FP _ d-& gt; exp? =? 0x7fe

FP _ d-& gt; Scores? =? 0xfffffffffffff

printf? ("Double? Maximum quantity: %le\n "? y);

FP _ d-& gt; Sign? =? 0;

FP _ d-& gt; exp? =? 0x 1;

FP _ d-& gt; Scores? =? 0x0

printf? ("Double? Minimum quantity: %le\n "? y);

FP _ d-& gt; Sign? =? 0;

FP _ d-& gt; exp? =? 0;

FP _ d-& gt; Scores? =? 0x 1;

printf? ("Double? Minimum weak canonical number: %le\n\n ",? y);

Charles? ch[ 10];

fp_ex_double? *? fp_ex_d? =? (fp_ex_double? *)ch;

FP _ ex _ d-& gt; Sign? =? 0;

FP _ ex _ d-& gt; exp? =? 0x 7 FFE;

FP _ ex _ d-& gt; Scores? =? 0xffffffffffffffff

//? I don't know how to output extended double-precision floating-point numbers.

//? But you can track it with od, then find the address of ch[0], and choose? Floating point? 80 is long double precision,

//? You can see the value.

FP _ ex _ d-& gt; Sign? =? 0;

FP _ ex _ d-& gt; exp? =? 0x 1;

FP _ ex _ d-& gt; Scores? =? 0x8000000000000000

FP _ ex _ d-& gt; Sign? =? 0;

FP _ ex _ d-& gt; exp? =? 0;

FP _ ex _ d-& gt; Scores? =? 0x 1;

Return? 0;

}