Programming and Problem Solving with Java

(やまだぃちぅ) #1

Binary Representation of Data


In a computer, data are represented electronically by pulses of electricity. Electric circuits, in
their simplest form, are either on or off. A circuit that is on is typically represented by the num-
ber 1; a circuit that is off is represented by the number 0. Any kind of data can be represented by
combinations of enough 1s and 0s. We simply have to choose which combination represents
each piece of data we are using. For example, we could arbitrarily choose the pattern 1101000110
to represent the name “Java.”
Data represented by 1s and 0s are in binary form. The binary, or base–2, number system uses
only 1s and 0s to represent numbers. (The decimal, or base–10, number system uses the digits 0
through 9.) The word bit(short for binary digit) refers to a single 1 or 0. Thus the pattern
1101000110 has 10 bits. A binary number with 10 bits can represent 2^10 (1,024) different patterns.
A byteis a group of 8 bits; it can represent 2^8 (256) patterns. Inside the computer, each character
(such as the letter A, the letter g, or a question mark) is usually represented by either one or two
bytes.^3 Groups of 16, 32, and 64 bits are generally referred to as words(the terms short wordand
long wordare sometimes used to refer to 16-bit and 64-bit groups, respectively).
The process of assigning bit patterns to pieces of data is calledcoding—the same name we
give to the process of translating an algorithm into a programming language. The names are the
same because the first computers recognized only one language—which was binary in form.
Thus, in the early days of computers, programming meant translating both data and algorithms
into patterns of 1s and 0s.

1.1 Overview of Object-Oriented Programming | 9

the majority of the effort expended on most applications. For
example, an application that is implemented in a few months
may need to be maintained over a period of many years. For
this reason, it is a cost-effective investment of time to carefully
develop the initial problem solution and algorithm implemen-
tation. Together, the problem-solving, implementation, and
maintenance phases constitute the application’s life cycle.
In addition to solving the problem, implementing the algo-
rithm, and maintaining the code,documentationis an important
part of the programming process. Documentation includes writ-
ten explanations of the problem at hand and the organization
of the solution, comments embedded within the code itself,
and user manuals that describe how to use the program.
Many different people are likely to work on an application
over its lifetime. Each of those individuals must be able to read and understand
the code.


Problem

Shortcut?

Problem-Solving Phase
Algorithm

Implementation Phase

Code

Figure 1.3 Programming Shortcut?

Documentation The written
text and comments that make
an application easier for others
to understand, use, and modify

(^3) Most programming languages use the American Standard Code for Information Interchange (ASCII) to
represent the English alphabet and other symbols. Each ASCII character is stored in a single byte. Java
recognizes both ASCII and a newer standard called Unicode, which includes the alphabets of many
other languages. A single Unicode character takes up two bytes in the computer’s memory.

Free download pdf