Section 3.1. Binary Overview

   

3.1 Binary Overview

If you have a good understanding of binary numbers, you can safely skip this section. You may want to take a look at the example program at the end of this section, however. This section offers an overview (or quick refresher if it's been a while) of binary numbers . This is important in Java because of memory management and conversions between types.

Common communications regarding numbers use a base 10 number system, likely because we generally have 10 fingers and 10 toes on which to count. Computers currently store data in terms of binary numbers. The reason for this is that early computers used valves to represent one of two different states (on or off). Though some might object, one could say that computers only had two fingers on which to count. But all of the same mathematical concepts can be represented in a base 2 (or binary) system. Of course, to represent the number 3, one would need two sets of valves , just as a base 10 numbering system has only the digits 0 through 9 (there is no "10" digit ”it is made up of a 1 and a 0). Because a bit can store one of two values (0 or 1), computers use 2s and powers of 2 to perform calculations and represent states; they are binary systems.

Note

The term bit was first coined by Claude Shannon (some claim John Wilder Tukey) in 1948. The word "bit" in this context is a kind of word called a portmanteau ”itself a term coined by Lewis Carroll in the 19th century. Carroll used portmanteau to refer to a word created by conjoining two existing words ”something he does frequently in his famous poem "The Jabberwocky." For instance, the word slithy in that poem doesn't exist in English; it is a portmanteau of the words "lithe" and "slimy," combined to form a new word meaning both things at once. Shannon's bit is a purposeful portmanteau of binary digit.


In a binary system, one calculates values by raising 2 to the power of the number of bits. The positions of the digits in a multidigit binary number correspond to powers of 2. Given a number composed of a 1 and all the rest zeros, that number is a power of 2. Table 3.1 gives a quick demonstration of this concept.

Table 3.1. Powers of 2 Represented in Binary

Power of 2

Binary

2

1

2 1

10

2 2

100

2 3

1,000

2 4

10,000

2 5

100,000

2 6

1,000,000

2 7

10,000,000

2 8

100,000,000

2 9

1,000,000,000

Performing mathematical computations in a base 2 numbering system can be tricky for people who are used to base 10. Table 3.2 shows the first few numbers in base 10 with their corresponding base 2 representations.

Table 3.2. Base 10 Numbers and Corresponding Base 2 Numbers

Base 10

Base 2

1

1

2

10

3

11

4

100

5

101

6

110

7

111

8

1000

9

1001

10

1010

In binary, 0 and 1 are represented just as they are in a decimal system. In decimal, 10 represents the next number after all of the single digits have been used up (9 being the last). Since 2 has no digit for itself in binary, it gets represented just like 10 does. That is, 2 base10 is the equivalent of 10 base2 . A more detailed exploration of alternative numbering systems (such as hexadecimal) isn't really required in order to work well with Java. It does help, however, to be comfortable with at least this much, as we will see when we move on into data types and memory.

Here is an example Java program that you can use to generate base 10 numbers in base 2 format. The output will resemble Table 3.2:

3.1.1 MakeBase2.java

 /*  File: chp3.MakeBase2.java Date: April 21 2002 Purpose: outputs the base10 numbers 0 - 10 in base2 Author: E Hewitt */ public class MakeBase2 {     public static void main(String[] args) {     // simple for loop     for (int i = 0; i <= 10; i++) {         // get the Integer wrapper class,         // call its 'toBinaryString' method         // create String to hold the converted value        String s = Integer.toBinaryString(i);         // print it to the command line         System.out.println(i + " in base2: " + s);        }     } } 

If the above program doesn't make sense yet, don't worry. We haven't discussed many of the elements in this program. However, the for loop and comments are likely to look familiar to you. It might be a good idea to type this program in, try changing the values, and recompile.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net