Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Place-Value Number Systems

The bits in memory aren’t just random voltages. They have meanings, and the meanings depend on the context. In one context, the bit sequence 0000000000100001 means the letter “A.” In another context, it means the number 65. Let’s explore how you get the number 65 out of the bits 0000000000100001.

When you write a number like 1406 in decimal notation, what you really mean is one thousand, four hundreds, no tens, and six ones. This may seem trivially obvious to you. After all, you’ve had this system drilled into you since early childhood. However, the place-value number system in which there are exactly ten digits and numbers larger than nine are represented by moving the digits further to the left is far from obvious. It took humanity most of its existence on this planet to develop this form of counting, and it didn’t become widespread, even in Eurasia, until well into the second millennium. It’s even less obvious that the digits on the left represent bigger numbers than the digits on the right. You could just as easily write the number as 6041 with the understanding that the first place is the ones place, the second place the tens place, the third place the hundreds, and so on.


Note:  Classical Hebrew writes numbers from right to left. However, it doesn’t use a place-value system.

Binary notation

The number 0000000000100001 that you saw in the preceding section is written in a place-value system based on powers of two called binary notation. Each place is a power of two, not of ten, and there are only two digits — 0 and 1. Moving from right to left, therefore, we have one one, zero twos, zero fours, zero eights, zero sixteens, zero thirty-twos, and one sixty-four. Therefore, 0000000000100001 is equal to 64 + 1, or 65, in decimal notation.

There are extra zeroes on the left side because Java uses bits only in groups of eight at a time, although the individual bits do have meaning. Furthermore, as you’ll see below, characters like A are always 16 bits wide. You could use 0100001 to represent the value 65, but unlike 0000000010-0001, it would not also mean the letter A.

Java has several methods to convert between binary and decimal notation. The Integer and Long classes each have a static toBinaryString() method which converts ints and longs respectively to binary strings of ones and zeroes. For example, to print the int value 65 as a binary string, you could write

     System.out.println(Integer.toBinaryString(65));

Longs are converted similarly:

     System.out.println(Long.toBinaryString(5000000000L));


Secret:  The Byte and Short classes do not have toBinaryString() methods, but bytes and shorts can be converted using the Integer.toBinaryString() method.

Given a binary string of ones and zeroes, the Byte, Short, Integer, and Long classes each have static valueOf() and parse methods that convert binary strings into integers of the specified width.

The Byte.parseByte(String s), Short.parseShort(String s), Integer. parseInt(String s), and Long.parseLong(String s) methods convert a string like “28” into a byte, short, int, or long value respectively. These methods presume that the string is written in base 10. However, you can change the base that’s used to make the conversion by passing an additional int containing the base to the method, like this:

     int m = Integer.parseInt("100001", 2);

To convert the binary string 00000000100001 into byte, short, int, and long values of 65, you would write

     byte b = Byte.parseByte("0100001", 2);
     short s = Short.parseShort("00000000100001", 2);
     int i = Integer.parseInt("00000000100001", 2);
     long l = Long.parseLong("00000000100001", 2);

If the string does not have the form appropriate for the base you specify in the second argument (for example, if you try to convert the string “97” in base 2), then a NumberFormatException will be thrown.

The static valueOf() methods in the Byte, Short, Integer, and Long classes are very similar except that they return objects of the type-wrapper classes rather than primitive data types. For example:

     Byte B = Byte.valueOf("0100001", 2);
     Short S = Short.valueOf("00000000100001", 2);
     Integer I = Integer.valueOf("00000000100001", 2);
     Long L = Long.valueOf("00000000100001", 2);


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.