Module 4 - Theory
Assignment 4.03


Objectives: After studying the Module 3 Theory Button, you will be able to:

  1. Distinguish between features of the decimal number system and the octal number system.
  2. Use a calculator to perform number base conversions.
  3. Use the OCT$ and HEX$ functions to perform base conversion.

 

YOUR LESSON

Introduction

We humans use the decimal number system (base 10) for most of our arithmetic. With just 10 decimal digits (0-9), we can do anything from simple arithmetic all the way to calculus and beyond. But, computers primarily use the binary number system (base 2) and for convenience, programmers often convert binary into hexadecimal (base 16).

In the Theory section of Modules 2 and 3, you learned how to use the successive division method to convert between binary and decimal and between hexadecimal and decimal.

The conversion algorithm (the steps in the conversion process) to go back and forth between base 10 and any other base is essentially the same, you just use different numbers.

In other words, you followed the same pattern on the worksheets to go back and forth between binary and decimal and back and forth between hexadecimal and decimal.


Now that you know there is a pattern, it is time to explore one last number system that programmers use, the octal number system...which is also known as base 8. If there are ten digits (0-9) in the decimal number system, two digits (0 and 1) in the binary number system , and sixteen digits (0-9 and A-F) in the hexadecimal number system, how many digits would you expect to be in the octal number system?


Can you predict the answer?

If there are ten digits (0-9) in the decimal number system, two digits (0 and 1) in the binary number system , and sixteen digits (0-9 and A-F) in the hexadecimal number system, which digits would you expect in the octal number system?

0, 1, 2, 3, 4, 5, 6, 7, 8
0, 1, 2, 3, 4, 5, 6, 7
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
I don't know.


Was your prediction correct? The octal number system (base 8) uses the digits 0-7.

Because binary numbers get very large quickly, the octal number system and the hexadecimal number system are sometimes used as shorthand notation. Like the hexadecimal number system, computer programmers like the octal number system because:

    1. it has many of the advantages of binary,
    2. it is relates to the state of a transistor, and
    3. it is concise and easy for humans to understand.

In this lesson, you will learn how to convert between the octal number system and the old familiar decimal number system. After that you can consider yourself an expert at converting between number systems.

But there is a surprise waiting for you!


"OCT"...This is easy!

Base 8 is the octal number system. Base 8 only uses the digits 0-7. Here are a few examples.

672
123
210
542


You have seen in the decimal number system, the binary number system, and the hexadecimal number system that the place value of a digit is determined by where it stands in relation to the other digits in a number. The same thing is true for octal numbers. To convert from octal to decimal , you need to know the place values of the octal number system.

The place values in the octal number system increase by powers of 8, for example 1, 8, 64, etc., etc. Look at the example illustrated in the following table. This table shows how to convert the octal number 542 to it's decimal equivalent.

 

Powers of 8
8^2
8^1
8^0
Octal Place Value
64
8
1
Octal Number
5
4
2
Decimal Number
320
32
2

Directly above in row 1 you see the first three powers of 8.

In row 2 you see the place value of the first three digits in the octal number system. From right to left they are 1, 8, and 64. The place values are increasing by powers of 8 as shown in the first row.

Once again, pay attention because here is the key! If you multiply the octal place value (row 2) times the octal number (row 3), you get the decimal equivalent (row 4). Then if you add the decimal equivalents up (320+32+2), you get the decimal number that the octal number 542 is equal to: 354. Wow!

The main thing that students always forget is that any number to the zero power is always 1.

  • in the decimal number system 10^0=1.
  • in the hexadecimal number system 16^0=1
  • in the binary number system 2^0 = 1.
  • in the octal number system 8^0=1.
OK, that's the theory behind how you convert from octal to decimal. And, as you probably suspect by now, converting from decimal to octal using the successive division method follows the same pattern that was used when converting from decimal to binary and decimal to hexadecimal.

Practice Time

By now you understand the concept behind base conversions, right? Place value is the key and you either use the chart method or the successive division method to go back and forth between base 10 and another base...any base. The conversions to and from the octal number system would be exactly the same pattern, right?

That's right, QB, you were promised a surprise (rollover the image now)!

