· 9 years ago · Oct 15, 2016, 06:28 AM
1Data Representation
2Methods
32
4Introduction to Binary Codes
5Beyond the physical hardware of the
6computer, programs (software) is written
7to input data and to output information.
8While each of us pay understand the
9concept of an alphabet, word, sentence,
10numbers, etc., computers are not so lucky.
11Electronically, a digital computer is only
12capable of understanding and processing
13the status of a bit, or binary digit.
143
15What is a Bit or Binary Digit?
16It does matter what electronic method is used, a bit
17is any device that is capable of having two states.
18It is either ON or OFF. Most modern computers
19have electronic components that in theory works
20like a light switch it is either ON or OFF. To make
21live easier, the number 1 is used to represent ON
22or True. The number 0
23Binary electronics are reliable and inexpensive to
24build. Since all digital computer operations are
25based on two states, all hardware computer
26functions are built on the basis of multiples of
27twos, i.e., 2, 4, 8, 16, 32, 64, 128, 258, 512 and
281024.
294
30Limitations of Binary
31While electrical engineers get excited about binary, the basic
32bit (or one light switch) does little to help an average
33person store or manipulate human data and information
34inside a computer. A bit can only represents two states.
35Symbolically, the ON state can represent a "Passing
36Grade" and the OFF state can represent a "Failing Grade"
37Paul Revere in his famous warning that the British were
38coming also used binary symbolic representation. "One by
39Land and Two by Sea". As history reports this data
40representation system was quite adequate. But, what if the
41British want to stop at Heinz Field to eat a a Parmenitti Bros
42Sandwich before the attack? There was no third state that
43could represent this change in the battle plan. (A little bit of
44humor - a real little bit.)
455
46Binary Codes or
47Binary Representation Methods
48To represent human information, electrical
49engineers combined multiple bits together to
50represent more complex human information.
51Representing numeric data was easy. The Decimal
52Number System (Base 10) used by humans could
53easily be converted and store in a Binary Number
54System (Base 2). The Binary Number System
55could also execute binary mathematics and bitwise
56operations, e.g., true/false Boolean logic.
57The first computers could only store and process
58numerical binary codes.
596
604-Bit Integer Binary Code
61The first and easiest to understand numeric binary
62code was the 4-bit integer binary code. Integer
63codes may only store whole numbers. Therefore,
64storing a price of a six-pack of beer was totally
65out of the question, e.g., $8.50.
66Four light switches were combined to represent
67numeric data. Each light switch had a name or
68meaning. The names of the light switch were
69called 2
703
71, 22
72, 21
73, and 20
74- based on the powers of
75two.
76To make life easier simply call them the 8, 4, 2, and
771 light switches.
787
79Basic Decimal to Binary Conversion
80The 4-bit binary code could represent any number between 0 and 15
81by turning the 8, 4, 2 or 1 light switches on or off in 16 different
82combinations. While representing numbers with only 16 different
83numbers may seem primitive, the 4-bit binary code is still used in
84the MICR code used at the bottom of your personal checks.
85Those crazy magnetic symbols at the bottom of your check
86represents the digits 0-9 (your bank number, account and check
87number) and six different banking operations, e.g., draw on this
88bank.
898 4 2 1 Decimal Number
900 0 1 0 Turning on the 2 light switch, you can store the number 2
910 1 0 0 Turning on the 2 light switch, you can store the number 2
920 1 1 1 Turning on the 4, 2 and 1 light switches, you can store the
93number 7 = 4+2+1
941 1 1 1 Turning on the 8. 4, 2 and 1 light switches, you can store the
95number 15 = 8+4+2+1
968
976, 8, 16, 32 and 64 bit Integer Binary Codes.
98To increase the range of numbers that can
99be stored in binary code, more bits can be
100combined. For example, the maximum
101number that may be stored in a integer
102binary is easy to determine. For example,
103an 8-bit integer, called short integers by
104Microsoft, can store an integer between 0
105and 255. Or 27 + 26 +25 +24 +23 +22 +21 +20
106which is 128+64+32+16 +8 +4 +2 +1
1079
108Excess Notation and Two's Complement Notation
109Mathematical Sign
110Increasing the combination of bits in a integer binary code will
111increase the range of the numeric data that can be stored. But, the
112previous examples assume the data was positive.
113To store the mathematical sign of an integer, a Binary code named
114Excess Notation can be used. In Excess Notation, the leading bit
115is used to represent the numeric sign. When using Excess
116Notation, the range of values that can be stores is always reduce.
117For example, an unsigned 8-bit integer can store an integer
118between 0 and 255, and an 8-bit integer can store an integer
119between -128 and +127. An signed 8-bit integer can not store the
120number 255.
121An alternative method used to store the mathematical sign is called
122Two's Compliments Notation. The processing efficiency of
123negative values using Two's Compliment notation is a distinct
124advantage compared to Excess Notation. But, both notations use
125the left-most bit to represent the mathematical sign and decreases
126the range of stored values.
12710
128Do I Need To Store the Mathematical Sign?
129This is simply a trade off between scale and need of the of the
130application or person storing the data. Many programming
131languages like COBOL gives you the choice. For example, a
132COBOL data declaration of PIC 999, does not store the
133mathematical sign. A COBOL data declaration of PIC S999
134does. PIC S means store the sign.
135Most modern programming languages and applications that you
136use, e.g., Excel, Databases, etc, automatically stores the
137mathematical sign. Before the 1990s, reducing the number of
138bits used to store data (Compactness) was very important
139when RAM cost between $1000 to $10,000 per Megabyte and a
14030 Megabyte hard drive cost between $4000 and $8000.
141While today RAM and disk space may be inexpensive, one must
142remember that historical data sources or applications were
143very sensitive concerning decisions to store a mathematical
144sign.
14511
146Storing Versus Display Mathematical Sign
147If the original numeric data was stored without a mathematical
148sign, it obviously can not be displayed or printed as a
149negative. But even if the original numeric data was stored with
150a mathematical sign, some programming languages and
151applications do not automatically display the mathematical
152sign.
153For example, either the COBOL data declarations of PIC 999 or
154PIC S999 will not display the mathematical sign. COBOL
155requires special formatting characters like many other
156languages. For example, PIC +999, PIC 999+, PIC -999, PIC 999-
157, PIC ----, and PIC ++++ are some of the options in COBOL that
158will display mathematical sign,
159Can you imagine a customer's reaction who expects a credit
160balance on their account, but since the mathematical sign was
161not display the balance was displayed incorrectly as positive.
16212
163Range and Overflow
164The range of a numeric value that can be stored is determined
165number of bits used by the binary code. Choosing to reduce the
166number of bits to store data reduces storage costs and
167increases processor efficiency.
168But, incorrectly planning for the range of a number may cause more
169serious problems. While programmers may correctly plan for the
170range of the number used as an input, a problem may exists to
171store intermediate data during calculations.
172For example, can a 4-bit binary integer code store the number 15.
173Yes, Can a 4-bit binary integer code store the result of 15+15 or
17415 times 15. No.
17513
176Range and Overflow
177What happens when the range of a number exceeds the
178capacity of the binary code to store that number?
179The minimum problem is that data may be corrupted,
180saturated (max value) or truncated. A bigger problem
181is the application will ABEND and the application will
182be stopped. When an application program ABENDs
183out-of-control, the current user may be able to access
184the system at a higher privilege level.
185It is very common that an application program executes
186at a higher privilege than the person using the
187program. This strategy of forcing an overflow
188condition is a popular hacker strategy.
18914
190Handling Overflows
191Every programming language has a method of
192handling an unexpected overflow condition. In
193COBOL the clause ON OVERFLOW statement
194could be added to the COMPUTE statement.
195Newer versions of COBOL and modern
196programming languages provide more
197sophisticated overflow error handling, e.g., ON
198ERROR.
199While each programming language is different,
200trapping an overflow error (or any other abend
201error, e.g., divide by zero) is very important to the
202integrity of the program and preventing hacker
203attacks.
20415
205Representing Floating Point or
206Real Numbers
207The first part of our discussion concerning the storage of numbers
208was limited to integers, i.e., numbers without a decimal point.
209Floating point (real numbers) will use a floating point to
210represent fractional amounts.
211Floating point numbers are stored in a scientific or exponent
212notation. Assume that the number + 3,456.78 is to be stored. In
213scientific notation this number would be stored as +0.345678 x
214104 The following provides the format of each component.
215Sign of the
216Exponent
217Exponent Sign of the
218Mantissa
219Mantissa
220+ 4 + .345678
22116
222Scientific Notation
223If you were like me, I feel asleep during my science classes when they discussed
224scientific notation. The objective of scientific notation is to efficiently represent
225very large and very small fractional numbers in a standardize manner.
226The mantissa represents the fractional part of scientific notation representing a
227number between 0 and 1. All floating point numbers must be converted to a
228fractional number between 0 and 1.
229The Base 10 exponent will adjusted to align the decimal point for the data stored in
230the mantissa to the correct point. For example, the exponent +4 positions the
231decimal point four positions to the right ( making it a larger number) and the
232exponent -4 positions the decimal point to the left (making it a smaller number).
233The sign of the mantissa simple signs the data as either positive or negative.
234Sign of the Exponent Exponent Sign of the Mantissa Mantissa
235+ 4 + .345678
23617
237More Examples of Scientific Notation
238As you will notice the Mantissa is always a value between 0 and 1. The
239last example using a number that is already between zero and 1 and
240the exponent is set to 0. Raising any number to the zero power is
241always 1.
242Real Number Sign Exp Exponent Sign
243Mantissa
244Mantissa
245+3,456.78 + 4 + .345678
246+.0000345678 - 4 + .345678
247-3,456.78 + 4 - .345678
248-.0000345678 - 4 - .345678
249+543.45 + 3 + .54345
250-.00312 + 0 - .00312
25118
252IEEE 754 and 854
253IEEE 754 and 954 describes the general format of 32 and 64-bit
254floating point numbers. A example of a 32-bit
255implementation is provided below.
256
257The Radix point separates the exponent from the Mantissa. In
258the example below the radix point is bit 8. The exact
259location of the radix point is not specified directly by IEEE
260since it involves a trade-off between the range of a number
261and the precision of a number.
262Sign of the
263Exponent
264Exponent Sign of the
265Mantissa
266Mantissa
267Bit 0 Bits 1-7 Bit 8 Bits 9-31
268Radix Point
26919
270Precision and Truncation
271What is the answer of the following expressions? 1/3 times 3
272If you answered 1, you would be mathematically correct, but not
273correct for storing floating point numbers. The problem is that
274binary representation methods can not store every number that
275human mind can think of.
276A processor can only process two operands at one time. 1 divided by
2773 is .3333333333. Why a human can think of the concept of onethird,
278computers can not store that number. The answer is
279truncated, or chopped off.
280Continuing with expression, .333333333 times 3 is therefore
281.99999999. Not 1.
282Am I being a little picky? In financial applications involving trillions of
283dollars or plotting a course for a space mission, a truncation of
284.000000000001 may be disastrous.
28520
286Word Size, Radix Point,
287Precision and Truncation
288In the previous example, the radix point was set to
289bit 8. If we shifted the radix point to bit 6, we
290would have increased precision, which resulted
291in less truncation. But, decreasing the size of the
292exponent would mean that the range of the
293number would be decreased.
294A more acceptable solution would be to increase
295the number of bits (word size) of the floating
296point data representation method from 32-bits to
29764-bits. This would enable both an increase in
298range and precision. In more scientific
299calculations 256-bit floating point representation
300methods can be used.
30121
302Integers or Floating Point?
303The most important advantage of storing numeric data into
304either integer and floating point data representation formats
305is the ability to perform mathematical operations.
306If your data has no decimal point does it really matter whether
307or not you use a integer or floating point code?
308Yes, it does matter. Using a floating point code will
309substantially increase the range of any type of number that
310you can store. But, using a floating point code
311unnecessarily will substantially increase the processing
312overhead to accommodate the floating point calculations.
31322
314Character Binary Codes
315An important limitation of integer of floating point
316binary codes is the inability to store letters of the
317alphabet and special characters, e.g., ?,",;, ", =,
318etc.
319While character codes may store almost any type of
320character, you may not perform any mathematical
321operations on data stored using character code.
322The following slide is summary of the popular
323character codes. While Unicode has the most
324features and advantages, Unicode inherited the
325many of previous character codes into the family
326of Unicodes.
32723
328Summary of Character Codes
329Code Unicode Features and Comments
3306-Bit BCD None Uppercase letters only, some special characters, no special
331formatting characters. No longer used, but historical data
332may be stored in this format. 64 combinations
3337-Bit ASCII UTF-7 Uppercase and lower case letters, special characters, no
334special formatting characters. Used by SMTP, POP3, IMAP4
335(Internet email). 128 combinations
3368-Bit
337EBCDIC
338None Uppercase and lower case letters, special characters, special
339formatting characters. Developed by IBM. Used by most IBM
340mainframes. Proprietary. 256 combinations.
3418-Bit ANSI UTF-8 Uppercase and lower case letters, special characters, special
342formatting characters. Most popular. Used by most non-IBM
343legacy applications and operating systems. 256
344combinations.
34516-BIT
346Unicode
347UTF-16
348UTF-16BE
349UTF-16LE
35065,536 combinations. May represent international character
351sets. Default character code for Windows 2000 OS and up.
35232-bit
353Unicode
354UTF-32 Not really used.
35524
356Internet Email
357Internet Email uses 7-bit ASCII, or UTF-7, by default. This means you can not
358use any special formatting option to send Internet Email messages.
359Are you crazy? I have been bolding and embedding graphics into my email
360message for years.
361While you may have been using formatting and other features with Internet
362email the fact remains Internet Email began and still remains 7-bit ASCII.
363However, the MIME (Multipurpose Internet Mail Extension) extends the 7-bit
364ASCII code to include other codes. While SMTP can transmit 8 or 16-bit
365codes or multimedia attachments, MIME temporarily re-packages these
366codes into 7-bit boxes for transmission and then un-packages theses
367attachments when received.
368Have you ever noticed a meaningless attachment in your Email client
369window? Sometimes that contains information to format your email
370message to be displayed as it would have appeared in Word.
37125
372Character Sets and Character Codes
373While the previous presentation implies that the 8-Bit ASCII
374code can store the letter "A", this is an over simplification.
375In reality, any binary code can only represents Boolean
376(true/false values) binary information or numbers.
377However, the 8-BIT ASCII code can be mapped or assigned to
378the letter "A". The 8-bit ASCII code can only store the
379numbers from 0 to 255. ASCII maps or assigns the number
38065 to the Capital Letter "A". ASCII maps or assigns the
381number 66 to the Capital Letter "B". ASCII maps or assigns
382the number 67 to the Capital Letter "C", and so forth.
383A Character Set, also called a code page, is the set of
384characters that is displayed or printed when a particular
385character code number is encountered. Characters sets has
386nothing to do with fonts.
38726
388Character Sets and Character Codes
389When you store a file using an 8-bit character code, the current
390default is the UTF-8 character set. You can change the UTF-8
391character set to some other character set, e.g., Roman-8. A
392given character code may support more than one character set.
393While almost all English 8-bit character sets will display the letters
394of the alphabet and numbers correctly, there may differences in
395the manner in which special characters are displayed.
396I frequently encounter a problem that the same ASCII number for a
397hyphen will be displayed as a box in a different application.
398Microsoft Applications provide you will a limited selection of 8-bit
399character sets that are design to display grave in French, the
400omlot in German, etc. to display phonetic versions of a letter.
40127
402Unicode and Scripts
403Unicode is an standard that represents the characters of any written
404language. While English and other European languages can be
405adequate represent its character sets with an 8-bit code, this was
406not possible for languages like Hebrew, Chinese, and Japanese
407that uses graphical characters.
408Unicode provides 65,636 combinations to represent characters. The
409question then is, Which Characters?
410Unicode uses the Universal Character Set that includes over 100,000
411characters used in a variety of languages.
412A script assigns a collection of characters from the Universal
413Character set to the Unicode-16 character code for a particular
414language. The Swedish and English language normally uses the
415Latin Script. There are several different scripts used to assign
416characters to the Japanese language.
41728
418Character Sets Versus Scripts
419Unicode does not like to use the term Character Set,
420like the original 8-bit ASCII code. Character sets
421imply a one-to-one relationship. The ASCII
422number 65 is mapped to one letter, the Capital
423"A".
424The Unicode Number 565 can be mapped to a
425variety of characters from the Universal
426Character set. A script assigns one number to
427one of many different characters. Not only can
428one Unicode number is assign different
429characters, the same character can be assigned
430to different Unicode number ot create differently
431collating sequences.
43229
433Word 2003 and Character Encoding
434You can changed the
435default the Character
436encoding method used
437to store your Word
438documents under Tools,
439Options, General, Web
440Options, Encoding.
441When Word opens the
442document it
443automatically detects the
444Character Code.
445It is not recommend to
446change default coding in
447Word unless, you have
448compatibility problems
449with foreign document
450users or web pages.
45130
452Word 2003 Character Sets
453Though the Font tab is selected,
454Character codes and
455Character sets are not fonts.
456Again it is not recommend to
457change the Character set in
458Word unless you encounter
459language problems.
460There is a very limited number of
461character sets supported by
462Word. In Unicode, it is
463recommended to match the
464Character code to the
465appropriate character set.
46631
467Internet Explorer
468In IE7, under Tools, Internet
469Options, General, Fonts, and
470then Language Script. Again do
471not be confused. The language
472Script or Character set is not a
473font.
474IE Explorer also supports a
475Language option. The language
476option is not a character set.
477Rather, it is a refinement of the
478character set.
479For examples, two different
480language may use the same
481character set or scripts. One
482language may provide an
483option to treat the letter "o"
484differently than the other.
48532
486Collating Sequence
487If you were ask to compare 106 to 22, you would state that the number 106
488is greater than 22. If you would ask compare "BEER" to 818, which
489would be greater? Your answer may be that you can not compare
490numbers with words.
491But, since each character is assigned a different number in a character
492code, a numerical comparison can be made. The digit "0" is
493represented by ASCII number 56. The upper-case Letter "A" is
494represented by ASCII number 65. The lower-case letter "A" is
495represented by ASCII number 92.
496The ASCII collating sequence is the lower case letters are greater than
497upper case letters, upper case letters are greater than digits, and the
498digits are greater than the space.
499The answer to the above question is that "BEER" is greater than the
500characters "818" based on the ASCII collating sequence.
50133
502Problems with Comparisons and Math
503Programming languages and other applications are very
504dependent with on the features and limitations of character
505codes.
506For example, the upper-case letter "A" is not equal to the
507lower-case letter "a". The upper-case letter "A" is
508represented as number 65 and the lower case letter is
509represented a 92. The number 65 will never be equal to the
510number 92.
511While numeric digits can be represented by a character code
512and set, you can not perform math. The digit "1" is the
513ASCII Number 66. What does 66 have to do with the number
5141. Nothing. When your application sees a ASCII number 66
515it simply displays a picture of the number 1.
51634
517Zipcodes: Number or Character
518While the newer 10 digit Zipcode includes an hyphen, the old 5
519digit Zipcode was comprised of all digits. Should you store
520the Zipcode as an Integer Code or a Character Code? The
521traditional answer was "Do you plan to perform math
522operations on the Zipcode?"
523Integer codes will generally store numeric data with fewer
524bytes than character codes. Some programming languages
525and user interfaces will prevent you from storing nonnumeric
526data into integers, e.g., using the letter "O" instead
527of a zero.
528Character codes are more flexible and universally transferable.
529Since every Zipcode is associated with one City and State,
530there may be no reason to store the City and State Data.
53135
532Boolean and Memory Addresses
533Boolean data can be stored in one bit rather than a
534word or byte. Boolean data is generally classifies
535as True/False or Yes/No. Boolean data types is
536very primitive and minimizes storage.
537A memory address is an identifier for a memory
538location in RAM. A computer program or a
539hardware device can store a piece of data for
540later retrieval. Memory addresses were stored in
541the Instruction Pointer (IP) register and were used
542control the execution flow of a program.
543Operating systems will store application
544programs starting at a specific memory address.
54536
546Word Size, Memory Address and
547Address Space
548Word Size represents the number of bits that a CPU
549can process at one time. The word size
550represents the size of various registers and some
551busses. Popular word sizes include 16, 32 and 64-
552bits.
553While in theory a 64-bit computer can access up to
55416 exlibytes of memory, the actual number of bits
555used to represent an memory address may be
556more or fewer than the word size of a computer.
557Address space is the range of discrete memory
558addresses that can be accessed by the
559processor.
56037
561Taking the Letter "A" for Granted
562Each piece of data that we store into the
563computer must be assigned a binary code
564and a memory address.
565At this moment you are looking at this letter
566"A". We all take computers for granted
567when we view a web page or document.
568That letter "A" and every other letter must
569be stored in a computer address and
570assigned a binary code.
57138
572Why is the Binary Codes So Important?
573Data needs to be stored in RAM and then transferred to the CPU
574registers to be processed further. When a program stores data
575into RAM, it must store the data at unique memory address.
576Where did you put your socks? In your upper drawer? We all use a
577unique identifiers to remember where we store things.
578OK. So we now can find our socks, what will you do with your socks?
579Put them on your head or your feet?
580While you may think that the previous statement is silly, a computer
581does not automatically know what is in the drawer and how to
582process the contents of the drawer.
583Binary codes will control what and how data is stored in a memory
584location, and it will eventually affect how the data will be
585processed by a program
58639
587How Do We Use Binary Codes?
588The choice of a Binary Code is programming
589languages and the applications is critical. It
590restricts the type of data that is stored and how it
591is processed.
592Then how do we tell the a program or application
593which Binary Code to use?
594We simply explicitly tell the program which type
595of binary code to use (Declaration), or
596The application or program can guess the binary
597code from the context (Context).
59840
599How Do We Assign Memory Addresses?
600In earlier times assigning memory addresses was very
601tedious, but not difficult. If postal workers can deliver mail
602to millions of household addresses how difficult can this
603be?
604While delivering mail to 1313 Mocking Bird Lane is
605transparent, a processor accesses a memory addresses in
606binary. More importantly, a computer address can only
607store one piece of data. If there is attempt to store
608additional data at a computer address, the original contents
609are overwritten. (Wouldn't that be great - One piece of junk
610mail per address?)
611Memory addresses can be directly assigned by the computer
612programmer or a programmer can use program language
613variables to assign memory addresses.
61441
615Program Language Variables
616A Variable (name) in a computer program language is a symbolic
617name of a memory location and it has nothing to do with what is
618stored inside it.
619When individuals are introduced the concept of variables in a
620computer programming language, they recall the mathematic
621courses had previously drove them crazy. But, computer
622programs variables are very simple. The following is a simple
623Basic Program than calculates a payroll amount.
624HOURS = 40
625RATE = 8.00
626PAY = HOURS*RATE
627END
628HOURS, RATE and PAY are examples of a program language
629variables.
63042
631Program Language Variables
632Every programming language has specific rules for
633naming variables, e.g., length, permissible
634characters, use of embedded spaces, no reserved
635words, etc. Reserved words are key words
636reserved for the operations and functions of the
637specific language. For example, you can not
638name a variable PRINT, since it is a reserved to
639print information.
640Given the line HOURS = 40, how does BASIC know
641that HOURS is a variable name. Older languages
642frequently assume that it was variable if it was
643not a keyword.
64443
645Assigning a Memory Location to a Variable
646Executing the first line in our
647BASIC program, i.e.,
648HOURS=40, the first thing
649BASIC encounters on the
650left side of the equal sign is
651a variable named HOURS.
652If the BASIC variable named
653HOURS did not previously
654exist, a memory location is
655automatically assigned
656memory location.
657Address Contents
658999 HOURS =40
6591000 RATE=8.00
6601001 PAY=HOURS*RATE
6611002 END
6621003
6631004
6641005
6651006
666Inside the Program Language
667Variable Location
668HOURS 1004
66944
670Storing Data into a Variable
671After a BASIC variable has been
672created and a memory location
673has been assigned the equal sign
674is then executed.
675The equal sign in most programming
676language means "TO STORE".
677Store the contents of the right
678side into this variable.
679Trying to be persuasive my mother
680placed the following note on my
681bedroom drawer:
682UPPER.DRAWER=SOCKS
683After weeks of ignoring that note, she
684placed the second following note:
685PUT YOUR #%%$@ SOCKS IN THE
686UPPER DRAWER.
687Address Contents
688999 HOURS =40
6891000 RATE=8.00
6901001 PAY=HOURS*RATE
6911002 END
6921003
6931004 40
6941005
6951006
696Inside the Program Language
697Variable Location
698HOURS 1004
69945
700Retrieving the Contents of Variable
701Executing the third line of the
702program, i.e., PAY=HOURS*RATE,
703a mathematical expression is to
704be executed:
705AFTER a memory location has
706been assigned to PAY, but
707BEFORE the new contents are
708stored in PAY.
709BASIC sees the variable name HOURS
710and retrieves the contents from
711memory location 1004. BASIC
712sees the variable name RATE and
713retrieves the contents from
714memory location 1005.
715Now the expression is PAY=40*8.00.
716Address Contents
717999 HOURS =40
7181000 RATE=8.00
7191001 PAY=HOURS*RATE
7201002 END
7211003
7221004 40
7231005 8.00
7241006 320.00
725Inside the Program Language
726Variable Location
727HOURS 1004
728RATE 1005
729PAY 1006
73046
731Viewing the Contents of Variable
732I would require my students to
733executed this four line BASIC
734program. Students would
735complain, "Nothing
736Happened".
737I would explain that variables
738were created, contents were
739stored and mathematical
740expressions were executed.
741They retorted, "But, I didn't
742see anything"
743Ohhh, You want to see
744something!
745Address Contents
746999 HOURS =40
7471000 RATE=8.00
7481001 PAY=HOURS*RATE
7491002 END
7501003
7511004 40
7521005 8.00
7531006 320.00
754Inside the Program Language
755Variable Location
756HOURS 1004
757RATE 1005
758PAY 1006
75947
760Viewing the Contents of Variable
761The variable and their contents
762are stored in memory and are
763invisible to us humans.
764Each program language has
765various output commands. In
766this program an extra line
767PRINT PAY was added. My
768students were happy.
769As you view this letter "A",
770understand that not only is
771this character stored at an
772assigned memory location
773using a character code, the
774programmer must also use a
775extra command like PRINT to
776VIEW the variable contents.
777Address Contents
778999 HOURS =40
7791000 RATE=8.00
7801001 PAY=HOURS*RATE
7811002 PRINT PAY
7821003 END
7831004 40
7841005 8.00
7851006 320.00
786Inside the Program Language
787Variable Location
788HOURS 1004
789RATE 1005
790PAY 1006
79148
792Assigning Binary Codes to Variables
793The following program provides no information about the Binary Codes.
794HOURS = 40
795RATE = 8.00
796PAY = HOURS*RATE
797PRINT PAY
798END
799Binary codes are assigned to variables in computer programs through data types. Data
800types are reserved words used to assign binary codes and other data structures.
801The above BASIC program automatically assigns a data type based on the contents to the
802right of the equal sign. The value 40 is a integer, i.e., all digits and no decimal point.
803The value 8.00 is a floating point, i.e., all digits but one decimal point.
804Using the context method to assign data types and binary codes may make the
805programmer's life easier, but the context method has been known to make wrong
806decisions and produce unusual results during execution.
80749
808Declaring Variables
809The BASIC statement DIM can be used create variables and
810assign data types to the contents (binary codes).
811DIM HOURS AS INTEGER, RATE AS FLOAT
812HOURS = 40
813RATE = 8.00
814PAY = HOURS*RATE
815PRINT PAY
816END
817Most programming languages require that variables must be
818declared in the beginning of the code before any statement
819is executed.
82050
821Strongly-Typed Variables
822Visual Basic provides the option to declare variables or not declare variables. The VB
823statement OPTION EXPLICIT states that not only must a variable be declared, the data
824type rules for content and operation must be enforced and the data type can not be
825automatically changed during the execution of the program. These are features of a
826strongly-type variable.
827OPTION EXPLICIT
828DIM HOURS AS INTEGER, RATE AS FLOAT
829HOURS = 40
830RATE = 8.00
831PAY = HOURS*RATE
832PRINT PAY
833END
834Without Option Explicit, Visual Basic may temporarily convert the data type to perform an
835otherwise illegal operation. For example, a multiplication operation can be performed
836on the character content of "STEELERS". When illegal character data is converted to
837a numeric binary code the contents is changed to zero. Do you want to multiply by
838zero? Without a strongly-typed programming language this may happen. Easier is not
839better.
84051
841COBOL, Variables and Data Types
842COBOL is a strongly-typed programming language. The COBOL
843Data Division is used to declare variables before the
844Procedure Division. Consider the following COBOL
845declarations.
84601 THIS-IS-AN-INTEGER PIC 999.
84701 THIS-IS-SIGNED-INTEGER PIC S9999.
84801 THIS-IS-FLOATING-POINT PIC 999V99.
84901 THIS-IS-CHARACTER PIC XXXX.
850The PIC clause is used to create variables and assign default data
851types. PIC 9 uses default numeric codes, PIC X uses default
852character codes. To be more specific the USAGE IS clause
853may be used.
85401 THIS-IS-AN-INTEGER PIC 9(8) USAGE IS COMP-2.
85501 THIS-IS-CHARACTER PIC XXXX USAGE IS DISPLAY-6.
85652
857SQL, Column Names and Data Types
858When table is created in a relational database using a Data
859Definition SQL statement, column names (variables) and
860data types are declared. Consider the following:
861CREATE TABLE FACULTY (
862FID NUMBER(8,0),
863FACULTY_NAME CHAR(20),
864FACULTY_PAY NUMBER(10,2));
865Most relational databases are strong-typed. The NUMBER(8,0)
866declaration specifies a column width of 8 digits and zero
867digits to the left of the decimal point.
86853
869JAVA Variables and Data Types
870To encourage reusability of program code, the JAVA programming
871languages uses CLASSES. A Class contains both data (attributes)
872and methods (execution statements). When a JAVA programmer
873uses a previously-written class, one must be careful to know the
874variable (attribute) names and data types of data that was
875previously.
876The JAVA programmer may declare their own variables in a similar
877manner to Visual Basic. For example:
878int HOURS;
879double RATE;
880double PAY;
881string EMPNAME;
882While the syntax of the language may be different, the method to declare and
883type variables in all programming languages are roughly the same.
88454
885Excel Variables and Data Types
886At this point, we are going to ignore the more advanced issues of Excel
887Macros. Concentrating on a simple worksheet each cell, an
888intersection of a row and column, is roughly equivalent to a variable
889name. Instead of using the variable name HOURS, we store the integer
89040 into cell B4, i.e., Column B and Row 4.
891While you may have never thought that Excel cells were equivalent to
892using programming language variables, it is. You can even rename an
893Excel cell to a more user-friendly variable name using the Excel
894Define Name feature.
895Data types are automatically assigned when the data is originally typed
896into an Excel cell. Excel does not use the term data types. Rather it
897use the concept of Formatting. Excel formatting is combination of
898data types and visual display of data, all-in-one.
899Of course, you can easily know the difference between the Excel Data
900Division and Procedure Division, a.k.a. COBOL. All Excel Procedural
901statements are called formulas and they begin with an Equal Sign, i.e.,
902store the results into this cell. Sound familiar?
90355
904One Last Thought
905Reconsider the first statement of following Basic
906program:
907HOURS = 40
908RATE = 8.00
909PAY = HOURS*RATE
910PRINT PAY
911END
912Given the statement HOURS=40, how does the
913programming language or computer know that a
914person worked 40 hours? Before continuing write
915down your best answer.
91656
917Your Answer??????
918Given the statement HOURS=40, how does the
919programming language or computer know
920that a person worked 40 hours?
921HOURS = 40
922RATE = 8.00
923PAY = HOURS*RATE
924PRINT PAY
925END
926Was your answer, "I told the program that it was
927HOURS"?
92857
929Doing the Wash
930It is my premise that if you can do the wash, you can write computer
931programs. You did not read this statement incorrectly
932When you do the wash, you separate your clothes into various laundry
933baskets, i.e., "Towels basket", "Blue Jean Basket", and the "Gungy
934basket". (If could only teach my kids how to separate the laundry.)
935You organized you laundry and stored your laundry in baskets that
936you either assigned symbolic names or meaning.
937Are you ready? How does the wash machine know that this is your
938"Gungy Basket"? Before you recover from the shock of this question,
939let us repeat the question. How does the wash machine know that this
940is your "Gungy Basket"? Now you can answer it. "You stupid idiot,
941the wash machine doesn't have the slightest idea what you are
942washing.
943OK. Let me repeat. Given the statement HOURS=40, how does the
944programming language or computer know that a person worked 40
945hours?
94658
947What is a Variable?
948A Variable (name) in a computer program language is a symbolic
949name of a memory location and it has nothing to do with what is
950stored inside it.
951The statement HOURS=40 assigns a symbolic name to a memory
952location. The statement MUSTARD=40 will work just as well.
953The computer program does have any knowledge of data contents of
954a variable, no more than the wash machine knows anything about
955your laundry. You gave the laundry basket an artificial name and
956you named a memory location an artificial variable name.
957The programming language relies on YOU to remember the variable
958names and the meaning of its contents. There is nothing magical
959about variable names. You even do not have to spell the variable
960name correctly. Just be consistent with its spelling and remember
961where you put the wash. Opps I mean the data.
96259
963A Program Error
964What is wrong with the following program?
965HOURS = 40
966RATE = 8.00
967PAY = HOURS*RATE
968PRINT PAYROLL
969END
970The program stored the results from the calculation
971HOURS*RATE in a variable named PAY, but we
972printed the contents of a different memory location
973named PAYROLL. This inconsistent spelling problem
974could have been prevented if we had use OPTION
975EXPLICIT or any other strongly-typed programming
976language.
97760
978Another Program Error
979What is wrong with the following program?
980PAY = HOURS*RATE
981HOURS = 40
982RATE = 8.00
983PRINT PAY
984END
985The program stored the results from the calculation HOURS*RATE
986in a variable named PAY before the contents of HOURS and
987RATE were stored. When the statement PAY=HOURS*RATE
988was executed the contents of HOURS and RATE were either
989undefined or zero.
990Back to the Basics - Input the data, then process it, and finally
991output it.
99261
993Invisible Variables
994Once you understand the concepts and problems associate with program
995language variables there still remains one problem. In a computer
996programs, the contents of the variables (memory locations) are NOT
997display on your screen or web page. While the contents of these variables
998are VERY important, they are hidden in RAM.
999Older program languages would place PRINT statements to debug and view
1000the contents of these hidden variables. Today, there is wide availability of
1001program debuggers which permits you to watch these hidden variables
1002while the program is running. You can even watch the program execute
1003behind the scenes.
1004For example, a web page is a printed result from a lot of programming effort.
1005A Program debuggers permits one to watch behinds the scenes as the
1006web page is developed.
1007While some area of computer programming may be considered challenging,
1008most programs are no different that concepts of stores data into variables
1009presented here. The popularity of program debuggers have even made this
1010process easier.