Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

FCPS

Java
Packets
Computer Number
Systems
Based upon material developed by
Sally Bellacqua, Mary Johnson, and Janet Mulloy
Revised by Charles Brewer, August 2004
Revised by Shane Torbert, July 2006
last revision May 2010

Fairfax County Public Schools


Fairfax, Virginia
CN 1

1. INTRODUCTION

Throughout history, different civilizations have used various devices to facilitate


computations. The Babylonians, Chinese, and early Europeans used the abacus for
calculations. The abacus consists of a divided frame with sliding beads on the top
representing fives and sliding beads on the bottom representing ones. Today, devices
such as calculators and computers perform a similar function using a combination of
Random Access Memory (RAM), Read Only Memory (ROM), a Central Processing
Unit (CPU), and a variety of support chips and storage, display, and input devices.

2. BINARY NUMBER SYSTEM

Current computer chips have two states, ON or OFF, which conveniently can represent
0 and 1, the two digits in the binary number system. All instructions and data in
current computers are represented as a series of 0’s and 1’s. While high-level
languages shield the user from the need to input instructions and data in binary form,
knowledge of binary arithmetic is useful in understanding how computers operate and
their limitations. The binary number system easily translates into the octal (base 8)
and the hexadecimal (base 16) system. Programmers need to think in all three
number systems.

Base 10 Base 2 Base16 Base 3 Base 6 Base 12


The table to the (decimal) (binary) (hexadecimal)
right shows how 0 0 0
numbers are 1 1 1
represented in 2 10 2
different bases. 3 11 3
4 100 4
Different bases 5 101 5
group the digits 6 110 6
differently. That’s 7 111 7
the reason for place 8 1000 8
value. 9 1001 9
10 1010 A
Try counting in 11 1011 B
base 3, 6, and 12. 12 1100 C
13 1101 D
14 1110 E
15 1111 F
16 10000 10
17 10001 11
18 10010 12
19 10011 13
20 10100 14
CN 2

A number in base b can be converted to its equivalent in base 10 by using place


values.

P la c e V a lu e F o rm u la
b 8 b 7 b 6 b 5 b 4 b 3b 2 b1b 0 . b −1b −2 b −3b −4

Given 1394210, what do the individual digits represent?

13942 10 = 1(10)4 + 3(10)3 + 9(10)2 + 4(10)1 + 2(10)0


= 10,000 + 3000 + 900 + 40 +2

Likewise, the digits in the base 2 number 110101 represent powers of the base 2.

110101 2 = 1(2)5 + 1(2)4 + 0(2)3 + 1(2)2 + 0(2)1 + 1(2)0


= 1(32) + 1(16) + 0(8) + 1(4) + 0(2) + 1(1)
= 32 + 16 +0 +4 +0 +1
= 5310

Examples:
1) Convert 13F16 to base ten. 2) Convert 0.23 to base 10

13F16 = 1(16)2 + 3(16)1 + F(16)0 the place value of the "2" is 3-1.
a negative exponent means a fraction 2
= 1(256) + 3(16) + 15(1) 3
= 256 + 48 + 15 2
= 319 10 therefore, 0.23 = = 0.6666667 10
3
Exercise 1

1. How many digits are there in base 8 (octal)? ______ digits, from ____ through ____

2. Count to eighteen in octal. __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __

In problems 3 – 10, convert the base b number to its equivalent in base 10.

3. 159 16 = ________ 10 4. 01101100 2 = ________ 10

5. 3A 16 = _________ 10 6. 10110 2 = _________ 10

7. DC 16 = __________ 10 8. 1010.101 2 = __________ 10

9. 5723 8 = _________ 10 10. 201 3 = ____________ 10


CN 3

2. ADDING IN DIFFERENT BASES

Adding in different bases follows the familiar addition rules, including when to carry the
number. You just need to pay attention to the base. For example, 78 + 38 has a carry
operation, namely, 78 + 38 = 1010 = (1 group of 8 and 2 left over) = 128
carry sum

