Java Reference

In-Depth Information

1.3 Binary Representation of Numbers

It is important to understand the fact that a computer can store only zeros and ones.

Consider the integer 423. It is equal to 4

10
0
. This is known as
base 10

representation
because every digit is between 0 and 9. Similarly, there is base 2 represen-

tation that uses only 2 digits: 0 and 1. This is also referred to as the
binary
representation

of the number. For example, the binary number 01010010 represents the decimal number

0

10
2
+2

10
1
+3

·

·

·

2
0
= 82.

There is also an easy algorithm for converting a decimal integer to a binary number.

Consider the decimal integer 134. The conversion algorithm keeps dividing the number by

2 and records the remainders.

134 /2 = 67 remainder 0

67 / 2 = 33 remainder 1

33 / 2 = 16 remainder 1

16 /2 = 8 remainder 0

8/2=4remainder0

4/2=2remainder0

2/2=1remainder0

add a 1

The binary number is created by starting with the number 1 and then taking the re-

mainders in reverse order. For example, the decimal number 134 is equal to the binary

number 10000110. To verify our work, we can try to convert the number back to a decimal

number. It will be equal to: 1

2
7
+1

2
6
+0

2
5
+1

2
4
+0

2
3
+0

2
2
+1

2
1
+0

·

·

·

·

·

·

·

·

2
0
= 134.

Sometimes, data that is stored in computers is represented using hexadecimal numbers.

These are numbers in base 16. Table 1.2 shows the hexadecimal digits. The first 10 digits

are the same as the decimal digits. Then the letters A-F are used to represent the numbers

10-15. Consider the hexadecimal number 4F1A. Since a hexadecimal digit takes 4 bits to

represent, the number will be 16 bits or 2 bytes long. To convert it into a binary number,

we can just use Table 1.2 as a reference and convert every digit individually. In binary, the

number will be: 0100 1111 0001 1010. Since the hexadecimal representation of a number is

much shorter than the binary representation, it is often used to represent binary data, such

as main memory addresses.

·

2
7
+0

·

2
6
+0

·

2
5
+0

·

2
4
+0

·

2
3
+1

·

2
2
+1

·

2
1
+0

·

1.4 Software Creation and Types of Software

Software is usually written by
software programmers
using a
programming language
,such

as Java or C. It is then converted into binary code that the CPU can understand. A
compiler

will directly translate the program code into binary (or executable) code, which can be later

executed. For example, in Windows executable files have the extension
.exe
. Conversely,

an
interpreter
interprets the program. It translates every line of code into executable code,

which is then executed. A file that contains the executable code is not created. An interpreter

is usually slower than a compiler because each line of code needs to be translated into

executable code before it can be executed.

Java takes an approach that uses both a compiler and interpreter. Java code is compiled

into Java binary code, which is different than the binary code that the CPU can understand.

This binary code can be later executed by a
Java Virtual Machine (JVM)
interpreter. This