July 27, 2011, 5:21 p.m.
posted by soulmaker
The Nature of Computer Data
In Chapter 2, "Introducing Visual Basic," I mentioned how all data in a computer eventually breaks down to individual bits, electrical impulses that represent either one or zero, on or off, true or false. Because our decimal number system requires more than just those two values, computers work in the world of binarya number system limited to only the numbers 0 and 1. Fortunately, it's pretty easy to represent basic decimal integer numbers using binary notation. You probably remember Mrs. Green back in second grade telling you about the different place values of multi-digit numbers, shown in Figure.
The fruits of Mrs. Green's labors
The same type of diagram can be used for binary numbers; only the position names and values are changed. For convenience, we call these positions by their decimal names, or use the related powers of two. All of this is shown in Figure.
The positions of an 8-bit (8-digit) binary number
To figure out what this number is in decimal, just add up the columns. Let's see, there's one each of fours, eights, and sixty-fours, and none of the rest. 4+8+64, that's 76. Because any binary digit can never be more than 1, the counting is pretty simple. I showed an 8-bit (8-digit) binary example herewhich can handle the numbers 0 through 255but you can represent larger decimal numbers by adding more binary digits.
That's just fine for integer values, but how do you represent decimal and fractional numbers? What about negative numbers; where do they fit in this binary system? And it's not just numbers. My computer can process text data, arrays of numbers, graphic images, and customer records. How are those stored in binary form?
To handle the myriad data forms, every computer includes a small community of Lilliputians who are good at math, language, and art. No wait, I think that's from a story I'm reading my son at bedtime. Oh yes, now I remember. Computers implement data types to handle all the various forms of data to be managed. Each data type acts as an interpreter between a collection of bits and a piece of information that a computer user can better utilize and understand.
All data types ultimately store their content as individual bits of data, but they differ in how those bits get interpreted. Imagine a data type named Vitamin that indicated which vitamins were included in a food product. Figure shows how the eight bits used earlier could be assigned and interpreted as vitamins.
Loaded with vitamins B6, D, and E
With such a data type, you could assign vitamin values to food items tracked in your application. (This is just a sampling of vitamins; you would require more bits to handle all of the vitamins. This example should not be construed as an offer of medical services. Consult your doctor.)
For an example that is more in tune with Visual Basic, take that number 76 we were discussing earlier. It's easy enough to convert it to binary representation, 01001100. The .NET Framework includes a few data types that do this conversion automatically, varying only by the number of binary digits (bits) they can handle. In the computer world, 76 also represents a letter of the alphabetthe capital letter "L." That's because there's a data type that establishes a dictionary between binary values and alphabetic (and other) characters. Windows programs have long used ASCII (American Standard Code for Information Interchange) as its number-to-character dictionary. This eight-bit system documents how to convert the numbers 0 through 255 into all the various characters used in English, including punctuation and other miscellaneous characters. Another dictionary, Unicode, uses 16 bits of data to handle around 65,000 different characters. .NET uses Unicode for its character and "string" data types.
Another rule-bearing data type is Boolean, which uses a single bit to represent either True (a bit value of 1) or False (0). Negative integers, floating point and fixed point decimal values, and dates and times round out the kinds of basic data most often managed by computers and their applications. More complex data structures can be built up from these basic types.