As another example,
B16 + 616 = 1110 + 610 = 1710 = (1 group of 16 and 1 left over) = 1116

Addition in base 2 is so simple that you only have to memorize three rules:
02 + 02 = 02 and 12 + 02 = 12 and 12 + 12 = 102

Also, you should memorize that 12 + 12 + 12 = 112

Exercise 2

Add in the indicated base. Then check your answers for 1 – 5 by doing the work in the
decimal base (base 10).
1. 12 2. 11 2 3. 112 4. 1112
+ 12 + 01 2 +112 +0112

5. 0010 1110 2 6. 342 8 7. 3A9 16 8. 432 5


+ 0011 1011 2 + 517 8 + 21C 16 + 123 5

3. CONVERTING BETWEEN BINARY AND HEXADECIMAL

The representation of a decimal number in binary form BINARY HEX


can require a lot of digits. For example, 6553510 = 0000 0
11111111111111112 These sixteen binary digits can 0001 1
be conveniently expressed as four hexadecimal digits by 0010 2
grouping the binary digits in groups of four and using the 0011 3
equivalent values from the table at the right. 0100 4
0101 5
Example: 1111 1111 1111 1111 2 = F F F F 16. 0110 6
0111 7
Similarly, any hexadecimal number can be converted to 1000 8
binary by replacing each hexadecimal digit with its four 1001 9
binary digit equivalent. 1010 A
1011 B
Example: 3B9 16 = 0011 1011 1001 2 1100 C
1101 D
1110 E
1111 F
CN 4

Exercise 3

Convert between binary and hexadecimal.

1. 01011001 2 = _____ 16 2. 54 16 = _____________ 2

3. 01000001 2 = _____ 16 4. 3E 16 = _____________ 2

5. 00110100.0110 2 = _____ 16 6. 6A.5 16 = _______________ 2

4. CONVERTING BASE 10 NUMBERS TO ANOTHER BASE

While we are primarily interested in being able to convert base 10 numbers to their
binary (or hexadecimal) equivalents, there is a convenient algorithm for the conversion
that works for all bases. The “Divide and Save” algorithm is simple to use and easy to
implement in a computer program.

The technique is to use successive integer division by the desired base until a zero
quotient is obtained. The remainders from each division are recorded from right to left
as the result.

Example:

Convert the base ten number 37 to binary. On paper, you start by dividing from the
bottom: Now you try. Convert 2510 to binary.
0 R1 ( one 25 digit )
21 R0 ( zero 24 digit )
2 2 R0 ( zero 23 digit )
2 4 R1 ( one 22 digit )
29 R0 ( zero 21 digit )
2 18 R1 ( one 20 digit )
____
2 37
2 │25

Therefore, 3710 = 1001012


Therefore, 2510 = _________ 2
Check the answer by converting back.
CN 5

Example:
6 R6
Convert the base ten number 110 to hexadecimal. ____
16 | 6 R 14 Æ E
______
16 │110
Therefore, 11010 = 6E16

Exercise 4
Convert the following base ten numbers to the indicated base number.

1. 25 10 = ___________ 2 2. 73 10 = ____________ 16

3. 65 10 = ___________ 2 4. 92 10 = ____________ 16

5. 32767 10 = _______________________ 2 = _______________________ 16

6. 7 10 = ______________ 3

5. REPRESENTATION OF NEGATIVE INTEGERS—Two’s Complement System

So far, we have only talked about positive integers. How should we represent both
positive and negative (or “signed”) numbers?

Computer scientists have invented several methods. The


Intel family of CPUs uses the two’s-complement method to 2 0000 0010
represent negative integers, mostly because it makes addition +-2 + 1111 1110
extremely easy. If you add 2 and -2, you want to get 0. In the 0 1 0000 0000
two’s-complement system, 0000 0010 and 1111 1110 add by discard
the standard binary addition rules directly to get 0.
127 0111 1111
Somehow, 1111 1110 ought to represent -2. How can we
... ...
see this? 4 0000 0100
3 0000 0011
One way to see this is to look at the table. 0 through 127 2 0000 0010
count up by ones in the standard binary counting system. If 1 0000 0001
we add 1 more, 1000 0000 "wraps around" to become -128. 0 0000 0000
If we keep counting up by ones, -2 turns out to be 1111 -1 1111 1111
-2 1111 1110
1110, which is just what we wanted.
-3 1111 1101
... ...
Thus, an 8-bit register can store 256 numbers, from -128 to -126 1000 0010
127. Notice that in the two’s-complement system, all -127 1000 0001
positive integers begin with 0 and all negative integers begin -128 1000 0000
with 1.
CN 6