Now that you know the secret behind base conversions, the truth can be revealed. While programmers (like you) need to clearly understand the concept and process of base conversion, most don't use a pencil and paper to do the calculations! (Sad, but true.) Let's look at three alternatives to doing base conversions by hand.


Hand Held Calculators: Many of you may have a calculator that can perform base number conversions. Many of the "scientific" calculators made today have a variety of function keys and some of them are designed to convert between bases. Check your calculator and see if yours has this handy feature.


Windows Calculator Application: One of the built-in applications that comes with Microsoft Windows is a calculator. Follow the steps to the right to access your calculator. After following the steps listed, you should see an expanded calculator with the following buttons available.

  1. Select the Windows Start button at the bottom of your screen.
  2. Select the Programs option from the menu.
  3. Select the Applications option from the menu.
  4. Select the Calculator option from the menu.
  5. Select the View option from the Calculator menu bar.
  6. Choose the Scientific option from the menu.

Above and to the right of these buttons you will notice a window where you can type a number. With the Dec button selected (this indicates the number you are entering will be in the decimal number system), type in a number...say 542. Then click on any Hex, Oct, and Bin and the decimal number 542 will be converted to the other bases.

Next, select the Bin button and type in 11010110. Select Hex, Dec, and Oct and see what that binary number is in the other number systems.

What would happen if you select Bin and try to type a number like 693? Be sure that Bin is selected and then try it and see. The number 693 is not a binary number, so the calculator would not accept it.

OK? With your knowledge of how number systems work (based on all those worksheets you did), you should have a pretty good idea of what the Windows Calculator is doing. Play with the Hex and Oct buttons and then move on to the next section.

 

QBasic Functions: The calculator method is useful, but now for the rest of the story. Sometime programs need to use hexadecimal numbers or octal numbers during program execution. So, QBasic has a way to perform base conversions back and forth between decimal and hexadecimal and between decimal and octal. (There is little need to do this for binary numbers.)

The HEX$ function converts a decimal number into its hexadecimal equivalent.
The OCT$ function converts a decimal number into its octal equivalent.

There is one important condition. The decimal number must either be in the range of 0 to +65535 or from -32768 to +32768.

Try this small program and see what happens. Save the program as bases1.bas.

CLS
INPUT
"Enter a decimal number: ",number
H$=HEX$(number)
O$=OCT$(number)
PRINT "The hexadecimal value of ";number;" is "; H$
PRINT "The octal value of ";number;" is ";O$
END

That's pretty interesting, but inquiring minds will want to know...can you convert from hexadecimal or octal back to decimal? But of course mon programmer! You can do it, but it's not quite as functional due to some peculiarities of the Basic programming language that are beyond the scope of this course. Try the following. You must put the & (ampersand) symbol in front of the hexadecimal number and the octal number. You must also put the letter H in front of a hexadecimal number and the letter O in front of an octal number. Save the program as bases2.bas.

CLS
PRINT "The hexadecimal number F9A is equal to ";&HF9A
PRINT "The octal number 723 is equal to ";&O723
END

When you run this little program, &HF9A should be equal to 3994 and &O723 should be equal to 467.


YOUR ASSIGNMENT

Don't worry, there are no worksheets for this assignment! Instead, use the Windows Calculator (or either of the short programs you wrote to convert between bases) and fill in the missing numbers in the following table. You will need the answers for a Quiz.

Decimal Binary Hex Octal    
2938          
  1111001110        
    C9A4      
      506    

After you fill in the table with the correct answers, review the information in this lesson on the octal number system and the QBasic functions HEX$ and OCT$ used for base conversions. When you are ready, go to Assignment 04.03 and take the Quiz. You will need your answers in the table above for part of the Quiz.

A Quick Review

Before you move on to the next assignment, please make sure that you can do the following.

  1. Distinguish between features of the decimal number system and the octal number system.
  2. Use a calculator to perform number base conversions.
  3. Use the OCT$ and HEX$ functions to perform base conversion.

If you know how to use the OCT$ and HEX$ functions or a calculator to convert between number systems, you won't ever have to do it by hand again, but at least you will know how!


After you have completed the Quiz for Assignment 04.03, please continue to the next assignment.


previousnext