Data Structures and Algorithm Analysis in C (2nd Edition)
| | Algorithms and Data Structures in C++ by Alan Parker CRC Press, CRC Press LLC ISBN: 0849371716 Pub Date: 08/01/93 |
| Previous | Table of Contents | Next |
1.2.3 Examples
This section presents examples of IEEE 32-bit and 64-bit floating point representations. Converting 100.5 to IEEE 32-bit notation is demonstrated in Example 1.1.
Determining the value of an IEEE 64-bit number is shown in Example 1.2. In many cases for problems as in Example 1.1 the difficulty lies in the actual conversion from decimal to binary. The next section presents a simple methodology for such a conversion.
1.2.4 Conversion from Decimal to Binary
This section presents a simple methodology to convert a decimal number, A, to its corresponding binary representation. For the sake of simplicity, it is assumed the number satisfies
in which case we are seeking the ak such that
The simple procedure is illustrated in Code List 1.12. The C Code performing the decimal to binary conversion is shown in Code List 1.13. The output of the program is shown in Code List 1.14. This program illustrates the use of the default value. When a variable is declared as z is by data z, z is assigned 0.0 and precision is assigned 32. This can be seen as in the program z.prec() is never called and the output results in 32 bits of precision. The paper conversion for 0.4 is illustrated in Example 1.3.
1.3 Character FormatsASCII
To represent keyboard characters, a standard has been adopted to ensure compatibility across many different machines. The most widely used standard is the ASCII (American Standard Code for Information Interchange) character set. This set has a one byte format and is shown in Table 1.8. It allows for 256 distinct characters and specifies the first 128. The lower ASCII characters are control characters which were derived from their common use in earlier machines.Although the ASCII standard is widely used, different operating systems use different file formats to represent data, even when the data files contain only characters. Two of the most popular systems, DOS and Unix differ in their file format. For example, the text file shown in Table 1.9 has a DOS format shown in Table 1.10 and a Unix format shown in Table 1.11. Notice that the DOS file use a carriage return, cr, followed by a new line, nl, while the Unix file uses only a new line. As a result Unix text files will be smaller than DOS text files. In the DOS and Unix tables, underneath each character is its ASCII representation in hex. The numbering on the left of each table is the offset in octal of the line in the file.
Code List 1.12 Decimal to Binary Conversion
Code List 1.13 Decimal to Conversion C++ Program
Code List 1.14 Output of Program in Code List 1.13
| ASCII Listing | |||||||
|---|---|---|---|---|---|---|---|
| oo nul08 bs10 dle18 can20 sp28 (30 038 840 @48 H50 P58 X6068 h70 p78 x | 01 soh09 ht11 dc119 em21 !29 )31 139 941 A49 I51 Q59 Y61 a69 i71 q79 y | 02 stx0a nl12 dc21a sub22 2a *32 23a :42 B4a J52 R5a Z62 b6a j72 r7a z | 03 etx0b vt13 dc31b esc23 #2b +33 33b ;43 C4b K53 S5b [63 c6b k73 s7b { | 04 eot0c np14 dc41c fs24 $2c ,34 43c <44 D4c L54 T5c \64 d6c l74 t7c | | 05 enq0d cr15 nak1d gs25 %2d -35 53d =45 E4d M55 U5d ]65 e6d m75 u7d } | 06 ack0e so16 syn1e rs26 &2e .36 63e >46 F4e N56 V5e ^66 f6e n76 v7e ~ | 07 bel0f si17 etb1f us27 2f /37 73f ?47 G4f O57 W5f _67 g6f o77 w7f del |
| Test File |
|---|
| This is a test fileWe will look at this file under Unix and DOS |
| Previous | Table of Contents | Next |
Copyright © CRC Press LLC