Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

6) a) Convert the following 2 numbers to binary, octal and hex. 2.875, 0.1796875

ID: 3603208 • Letter: 6

Question

6) a) Convert the following 2 numbers to binary, octal and hex.

   2.875, 0.1796875

When done, convert the following to a float representation

by the definition in class. In other words, I want an 8 digit hex

      number representation using the 4 byte float specification

defined in class.

   b) Do the same for a) given they are negative values.

   c) Convert the float representations of the following into

   the decimal number given the definition in class.

   59999901, 59999902, A66667FE

Note: This doesn't require a program, however, I want you to write a

simple function that outputs the answers you did by hand with "cout".

Explanation / Answer

How a Number System Works

Number systems are used to describe the quantity of something or represent certain information. Because of this, I can say that the word "calculator" contains ten letters. Our number system, the decimal system, uses ten symbols. Therefore, decimal is said to be Base Ten. By describing systems with bases, we can gain an understanding of how that particular system works.

When we count in Base Ten, we count starting with zero and going up to nine in order.

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, …

Once we reach the last symbol, we create a new placement in front of the first and count that up.

8, 9, 10, 11, 12, … , 19, 20, …


This continues when we run out of symbols for that placement. So, after 99, we go to 100.

The placement of a symbol indicates how much it is worth. Each additional placement is an additional power of 10. Consider the number of 2853. We know this number is quite large, for example, if it pertains to the number of apples in a basket. That's a lot of apples. How do we know it is large? We look at the number of digits.

Each additional placement is an additional power of 10, as stated above. Consider this chart.

Each additional digit represents a higher and higher quantity. This is applicable for Base 10 as well as to other bases. Knowing this will help you understand the other bases better.

Binary

Binary is another way of saying Base Two. So, in a binary number system, there are only two symbols used to represent numbers: 0 and 1. When we count up from zero in binary, we run out of symbols much more frequently.

0, 1, …

From here, there are no more symbols. We do not go to 2 because in binary, a 2 doesn't exist. Instead, we use 10. In a binary system, 10 is equal to 2 in decimal.

We can count further.

Just like in decimal, we know that the more digits there are, the larger the number. However, in binary, we use powers of two. In the binary number 1001101, we can create a chart to find out what this really means.

Since this is base two, however, the numbers don't get quite as large as it does in decimal. Even still, a binary number with 10 digits would be larger than 1000 in decimal.

The binary system is useful in computer science and electrical engineering. Transistors operate from the binary system, and transistors are found in practically all electronic devices. A 0 means no current, and a 1 means to allow current. With various transistors turning on and off, signals and electricity is sent to do various things such as making a call or putting these letters on the screen.

Computers and electronics work with bytes or eight digit binary numbers. Each byte has encoded information that a computer is able to understand. Many bytes are stringed together to form digital data that can be stored for use later.

Octal

Octal is another number system with less symbols to use than our conventional number system. Octal is fancy for Base Eight meaning eight symbols are used to represent all the quantities. They are 0, 1, 2, 3, 4, 5, 6, and 7. When we count up one from the 7, we need a new placement to represent what we call 8 since an 8 doesn't exist in Octal. So, after 7 is 10.

Just like how we used powers of ten in decimal and powers of two in binary, to determine the value of a number we will use powers of 8 since this is Base Eight. Consider the number 3623 in base eight.

Each additional placement to the left has more value than it did in binary. The third digit from the right in binary only represented 23-1, which is 4. In octal, that is 83-1 which is 64.

Hexadecimal

The hexadecimal system is Base Sixteen. As its base implies, this number system uses sixteen symbols to represent numbers. Unlike binary and octal, hexadecimal has six additional symbols that it uses beyond the conventional ones found in decimal. But what comes after 9? 10 is not a single digit but two… Fortunately, the convention is that once additional symbols are needed beyond the normal ten, letters are to be used. So, in hexadecimal, the total list of symbols to use is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. In a digital display, the numbers B and D are lowercase.

When counting in hexadecimal, you count 0, 1, 2, and so on. However, when you reach 9, you go directly to A. Then, you count B, C, D, E, and F. But what is next? We are out of symbols! When we run out of symbols, we create a new digit placement and move on. So after F is 10. You count further until you reach 19. After 19, the next number is 1A. This goes on forever.

Digits are explained as powers of 16. Consider the hexadecimal number 2DB7.

As you can see, placements in hexadecimal are worth a whole lot more than in any of the other three number systems.

Conversion

It is important to know that 364 in octal is not equal to the normal 364. This is just like how a 10 in binary is certainly not 10 in decimal. 10 in binary (this will be written as 102 from now on) is equal to 2. 108 is equal to 8. How on earth do we know this? What is 20C.38F16, and how do we find out?

Here is why it is important to understand how the number systems work. By using our powers of the base number, it becomes possible to turn any number to decimal and from decimal to any number.

Base to Decimal

So, we know that 3648 is not equal to the decimal 364. Then what is it? There is a simple method in converting from any base to the decimal base ten. If you remember how we dissected the numbers above, we used powers, such as 24, and ended up with a number we understand. This is exactly what we do to convert from a base to decimal. We find out the true value of each digit according to their placement and add them together.

In a formula, this algorithm looks like:

(1)

V10=vpBp+vp1Bp1+...+v1B+v0