Mathematically, in two’s-complement a signed binary integer is calculated (in 8 bits)


using the relationship 256 - | n |, where | n | is the absolute value of n. For example,

-6 10 = (256 - | -6 |)10 = 250 10 = 1111 1010 2

-128 10 = (256 -| -128 |) 10 = 128 10 = 1000 00002

The computer actually calculates the negative number using a different algorithm. For
example, to represent –6 in binary:

First, change the absolute value of the number to binary | - 6 10 | = 0000 0110

Second, form the one’s complement by interchanging 0s and 1s 1111 1001


Third, add 1 + 1
to form the two’s complement 1111 1010

Example:
Convert -128 to its two-complement representation:
Step 1 – Change | -128 | to binary 1000 0000

Step 2 – Form the one’s complement 0111 1111


Step 3 -- Add 1 +1
to form the two’s complement 1000 0000

In this unit, we will use 8-bits instead of the 32-, or 64-bits that today’s computers
actually use. The Apple II and other personal computers of the 1970’s were 8-bit
computers.

Example:
What base ten integer does the signed binary number, 10111010, represent?

Since the leading bit is a 1, the number must be negative; therefore, the two’s-
complement method must be used to find the absolute value.

Given signed binary number 1011 1010


Step 1 – Form one’s complement 0100 0101
Step 2 – Add 1 to +1
form the two’s complement 0100 0110

Step 3 – Convert the two’s complement


to the base ten absolute value 0100 01102 = 7010

Step 4 – Insert the negative sign. Therefore, 1011 10102 = -7010.


CN 7

EXERCISE 5
1 – 4. Form the one’s and two’s complements of the following signed binary numbers.

SIGNED BINARY ONE’S COMPLEMENT TWO’S COMPLEMENT


1010 1011
0111 0000
0000 0001
0000 0000

5 – 8. Represent each negative integer in 8-bit two’s complement form.

DECIMAL ABSOLUTE VALUE ONE’S TWO’S


INTEGER IN BINARY COMPLEMENT COMPLEMENT
-1
-2
-30
-8

6. ADDING SIGNED BINARY NUMBERS

