![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Place-Value Number SystemsThe bits in memory arent 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. Lets 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, youve 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 didnt become widespread, even in Eurasia, until well into the second millennium. Its 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.
Binary notationThe 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 youll 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));
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 thats 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);
|
![]() |
|