Where V10 is the decimal value, v is the digit in a placement, p is the placement from the right of the number assuming the rightmost placement is 0, and B is the starting base. Do not be daunted by the formula! We are going to go through this one step at a time.

So, let us say we had the simple hexadecimal number 2B. We want to know what this number is in decimal so that we can understand it better. How do we do this?

Let us use the formula above. Define every variable first. We want to find V10, so that is unknown. The number 2B16 has two positions since it has two digits. p therefore is one less than that1, so p is 1. The number is in base 16, so B is 16. Finally, we want to know what v is, but there are multiple v's. You have v1 and v0. This refers to the value of the digit in the subscripted position. v1 refers to the digit in position one (the second digit from the right). So, v1 is 2. v0 is the first digit which is B. In the case of the conversion, you must convert all the letters to what they are in decimal. B is 11 in decimal, so v0 is 11.

Now, plug all this into the formula:

(2)

V10=2(161)+11(160)V10=2(16)+11(1)V10=32+11V10=43

Therefore, 2B16 is equal to 43.

Now, let me explain how this works. Remember how digit placement affects the actual value? For example, in the decimal number 123, the "1" represents 100 which is 1*102. The "2" is 20, or 2*101. Likewise, in the number 2B16, the "2" is 2*161, and the B is 11*160.

We can determine the value of numbers in this way. For the number 3648, we will make a chart that exposes the decimal value of each individual digit. Then, we can add them up so that we have the whole. The number has three digits, so starting from the right, we have position 0, position 1, and position 2. Since this is base eight, we will use powers of 8.

Now, 82 is 64. 81 is 8. 80 is 1. Now what?

Remember what we did with the decimal number 123? We took the value of the digit times the respective power. So, considering this further…

Now, we add the values together to get 244. Therefore, 3648 is equal to 24410.

In the same way that for 123, we say there is one group of 100, two groups of 10, and three groups of 1, for octal and the number 364, there are three groups of 64, six groups of 8, and four groups of 1.

Decimal to Base

Just like how we can convert from any base to decimal, it is possible to convert decimal to any base. Let us say that we want to represent the number 23610 in binary, octal, and hexadecimal. What we need to do is pretty much reverse whatever we did above. There isn't really a good formula for this, but there is an algorithm that you can follow which will help accomplish what we want.

(3)

(1)LetP=int(VB)(2)Letv=int(V÷BP)(visthenextdigittotheright)(3)MakeV=VvBp(4)Repeatsteps1through3untilp=0

This algorithm may look confusing at first, but let us go through an example to see how it can be used. We want to represent 236 in binary, octal, and hexadecimal. So, let's try getting it to binary first.

The first step is to make p equal to int(VB). B is the base we want to convert to which is 2. The V is the number we want to convert, 236. Essentially, we are taking the square root of 236 and disregarding the decimal part. Doing this makes p become 7.

Step two says to let v equal our number V divided by Bp. Bp is 27, or 128, and the integer part of 236 divided by 128 is 1. Therefore, our first digit on the left is 1. Now, we actually change V to become V minus the digit times the Bp. So, V will now be 236-128, or 108.

We simply repeat the process until the p becomes a zero. When p becomes zero, we complete the steps a last time and then end.

So, since V is now 108, p becomes 6. 108 divided by 26 is 1. The 1 goes to the right of the 1, so now we have 11. V becomes 44 since 108-64 is 44.

How?

Now you might be asking yourself how to read these numbers. Well, that’s not so difficult. First, I’ll give a general mathematical explanation which can be fit into one formula:

(4)

V=vBP

In human language: the value of the cipher in the number is equal to the value of the cipher on its own multiplied by the base of the number system to the power of the position of the cipher from left to right in the number, starting at 0. Read that a few times and try to understand it.

Thus, the value of a digit in binary doubles every time we move to the left. (see table below)

From this follows that every hexadecimal cipher can be split up into 4 binary digits. In computer language: a nibble. Now take a look at the following table:

Another interesting point: look at the value in the column top. Then look at the values. You see what I mean? Yeah, you’re right! The bits switch on and off following their value. The value of the first digit (starting from the right), goes like this: 0,1,0,1,0,1,0,1,0,1,… Second digit: 0,0,1,1,0,0,1,1,0,0,1,1,0,0… Third digit (value=4): 0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,… And so on…

Now, what about greater numbers? Therefore we’ll need an extra digit. (but I think you figured that out by yourself). For the values starting from 16, our table looks like this:

For octals, this is similar, the only difference is that we need only 3 digits to express the values 1->7. Our table looks like this:

Conversion.

In the latter topic I explained the logic behind the binary, hexadecimal and octal number systems. Now I’ll explain something more practical. If you fully understood the previous thing you can skip this topic.

From decimal to binary

An example:
Convert 68 to binary:

From binary to decimal

An example:
Convert 101100 to decimal:

From decimal to hexadecimal.

THIS IS ONLY ONE OF THE MANY WAYS!

(you can add zeroes at the beginning if the number of bits is not divisible by 4, because, just as in decimal, these don’t matter)

An example:
Convert 39 to hexadecimal:

From hexadecimal to decimal

*Check the formula in the first paragraph and use it on the ciphers in your hexadecimal number. (this actually works for any conversion to decimal notation)

An example:
Convert 1AB to decimal:

103 102 101 100 digit digit digit digit *1000 *100 *10 *1
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote