1 0 0 1 1 0
1 1 1 0 1 0
To test out the derivation for calculating the 2’s complement of a number derived in Section
1.1.3 a program to calculate the negative of a number is shown in Code List 1.7. The output of
the program is shown in Code List 1.8. Problem 1.11 investigates the output of the program.
Code List 1.7 Testing the Binary Operators in C++
Code List 1.8 Output of Program in Code List 1.7
A program demonstrating one of the most important uses of the OR operator, |, is shown in Code
List 1.9. The output of the program is shown in Code List 1.10. Figure 1.1 demonstrates the
value of x for the program. The eight attributes are packed into one character. The character field
can hold 256 = 2
8
combinations handling all combinations of each attribute taking on the value
ON or OFF. This is the most common use of the OR operators. For a more detailed example
consider the file operation command for opening a file. The file definitions are defined in
<iostream.h> by BORLAND C++ as shown in Table 1.7.
Figure 1.1 Packing Attributes into One Character
Code List 1.9 Bit Operators
Code List 1.10 Output of Program in Code List 1.9
Table1.7FieldsforFileOperations
inC++Source
enumopen_mode{
in=0x01,//openforreading
out=0x02,//openforwriting
ate=0x04,//seektoeofuponoriginalopen
app=0x08,//appendmode:alladditionsateof
trunc=0x10,//truncatefileifalreadyexists
nocreate=0x20,//open
failsiffiledoesn’texist
noreplace=0x40,//openfailsiffilealreadyexists
binary=0x80//binary(nottext)file
};
A program illustrating another use is shown in Code List 1.11. If the program executes correctly
the output file, test.dat, is created with the string, “This is a test”, placed in it. The file, test.dat, is
opened for writing with ios::out and for truncation with ios::trunc. The two modes are presented
together to the ofstream constructor with the use of the or function.
Code List 1.11 Simple File I/O
Previous TableofContents Next
Copyright © CRC Press LLC
Algorithms and Data Structures in C++
by Alan Parker
CRC Press, CRC Press LLC
ISBN: 0849371716 Pub Date: 08/01/93
Previous
TableofContents Next
1.2.3Examples
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.4ConversionfromDecimaltoBinary
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 a
k
such that
Example 1.1 IEEE 32-Bit Format
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 Formats—ASCII
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.
Example 1.2
Calculating the Value of an IEEE 64-Bit Number
Example 1.3
Converting 0.4 from Decimal to Binary
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
Table1.8ASCIIListingASCIIListing
oonul
08bs
10dle
18can
20sp
28(
300
388
40@
48H
50P
58X
01soh
09ht
11dc1
19em
21!
29)
311
399
41A
49I
51Q
59Y
02stx
0anl
12dc2
1asub
22“
2a
*
322
3a:
42B
4aJ
52R
5aZ
03etx
0bvt
13dc3
1besc
23#
2b+
333
3b;
43C
4bK
53S
5b[
04eot
0cnp
14dc4
1cfs
24$
2c,
344
3c<
44D
4cL
54T
5c\
05enq
0dcr
15
nak
1dgs
25%
2d‐
355
3d=
45E
4dM
55U
5d]
06ack
0eso
16syn
1ers
26&
2e.
366
3e>
46F
4eN
56V
5e^
07bel
0fsi
17etb
1fus
27
‘
2f/
377
3f?
47G
4fO
57W
5f_
60
68h
70p
78x
61a
69i
71q
79y
62b
6aj
72r
7az
63c
6bk
73s
7b{
64d
6cl
74t
7c|
65e
6dm
75u
7d}
66f
6en
76v
7e~
67g
6fo
77w
7fdel
Table1.9TextFileTestFile
Thisisatestfile
WewilllookatthisfileunderUnixandDOS
Previous TableofContents Next
Copyright © CRC Press LLC
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