Any time you enter, e.g. -2, we have seen that two's complement system translates
that into 1111 1110. This may seem complicated, but in this system, *all* arithmetic
operations (+ - * / % √ ) become easier. Addition is just the addition of bits.
Subtraction is the addition of two’s complement. (Computers and calculators don’t
actually subtract; they just add the two's complement.) Furthermore, multiplication is
repeated addition. Division is repeated two’s complement addition. No wonder
programmers like the two’s complement system.

Example:

What is the answer to 10 10 + (– 10 10) ?

The 8-bit binary representation of 10 is 0000 1010

The 8-bit binary representation of –10 0000 1010


is the two’s-complement 1111 0101
+1
1111 0110 1111 0110
Add the binary numbers and discard the 1 0000 0000
overflow digit, the resulting being 0000 0000 2
or 0 10.
CN 8

Example:

Add these two 8-bit signed binary numbers and 1110 1100
then check your work by converting all three + 1101 0110
numbers to base ten.

11111
Add – Add the bits, column by column, carrying. 1110 1100
If the sum overflows 8 bits, discard + 1101 0110
the overflow bit. 1 1100 0010

Check – Check the addition by converting the three numbers to base ten to. Since all
three are negative in this example, the two’s complement method (see Section 5) will
be used to find their absolute values.

ORIGINAL BINARY Æ (1’s comp + 1 = 2’s comp) Æ DECIMAL

1110 1100 → 0001 0011 + 1 = 0001 0100 → − 20


1101 0110 → 0010 1001 + 1 = 0010 1010 → − 42
1100 0010 → 0011 1101 + 1 = 0011 1110 → − 62

Exercise 6

Add these 8-bit signed binary numbers. Check your work by converting all three
numbers to base ten. (The check is more work than the original addition problem.)

1. 0000 1001 2. 0001 1100


+ 1111 1100 + 1011 0101

Represent these decimal numbers as 8-bit signed binary numbers. Add the binary
number and then convert each answer back to base ten as a check.

3. -7 4. -17
+ 9 + -13
CN 9

6. REPRESENTATION OF REAL NUMBERS

Real numbers are represented using one of the standard formats, either float or
double. The Institute of Electrical and Electronic Engineers (IEEE) standard for 32-bit
or 4-byte representation is used for floats. The standard consists of 3 parts: 1 bit is
used for sign information, 8 bits for weighted exponent, and 23 bits for normalized
mantissa.

Example: Express 7.5 as a float.

First, convert the decimal to binary. 7.5 10 = 0111.12 = 1.111 2 * 2 2

Second, determine the weighted exponent by adding 127 to the power of 2 resulting in
129 or in binary form ( 1000 0010 ).

Third, create the normalized mantissa (fraction) by dropping the 1 to the left of the
binary point and adding zeroes to the right to give 23 digits.

The resulting float representation is: 0100 0001 0111 0000 0000 0000 0000 0000

Normalized Mantissa

Weighted Exponent
Sign

Special Values
If the exponent (E) is 255 and the fraction (F) is nonzero, the value is NaN for Not a
Number. If E=255 and F=0 the value is Infinity or -Infinity, depending on the sign bit.

If E=0 and F=0 the value if 0 or -0, depending on the sign bit. If E=0 and F is nonzero
the fraction is considered unnormalized (i.e., we assume a 0 in front of the binary point
instead of a 1).

The largest positive value is when E=254, so the exponent is E-127=127. This is on
the order of 2^127=(2^10)^12*2^7=(10^3)^12*128=10^36*10^2=10^38, approximately.

The smallest positive value is when E=0 and the fraction is unnormalized. If F is as
small as possible we get an exponent of -126 and a fraction of 2^-23. Multiplying this is
2^-149=(2^10)^-15=(10^3)^-15=10^-45, approximately. Positive values smaller than
this cannot be represented in a float variable. Underflow occurs, and the resulting
value stored is exactly equal to zero.

Float values are denser closer to zero, and generally the number of distinct floats
between consecutive powers of two is constant. This means that very large values
have very large gaps between them, and swamping is possible.
CN 10

Double
A double uses twice the bits of a float. It has 1 sign bit, 11 exponent bits, and 52
fraction bits.

If E=2047 and F is nonzero, the value is NaN for Not a Number. If E=2047 and F=0
the value is Infinity or -Infinity, depending on the sign bit.

If E=0 and F=0 the value if 0 or -0, depending on the sign bit. If E=0 and F is nonzero
the fraction is considered unnormalized.

Its largest value is 2^1023=(2^10)^102*2^3=(10^3)^102*10=10^307, approximately.

Its smallest positive value is 2^-1022*2^-52=10^-324, approximately.

Code Peculiarities in Java

double x = Math.sqrt(-1);
System.out.println( x +" " + (x == x) ); NaN false

double y = 1.0 / 0.0;


System.out.println( y +" " + (y == y+ 1) ); Infinity true

y *= -1.0;
System.out.println( y +" " + (y == y + 1) ); -Infinity true

Evidently not-a-number is not equal to itself. It seems that infinity-plus-one equals one.
Notice that NaN, Infinity, and Negative Infinity are actually stored in double variables.

Precision
The fractional part of the scientific notation determines precision. A double’s fraction is
accurate to 2^-52=10^-16. This is why you see 16 digits after the decimal point when
you print out, say, a random number from Math.random.

Precision determines the spread of values that can be represented in a double, which
in turn determines when swamping occurs. One important value is given the special
name machine epsilon. This is the smallest number that can be added to 1 and yield a
result that is different from 1. For doubles in 32-bit compilers, the machine epsilon is
approximately 10^-16, or approximately 2^-52. That is, 1.0 + something less than
10^-16 is indistinguishable from 1.0.

Note that machine epsilon is not the smallest number that can be represented, which is
10^-324, or 2^-1074,
CN 11

7. MULTIPLICATION IN BINARY

The Arithmetic Logic Unit (ALU) in the CPU multiplies binary numbers using a “shift
left” and “shift right” logic. The following function uses a multiplying algorithm that
illustrates the operation of this shift logic.

int result (int x, int y)


{
int z = 0;
while ( y != 0 )
{
if (y % 2 == 1) // If y is odd add x to interim result (z)
z = z + x;
x = 2 * x; // Shifts binary number left
y=y/2; // Shifts binary number right
}
return z; // When y is 0, return interim result
}

A call to this function with x = 7 and y =13 would return 91 in this way:

Decimal Binary
Iteration z x y z x y
0 0 7 13 0000 0000 0000 0111 0000 1101
1 7 14 6 0000 0111 0000 1110 0000 0110
2 7 28 3 0000 0111 0001 1100 0000 0011
3 35 56 1 0010 0011 0011 1000 0000 0001
4 91 112 0 0101 1011 0111 0000 0000 0000

8. REPRESENTATIONS OF CHARACTERS

Characters are normally represented on PCs using the 8-bit ASCII (American Standard
Code for Information Interchange). The ASCII code defines 128 characters from 0 to
127 using the seven of the eight bits in a byte. The extended ASCII set includes the
code from 128 to 255 and varies from machine to machine and font to font.

A standard for 16-bit representation of characters called Unicode has been created and
is used by the Java compiler. This larger data size provides for non-English
characters. A table of Unicode characters is on the next page. Note that the Unicode
for 'A' is '\u0041' (in hexadecimal) and the unicode for 'a' is '\u0061' (in hexadecimal).
What is the code for 'A' in decimal? _____ What is the code for 'a' in decimal? ______

Some mainframe computers use still another format for character representation called
EBCDIC (Extended Binary Coded Decimal Interchange Code).
CN 12

Unicode, expressed in hexadecimal

The Primitive Data Types in Java

Keyword Size Range Intel Format Typical Use


byte 8-bit -128 to 127 two's complement where space is a concern
short 16-bit -32768 to 32767 two's complement
Integers

int 32-bit -231 to 231 -1 two's complement for integers

elapsed time in milliseconds,


long 64-bit two's complement financial calculations,
large Fibonacci numbers
float 32-bit IEEE 754
Real

double 64-bit 10^-324 to 10^307 IEEE 754 scientific uses


char 16-bit Unicode character single letters or characters
boolean 1 bit 0, 1 true or false
CN 13

9. UNIT REVIEW EXERCISES

1. What is the largest digit in base 16? ___________

2. What is the base-ten value of the underlined bit in the


binary number 0 1 1 0 1 1 1 0? ___________

3. Convert 1245 to base 10. ___________

4. Express the hexadecimal number FF in base ten. ___________

5. Express 12A16 in binary. ___________

6. Find the two’s complement of 1101 0111. ___________

7. Add these hexadecimal numbers. 4F


Leave your answer in hexadecimal. + 68 ___________

8. 1100 1111 is the signed 8-bit representation


of what base ten number? ___________

9. Convert 3710 to binary. ___________

10. Express the binary 0101 1011.0110 in base sixteen. ___________

11. Express 8710 in base sixteen. ___________

12. Convert 2310 to its signed 8-bit representation. ___________

13. Perform the following addition of two 1111 0011


8-bit signed binary numbers. Check + 0000 1101
your work by converting each binary
number into base ten.

14. Convert the following base ten numbers - 37


to binary and add. Check your result + 23
by converting the answer back to base ten. -14

You might also like