- •CONTENTS
- •1.1 Introduction
- •1.2 What Is a Computer?
- •1.3 Programs
- •1.4 Operating Systems
- •1.5 Java, World Wide Web, and Beyond
- •1.6 The Java Language Specification, API, JDK, and IDE
- •1.7 A Simple Java Program
- •1.8 Creating, Compiling, and Executing a Java Program
- •1.9 (GUI) Displaying Text in a Message Dialog Box
- •2.1 Introduction
- •2.2 Writing Simple Programs
- •2.3 Reading Input from the Console
- •2.4 Identifiers
- •2.5 Variables
- •2.7 Named Constants
- •2.8 Numeric Data Types and Operations
- •2.9 Problem: Displaying the Current Time
- •2.10 Shorthand Operators
- •2.11 Numeric Type Conversions
- •2.12 Problem: Computing Loan Payments
- •2.13 Character Data Type and Operations
- •2.14 Problem: Counting Monetary Units
- •2.15 The String Type
- •2.16 Programming Style and Documentation
- •2.17 Programming Errors
- •2.18 (GUI) Getting Input from Input Dialogs
- •3.1 Introduction
- •3.2 boolean Data Type
- •3.3 Problem: A Simple Math Learning Tool
- •3.4 if Statements
- •3.5 Problem: Guessing Birthdays
- •3.6 Two-Way if Statements
- •3.7 Nested if Statements
- •3.8 Common Errors in Selection Statements
- •3.9 Problem: An Improved Math Learning Tool
- •3.10 Problem: Computing Body Mass Index
- •3.11 Problem: Computing Taxes
- •3.12 Logical Operators
- •3.13 Problem: Determining Leap Year
- •3.14 Problem: Lottery
- •3.15 switch Statements
- •3.16 Conditional Expressions
- •3.17 Formatting Console Output
- •3.18 Operator Precedence and Associativity
- •3.19 (GUI) Confirmation Dialogs
- •4.1 Introduction
- •4.2 The while Loop
- •4.3 The do-while Loop
- •4.4 The for Loop
- •4.5 Which Loop to Use?
- •4.6 Nested Loops
- •4.7 Minimizing Numeric Errors
- •4.8 Case Studies
- •4.9 Keywords break and continue
- •4.10 (GUI) Controlling a Loop with a Confirmation Dialog
- •5.1 Introduction
- •5.2 Defining a Method
- •5.3 Calling a Method
- •5.4 void Method Example
- •5.5 Passing Parameters by Values
- •5.6 Modularizing Code
- •5.7 Problem: Converting Decimals to Hexadecimals
- •5.8 Overloading Methods
- •5.9 The Scope of Variables
- •5.10 The Math Class
- •5.11 Case Study: Generating Random Characters
- •5.12 Method Abstraction and Stepwise Refinement
- •6.1 Introduction
- •6.2 Array Basics
- •6.3 Problem: Lotto Numbers
- •6.4 Problem: Deck of Cards
- •6.5 Copying Arrays
- •6.6 Passing Arrays to Methods
- •6.7 Returning an Array from a Method
- •6.8 Variable-Length Argument Lists
- •6.9 Searching Arrays
- •6.10 Sorting Arrays
- •6.11 The Arrays Class
- •7.1 Introduction
- •7.2 Two-Dimensional Array Basics
- •7.3 Processing Two-Dimensional Arrays
- •7.4 Passing Two-Dimensional Arrays to Methods
- •7.5 Problem: Grading a Multiple-Choice Test
- •7.6 Problem: Finding a Closest Pair
- •7.7 Problem: Sudoku
- •7.8 Multidimensional Arrays
- •8.1 Introduction
- •8.2 Defining Classes for Objects
- •8.3 Example: Defining Classes and Creating Objects
- •8.4 Constructing Objects Using Constructors
- •8.5 Accessing Objects via Reference Variables
- •8.6 Using Classes from the Java Library
- •8.7 Static Variables, Constants, and Methods
- •8.8 Visibility Modifiers
- •8.9 Data Field Encapsulation
- •8.10 Passing Objects to Methods
- •8.11 Array of Objects
- •9.1 Introduction
- •9.2 The String Class
- •9.3 The Character Class
- •9.4 The StringBuilder/StringBuffer Class
- •9.5 Command-Line Arguments
- •9.6 The File Class
- •9.7 File Input and Output
- •9.8 (GUI) File Dialogs
- •10.1 Introduction
- •10.2 Immutable Objects and Classes
- •10.3 The Scope of Variables
- •10.4 The this Reference
- •10.5 Class Abstraction and Encapsulation
- •10.6 Object-Oriented Thinking
- •10.7 Object Composition
- •10.8 Designing the Course Class
- •10.9 Designing a Class for Stacks
- •10.10 Designing the GuessDate Class
- •10.11 Class Design Guidelines
- •11.1 Introduction
- •11.2 Superclasses and Subclasses
- •11.3 Using the super Keyword
- •11.4 Overriding Methods
- •11.5 Overriding vs. Overloading
- •11.6 The Object Class and Its toString() Method
- •11.7 Polymorphism
- •11.8 Dynamic Binding
- •11.9 Casting Objects and the instanceof Operator
- •11.11 The ArrayList Class
- •11.12 A Custom Stack Class
- •11.13 The protected Data and Methods
- •11.14 Preventing Extending and Overriding
- •12.1 Introduction
- •12.2 Swing vs. AWT
- •12.3 The Java GUI API
- •12.4 Frames
- •12.5 Layout Managers
- •12.6 Using Panels as Subcontainers
- •12.7 The Color Class
- •12.8 The Font Class
- •12.9 Common Features of Swing GUI Components
- •12.10 Image Icons
- •13.1 Introduction
- •13.2 Exception-Handling Overview
- •13.3 Exception-Handling Advantages
- •13.4 Exception Types
- •13.5 More on Exception Handling
- •13.6 The finally Clause
- •13.7 When to Use Exceptions
- •13.8 Rethrowing Exceptions
- •13.9 Chained Exceptions
- •13.10 Creating Custom Exception Classes
- •14.1 Introduction
- •14.2 Abstract Classes
- •14.3 Example: Calendar and GregorianCalendar
- •14.4 Interfaces
- •14.5 Example: The Comparable Interface
- •14.6 Example: The ActionListener Interface
- •14.7 Example: The Cloneable Interface
- •14.8 Interfaces vs. Abstract Classes
- •14.9 Processing Primitive Data Type Values as Objects
- •14.10 Sorting an Array of Objects
- •14.11 Automatic Conversion between Primitive Types and Wrapper Class Types
- •14.12 The BigInteger and BigDecimal Classes
- •14.13 Case Study: The Rational Class
- •15.1 Introduction
- •15.2 Graphical Coordinate Systems
- •15.3 The Graphics Class
- •15.4 Drawing Strings, Lines, Rectangles, and Ovals
- •15.5 Case Study: The FigurePanel Class
- •15.6 Drawing Arcs
- •15.7 Drawing Polygons and Polylines
- •15.8 Centering a String Using the FontMetrics Class
- •15.9 Case Study: The MessagePanel Class
- •15.10 Case Study: The StillClock Class
- •15.11 Displaying Images
- •15.12 Case Study: The ImageViewer Class
- •16.1 Introduction
- •16.2 Event and Event Source
- •16.3 Listeners, Registrations, and Handling Events
- •16.4 Inner Classes
- •16.5 Anonymous Class Listeners
- •16.6 Alternative Ways of Defining Listener Classes
- •16.7 Problem: Loan Calculator
- •16.8 Window Events
- •16.9 Listener Interface Adapters
- •16.10 Mouse Events
- •16.11 Key Events
- •16.12 Animation Using the Timer Class
- •17.1 Introduction
- •17.2 Buttons
- •17.3 Check Boxes
- •17.4 Radio Buttons
- •17.5 Labels
- •17.6 Text Fields
- •17.7 Text Areas
- •17.8 Combo Boxes
- •17.9 Lists
- •17.10 Scroll Bars
- •17.11 Sliders
- •17.12 Creating Multiple Windows
- •18.1 Introduction
- •18.2 Developing Applets
- •18.3 The HTML File and the <applet> Tag
- •18.4 Applet Security Restrictions
- •18.5 Enabling Applets to Run as Applications
- •18.6 Applet Life-Cycle Methods
- •18.7 Passing Strings to Applets
- •18.8 Case Study: Bouncing Ball
- •18.9 Case Study: TicTacToe
- •18.10 Locating Resources Using the URL Class
- •18.11 Playing Audio in Any Java Program
- •18.12 Case Study: Multimedia Animations
- •19.1 Introduction
- •19.2 How is I/O Handled in Java?
- •19.3 Text I/O vs. Binary I/O
- •19.4 Binary I/O Classes
- •19.5 Problem: Copying Files
- •19.6 Object I/O
- •19.7 Random-Access Files
- •20.1 Introduction
- •20.2 Problem: Computing Factorials
- •20.3 Problem: Computing Fibonacci Numbers
- •20.4 Problem Solving Using Recursion
- •20.5 Recursive Helper Methods
- •20.6 Problem: Finding the Directory Size
- •20.7 Problem: Towers of Hanoi
- •20.8 Problem: Fractals
- •20.9 Problem: Eight Queens
- •20.10 Recursion vs. Iteration
- •20.11 Tail Recursion
- •APPENDIXES
- •INDEX
APPENDIXES
Appendix A
Java Keywords
Appendix B
The ASCII Character Set
Appendix C
Operator Precedence Chart
Appendix D
Java Modifiers
Appendix E
Special Floating-Point Values
Appendix F
Number Systems
707
This page intentionally left blank
APPENDIX A
Java Keywords
The following fifty keywords are reserved for use by the Java language:
abstract |
double |
int |
super |
assert |
else |
interface |
switch |
boolean |
enum |
long |
synchronized |
break |
extends |
native |
this |
byte |
for |
new |
throw |
case |
final |
package |
throws |
catch |
finally |
private |
transient |
char |
float |
protected |
try |
class |
goto |
public |
void |
const |
if |
return |
volatile |
continue |
implements |
short |
while |
default |
import |
static |
|
do |
instanceof |
strictfp* |
|
The keywords goto and const are C++ keywords reserved, but not currently used, in Java. This enables Java compilers to identify them and to produce better error messages if they appear in Java programs.
The literal values true, false, and null are not keywords, just like literal value 100. However, you cannot use them as identifiers, just as you cannot use 100 as an identifier.
assert is a keyword added in JDK 1.4 and enum is a keyword added in JDK 1.5.
*The strictfp keyword is a modifier for method or class to use strict floating-point calculations. Floatingpoint arithmetic can be executed in one of two modes: strict or nonstrict. The strict mode guarantees that the evaluation result is the same on all Java Virtual Machine implementations. The nonstrict mode allows intermediate results from calculations to be stored in an extended format different from the standard IEEE floatingpoint number format. The extended format is machine-dependent and enables code to be executed faster. However, when you execute the code using the nonstrict mode on different JVMs, you may not always get precisely the same results. By default, the nonstrict mode is used for floating-point calculations. To use the strict mode in a method or a class, add the strictfp keyword in the method or the class declaration. Strict floatingpoint may give you slightly better precision than nonstrict floating-point, but the distinction will only affect some applications. Strictness is not inherited; that is, the presence of strictfp on a class or interface declaration does not cause extended classes or interfaces to be strict.
709
APPENDIX B
The ASCII Character Set
Tables B.1 and B.2 show ASCII characters and their respective decimal and hexadecimal codes. The decimal or hexadecimal code of a character is a combination of its row index and column index. For example, in Table B.1, the letter A is at row 6 and column 5, so its decimal equivalent is 65; in Table B.2, letter A is at row 4 and column 1, so its hexadecimal equivalent is 41.
710
TABLE B.1 ASCII Character Set in the Decimal Index
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
|
|
|
|
|
|
|
|
|
|
|
0 |
nul |
soh |
stx |
etx |
eot |
enq |
ack |
bel |
bs |
ht |
1 |
nl |
vt |
ff |
cr |
so |
si |
dle |
dcl |
dc2 |
dc3 |
2 |
dc4 |
nak |
syn |
etb |
can |
em |
sub |
esc |
fs |
gs |
3 |
rs |
us |
sp |
! |
” |
# |
$ |
% |
& |
’ |
4 |
( |
) |
* |
+ |
, |
- |
. |
/ |
0 |
1 |
5 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
: |
; |
6 |
6 |
= |
7 |
? |
@ |
A |
B |
C |
D |
E |
7 |
F |
G |
H |
I |
J |
K |
L |
M |
N |
O |
8 |
P |
Q |
R |
S |
T |
U |
V |
W |
X |
Y |
9 |
Z |
[ |
O |
] |
¿ |
- |
‘ |
a |
b |
c |
10 |
d |
e |
f |
g |
h |
i |
j |
k |
l |
m |
11 |
n |
o |
p |
q |
r |
s |
t |
u |
v |
w |
12 |
x |
y |
z |
5 |
ƒ |
6 |
' |
del |
|
|
|
|
|
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|
TABLE B.2 ASCII Character Set in the Hexadecimal Index
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
A |
B |
C |
D |
E |
F |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
0 |
nul |
soh |
stx |
etx |
eot |
enq |
ack |
bel |
bs |
ht |
nl |
vt |
ff |
cr |
so |
si |
1 |
dle |
dcl |
dc2 |
dc3 |
dc4 |
nak |
syn |
etb |
can |
em |
sub |
esc |
fs |
gs |
rs |
us |
2 |
sp |
! |
” |
# |
$ |
% |
& |
’ |
( |
) |
* |
+ |
, |
- |
. |
/ |
3 |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
: |
; |
6 |
= |
7 |
? |
4 |
@ |
A |
B |
C |
D |
E |
F |
G |
H |
I |
J |
K |
L |
M |
N |
O |
5 |
P |
Q |
R |
S |
T |
U |
V |
W |
X |
Y |
Z |
[ |
O |
] |
¿ |
- |
6 |
‘ |
a |
b |
c |
d |
e |
f |
g |
h |
i |
j |
k |
l |
m |
n |
o |
7 |
p |
q |
r |
s |
t |
u |
v |
w |
x |
y |
z |
5 |
ƒ |
6 |
' |
del |
|
|
|
711
APPENDIX C
Operator Precedence Chart
The operators are shown in decreasing order of precedence from top to bottom. Operators in the same group have the same precedence, and their associativity is shown in the table.
Operator |
Name |
Associativity |
() |
Parentheses |
Left to right |
() |
Function call |
Left to right |
[] |
Array subscript |
Left to right |
. |
Object member access |
Left to right |
++ |
Postincrement |
Right to left |
–– |
Postdecrement |
Right to left |
|
|
|
++ |
Preincrement |
Right to left |
–– |
Predecrement |
Right to left |
+ |
Unary plus |
Right to left |
– |
Unary minus |
Right to left |
! |
Unary logical negation |
Right to left |
|
|
|
(type) |
Unary casting |
Right to left |
new |
Creating object |
Right to left |
|
|
|
* |
Multiplication |
Left to right |
/ |
Division |
Left to right |
% |
Remainder |
Left to right |
|
|
|
+ |
Addition |
Left to right |
– |
Subtraction |
Left to right |
|
|
|
<< |
Left shift |
Left to right |
>> |
Right shift with sign extension |
Left to right |
>>> |
Right shift with zero extension |
Left to right |
|
|
|
< |
Less than |
Left to right |
<= |
Less than or equal to |
Left to right |
> |
Greater than |
Left to right |
|
|
|
>= |
Greater than or equal to |
Left to right |
instanceof |
Checking object type |
Left to right |
|
|
|
712
Appendix C 713
Operator |
Name |
Associativity |
== |
Equal comparison |
Left to right |
!= |
Not equal |
Left to right |
|
|
|
& |
(Unconditional AND) |
Left to right |
|
|
|
^ |
(Exclusive OR) |
Left to right |
|
|
|
| |
(Unconditional OR) |
Left to right |
|
|
|
&& |
Conditional AND |
Left to right |
|
|
|
|| |
Conditional OR |
Left to right |
|
|
|
?: |
Ternary condition |
Right to left |
|
|
|
= |
Assignment |
Right to left |
+= |
Addition assignment |
Right to left |
–= |
Subtraction assignment |
Right to left |
*= |
Multiplication assignment |
Right to left |
/= |
Division assignment |
Right to left |
%= |
Remainder assignment |
Right to left |
|
|
|
APPENDIX D
Java Modifiers
Modifiers are used on classes and class members (constructors, methods, data, and class-level blocks), but the final modifier can also be used on local variables in a method. A modifier that can be applied to a class is called a class modifier. A modifier that can be applied to a method is called a method modifier. A modifier that can be applied to a data field is called a data modifier. A modifier that can be applied to a class-level block is called a block modifier. The following table gives a summary of the Java modifiers.
Modifier |
class |
constructor |
method |
data |
block |
Explanation |
|
|
|
|
|
|
|
(default)* |
|
|
|
|
|
A class, constructor, method, |
|
|
|
|
|
|
or data field is |
|
|
|
|
|
|
visible in this package. |
public |
|
|
|
|
|
A class, constructor, method, |
|
|
|
|
|
|
or data field is |
|
|
|
|
|
|
visible to all the programs |
|
|
|
|
|
|
in any package. |
private |
|
|
|
|
|
A constructor, method or data |
|
|
|
|
|
|
field is only visible in this |
|
|
|
|
|
|
class. |
protected |
|
|
|
|
|
A constructor, method or data |
|
|
|
|
|
|
field is visible in this package |
|
|
|
|
|
|
and in subclasses |
|
|
|
|
|
|
of this class in any package. |
static |
|
|
|
|
|
Define a class method, or a |
|
|
|
|
|
|
class data field or a static |
|
|
|
|
|
|
initialization block. |
final |
|
|
|
|
|
A final class cannot be |
|
|
|
|
|
|
extended. A final method can- |
|
|
|
|
|
|
not be modified in a |
|
|
|
|
|
|
subclass. A final data field |
|
|
|
|
|
|
is a constant. |
abstract |
|
|
|
|
|
An abstract class must be |
|
|
|
|
|
|
extended. An abstract method |
|
|
|
|
|
|
must be implemented in a |
|
|
|
|
|
|
concrete subclass. |
native |
|
|
|
|
|
A native method indicates |
|
|
|
|
|
|
that the method is |
|
|
|
|
|
|
implemented using a |
|
|
|
|
|
|
language other than Java. |
|
|
|
|
|
|
|
*Default access has no modifier associated with it. For example: class Test {}
714
Appendix D 715
Modifier |
class constructor |
method data |
block |
Explanation |
|
|
|
|
|
synchronized |
|
|
|
Only one thread at a time can |
|
|
|
|
execute this method. |
strictfp |
|
|
|
Use strict floating-point |
|
|
|
|
calculations to guarantee that |
|
|
|
|
the evaluation result is the |
|
|
|
|
same on all JVMs. |
transient |
|
|
|
Mark a nonserializable |
|
|
|
|
instance data field. |
APPENDIX E
Special Floating-Point Values
Dividing an integer by zero is invalid and throws ArithmeticException, but dividing a floating-point value by zero does not cause an exception. Floating-point arithmetic can overflow to infinity if the result of the operation is too large for a double or a float, or underflow to zero if the result is too small for a double or a float. Java provides the special floating-point values POSITIVE_INFINITY, NEGATIVE_INFINITY, and NaN (Not a Number) to denote these results. These values are defined as special constants in the Float class and the Double class.
If a positive floating-point number is divided by zero, the result is POSITIVE_INFINITY. If a negative floating-point number is divided by zero, the result is NEGATIVE_INFINITY. If a floating-point zero is divided by zero, the result is NaN, which means that the result is undefined mathematically. The string representation of these three values are Infinity, -Infinity, and NaN. For example,
System.out.print(1.0 / 0); // Print Infinity
System.out.print(–1.0 / 0); // Print –Infinity
System.out.print(0.0 / 0); // Print NaN
These special values can also be used as operands in computations. For example, a number divided by POSITIVE_INFINITY yields a positive zero. Table E.1 summarizes various com-
binations of the /, *, |
%, +, and – operators. |
|
|
|
||
TABLE E.1 Special Floating-Point Values |
|
|
|
|
||
|
|
|
|
|
|
|
x |
y |
x/y |
x*y |
x%y |
x + y |
x - y |
|
|
|
|
|
|
|
Finite |
± 0.0 |
± q |
± 0.0 |
NaN |
Finite |
Finite |
Finite |
± q |
± 0.0 |
± 0.0 |
x |
± q |
q |
± 0.0 |
± 0.0 |
NaN |
± 0.0 |
NaN |
± 0.0 |
± 0.0 |
± q |
Finite |
± q |
± 0.0 |
NaN |
± q |
± q |
± q |
± q |
NaN |
± 0.0 |
NaN |
± q |
q |
± 0.0 |
± q |
± 0.0 |
NaN |
± 0.0 |
± q |
± 0.0 |
NaN |
Any |
NaN |
NaN |
NaN |
NaN |
NaN |
Any |
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
Note
If one of the operands is NaN, the result is NaN.
716
APPENDIX F
Number Systems
1 Introduction
Computers use binary numbers internally, because computers are made naturally to store and |
binary numbers |
||||
process 0s and 1s. The binary number system has two digits, 0 and 1. A number or character |
|
||||
is stored as a sequence of 0s and 1s. Each 0 or 1 is called a bit (binary digit). |
|
||||
In our daily life we use decimal numbers. When we write a number such as 20 in a pro- |
decimal numbers |
||||
gram, it is assumed to be a decimal number. Internally, computer software is used to convert |
|
||||
decimal numbers into binary numbers, and vice versa. |
|
||||
We write computer programs using decimal numbers. However, to deal with an operating |
|
||||
system, we need to reach down to the “machine level” by using binary numbers. Binary num- |
|
||||
bers tend to be very long and cumbersome. Often hexadecimal numbers are used to abbrevi- |
|
||||
ate them, with each hexadecimal digit representing four binary digits. The hexadecimal |
hexadecimal number |
||||
number system has 16 digits: 0–9, A–F. The letters A, B, C, D, E, and F correspond to the dec- |
|
||||
imal numbers 10, 11, 12, 13, 14, and 15. |
|
||||
The digits in the decimal number system are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. A decimal num- |
|
||||
ber is represented by a sequence of one or more of these digits. The value that each digit rep- |
|
||||
resents depends on its position, which denotes an integral power of 10. For example, the digits |
|
||||
7, 4, 2, and 3 in decimal number 7423 represent 7000, 400, 20, and 3, respectively, as shown |
|
||||
below: |
|
|
|||
|
|
|
|
= 7 * 103 + 4 * 102 + 2 * 101 + 3 * 100 |
|
7 4 2 3 |
|
||||
|
|
|
|
|
|
103 102 101 100 |
|
= 7000 + 400 + 20 + 3 = 7423 |
|
||
The decimal number system has ten digits, and the position values are integral powers of 10. |
|
||||
We say that 10 is the base or radix of the decimal number system. Similarly, since the binary |
base |
||||
number system has two digits, its base is 2, and since the hex number system has 16 digits, its |
radix |
||||
base is 16. |
|
|
|||
If 1101 is a binary number, the digits 1, 1, 0, and 1 represent 1 * 23, 1 * 22, 0 * 21, and |
|
1* 20, respectively:
1 1 0 1 = 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20
23 22 21 20 = 8 + 4 + 0 + 1 = 13
If 7423 is a hex number, the digits 7, 4, 2, and 3 represent 7 * 163, 4 * 162, 2 * 161, and
3* 160, respectively:
7 4 2 3 = 7 * 163 + 4 * 162 + 2 * 161 + 3 * 160
163 162 161 160 = 28672 + 1024 + 32 + 3 = 29731
717
718 Appendix F
2 Conversions Between Binary and Decimal Numbers
binary to decimal |
Given a binary number bnbn- 1bn- 2 Á b2b1b0, the equivalent decimal value is |
|
||||
|
bn * 2n + bn- 1 * 2n- 1 + bn- 2 * 2n- 2 + Á + b2 * 22 + b1 * 21 + b0 * 20 |
|||||
|
Here are some examples of converting binary numbers to decimals: |
|
||||
|
Binary |
Conversion Formula |
Decimal |
|||
|
|
|
|
|
|
|
|
10 |
1 * 21 |
+ 0 * 20 |
|
2 |
|
|
1000 |
1 |
* 23 |
+ 0 * 22 |
+ 0 * 21 + 0 * 20 |
8 |
|
10101011 |
1 |
* 27 |
+ 0 * 26 |
+ 1 * 25 + 0 * 24 + 1 * 23 + 0 * 22 + |
171 |
|
|
|
|
|
|
1 * 21 + 1 * 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||
decimal to binary |
To convert a decimal number d to a binary number is to find the bits bn, bn- 1, bn- 2, Á , b2, b1, |
|||||||||||||||||||||||||||||
|
and b0 such that |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
d = bn * 2n + bn- 1 * 2n- 1 + bn- 2 * 2n- 2 + Á + b2 * 22 + b1 * 21 + b0 * 20 |
|||||||||||||||||||||||||||||
|
These bits can be found by successively dividing d by 2 until the quotient is 0. The remainders |
|||||||||||||||||||||||||||||
|
are b0, b1, b2, Á , bn- 2, bn- 1, and bn. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||||||
|
For example, the decimal number 123 is 1111011 in binary. The conversion is done as follows: |
|||||||||||||||||||||||||||||
|
|
|
0 |
|
|
|
1 |
|
3 |
|
|
7 |
|
|
15 |
|
|
30 |
|
61 |
|
|
Quotient |
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||||||||
|
2 |
1 |
|
2 |
3 |
2 |
7 |
2 |
15 |
2 |
30 |
2 |
61 |
2 |
123 |
|
|
|
||||||||||||
|
|
|
0 |
|
|
|
2 |
|
6 |
|
|
14 |
|
|
30 |
|
|
60 |
|
122 |
|
|
|
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Remainder |
|
|
|
1 |
|
|
|
1 |
|
1 |
|
|
1 |
|
|
0 |
|
|
1 |
|
1 |
|
|
||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
b6 |
|
|
b5 |
|
b4 |
|
|
b3 |
|
|
b2 |
|
|
b1 |
|
b0 |
|
|
|
Tip
The Windows Calculator, as shown in Figure F.1, is a useful tool for performing number conversions. To run it, choose Programs, Accessories, and Calculator from the Start button, then under
View select Scientific.
Decimal Binary
Hex
FIGURE F.1 You can perform number conversions using the Windows Calculator.
Appendix F 719
3 Conversions Between Hexadecimal and Decimal
Numbers
Given a hexadecimal number hnhn- 1hn- 2 Á h2h1h0, the equivalent decimal value is |
hex to decimal |
||
hn * 16n + hn- 1 * 16n- 1 + hn- 2 * 16n- 2 + Á + h2 * 162 + h1 * 161 + h0 * 160 |
|
||
Here are some examples of converting hexadecimal numbers to decimals: |
|
|
|
Hexadecimal |
Conversion Formula |
Decimal |
|
7F |
7 * 161 + 15 * 160 |
127 |
|
FFFF |
15 * 163 + 15 * 162 + 15 * 161 + 15 * 160 |
65535 |
|
431 |
4 * 162 + 3 * 161 + 1 * 160 |
1073 |
|
To convert a decimal number d to a hexadecimal number is to find the hexadecimal digits decimal to hex hn, hn- 1, hn- 2, Á , h2, h1, and h0 such that
d= hn * 16n + hn- 1 * 16n- 1 + hn- 2 * 16n- 2 + Á + h2 * 162
+h1 * 161 + h0 * 160
These numbers can be found by successively dividing d by 16 until the quotient is 0. The re-
mainders are h0, h1, h2, Á , hn- 2, hn- 1, and hn.
For example, the decimal number 123 is 7B in hexadecimal. The conversion is done as follows:
07 Quotient
16 7 16 123
0112
711 Remainder
h1 h0
4 Conversions Between Binary and Hexadecimal Numbers
To convert a hexadecimal to a binary number, simply convert each digit in the hexadecimal |
hex to binary |
||||||||||
number into a four-digit binary number, using Table F.1. |
|
||||||||||
For example, the hexadecimal number 7B is 1111011, where 7 is 111 in binary, and B is |
|
||||||||||
1011 in binary. |
|
|
|
|
|
|
|
|
|
||
To convert a binary number to a hexadecimal, convert every four binary digits from right to |
|
||||||||||
left in the binary number into a hexadecimal number. |
binary to hex |
||||||||||
For example, the binary number 1110001101 is 38D, since 1101 is D, 1000 is 8, and 11 is |
|
||||||||||
3, as shown below. |
|
|
|
|
|
|
|
|
|
||
1 |
1 1 0 0 0 1 1 0 1 |
|
|||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
8 |
D |
720 Appendix F
TABLE F.1 Converting Hexadecimal to Binary
Hexadecimal |
Binary |
Decimal |
|
|
|
0 |
0000 |
0 |
1 |
0001 |
1 |
2 |
0010 |
2 |
3 |
0011 |
3 |
4 |
0100 |
4 |
5 |
0101 |
5 |
6 |
0110 |
6 |
7 |
0111 |
7 |
8 |
1000 |
8 |
9 |
1001 |
9 |
A |
1010 |
10 |
B |
1011 |
11 |
C |
1100 |
12 |
D |
1101 |
13 |
E |
1110 |
14 |
F |
1111 |
15 |
(Note: Octal numbers are also useful. The octal number system has eight digits, 0 to 7. A decimal number 8 is represented in the octal system as 10.)
REVIEW QUESTIONS
1. Convert the following decimal numbers into hexadecimal and binary numbers.
100; 4340; 2000
2. Convert the following binary numbers into hexadecimal and decimal numbers.
1000011001; 100000000; 100111
3. Convert the following hexadecimal numbers into binary and decimal numbers.
FEFA9; 93; 2000