<a name='top'></a>
# Integer Representations
## Directory:
- ### [Example 1: Convert this 4-bit binary number to its decimal equivalent.](#example-1)
- ### [Example 2: Convert this 12-bit binary number to its octal equivalent.](#example-2)
- ### [Example 3: Convert this 8-bit binary number to its hexadecimal equivalent.](#example-3)
- ### [Example 4: Convert this 3 digit base 8 number to its base 10 equivalent.](#example-4)
- ### [Example 5: Convert this 4-digit base 8 number to base 16 (hexadecimal form): 712.](#example-5)
- ### [Example 6: Convert this 3-digit base 16 number to base 2: 712.](#example-6)
- ### [Example 7: Convert this base 10 number to its base 2 equivalent.](#example-7)
---
## Intro to Integer Representation
- Integers can be expressed using any integer greater than one as a base.
- Binary Expansion
- Each digit is either a 0 or a 1
- An integer is just a bit string
<a name='example-1'></a>
## Example 1 <sub>[(back to top)](#top)</sub>
__[q0]__ Convert this 4-bit binary number to base 10 (decimal form): 1101.
```If-yes, goto [done]. If-no, goto [q1]```
> __[q1]__ Each bit in a binary number represents a power of 2. Consider that the conversion begins from the right. What power is 2 raised to for the right-most bit?
> ```If-yes, go to [q3]. If-no, goto [q2]```
>
> >__[q2]__ The right-most bit always represents 2 raised to the 0 power, and the power increases by one as you move from right to left. The second bit from the right would represent 2 raised to the power of 1 and so on. Using this logic, what power is 2 raised to for the left-most bit in this binary number?
> >```If-yes, go to [q4]. If-no, goto [q3]```
>
>>>__[q3]__ We know that for this example the right-most bit represents 2 raised to the power of 0, and the left-most bit represents 2 raised to the power of 3 with the power increasing from right to left. We have the values that each bit represents, but we now must determine what to multiply these values by. We know that we either multiply each power of 2 by 0 or 1. In this example, take the right-most bit. What value do we multiply 2 raised to the power of 0 by?
>>>```If-yes, go to [q5]. If-no, go to [q4]```
>
>>>>__[q4]__ Remember that in binary representation, there are only two values: 0 or 1. When there is a 1, we multiply the power of 2 for that bit by 1, and when there is a 0, we multiply the power of 2 for that bit by 0. Take the second bit from the right. We know that its value is 2 raised to the power of 1. The bit is a 0, so we multiply 2 raised to the power of 1 by 0. Remember that anything multiplied by 0 is 0. Now take the left-most bit, and multiply 2 to the power of 3 by the value of the bit. What is this value?
>>>>```If-yes, go to [q5]. If-no, go to [q6]```
>
>>>>>__[q5]__ Great work! We know that we multiply the power of 2 by 0 or 1 based on the bit the power of 2 represents. Considering this, we have 1 * 2^0, 0 * 2^1, 1 * 2^2, and 1 * 2^3. The final step is to sum these values. What is the sum of 1 * 2^0 + 0 * 2^1 + 1 * 2^2 + 1 * 2^3?
>>>>>```If-yes, go to [done]. If-no, go to [q6].```
>>>>>>__[q6]__ Final Answer
>>>>>> Taking 1101, we can split this number up starting from right to left into the following:
>>>>>> 2^0, 2^1, 2^2, 2^3.
>>>>>> We then multiply these values by the bit they represent.
>>>>>> 1 * 2^0, 0 * 2^1, 1 * 2^2, 1 * 2^3.
>>>>>> We then sum these values.
>>>>>> 1 * 2^0 + 0 * 2^1 + 1 * 2^2 + 1 * 2^3.
>>>>>> We then get 1 + 0 + 4 + 8 to give us our final answer of 13.
>>>>>> ```goto [done]```
<a name='example-2'></a>
## Example 2 <sub>[(back to top)](#top)</sub>
__[q0]__ Convert this 12-bit binary number to its base 8 (octal form): 1101 1111 0001.
```If-yes, goto [done]. If-no, goto [q1]```
> __[q1]__ In base 8, we know that there are only 8 digits from 0 to 7, so we can represent any digit in base 8 using 3 bits. Looking at the binary number given in the example, how many groups of 3 bits can be formed?
> ```If-yes, goto [q3]. If-no, goto [q2].```
>> __[q2]__ Knowing that we have a 12-bit binary number, and each base 8 number can be represented with 3 bits, we can determine that there are 4 groups of 3 bits in this binary number. Starting from the left, our first group of four bits is 110. What number do these bits represent?
>> ```If-yes, goto[q5]. If-no, goto [q3].```
>>> __[q3]__ We know that each bit represents a power of 2, and starting from right to left, the first bit represents 2 raised to the power of 0, the second bit is 2 raised to the power of 1, and the left-most bit represents 2 raised to the power of 2. Also remember that we multiply the power of 2's by the value of the bit they represent, i.e. * 0 if the bit is 0, and * 1 if the bit is 1. Considering this, reevaluate 110.
>>> ```If-yes, goto [q5]. If-no, goto [q4].```
>>>> __[q4]__ 110 represents the number 6, as we have 0 * 2^0 + 1 * 2^1 + 1 * 2^2. Following this format, what is the next group of 3 bits in this binary number?
>>>> ```If-yes, goto [q5]. If-no, goto [q6].```
>>>>> __[q5]__ Great work! Our next sequence of 3 bits is 111. What number do these bits represent?
>>>>> ```If-yes, goto [q7]. If-no, goto[q6].```
>>>>>> __[q6]__ Our next sequence of bits is 111. Following the same format, we know that each bit represents a power of 2, starting from right to left at 2 raised to the power of 0 and increasing by 1. We also multiply the power of 2s by the bit it represents, which will be * 1 for this example. What number, then, do these bits represent?
>>>>>> ```If-yes, goto [q7]. If-no, goto [q8].```
>>>>>>> __[q7]__ Great work! 111 represents the number 7. We now know that the first two digits of this octal numer are 67. Continuing the sequence, the next 3 bits are 110 and 001. Convert these binary numbers to base 8, and concatenate them onto 67. What is this number in base-8?
>>>>>>> ```If-yes, goto [done]. If-no, goto [q9].```
>>>>>>>> __[q8]__ 111 represents the number 7, as we have 1 * 2^0 + 1 * 2^1 + 1 * 2^2. We now know that the first two digits of this octal numer are 67. Continuing the sequence, the next 3 bits are 110 and 001. Start with 110. What is this number in base-8?
>>>>>>>> ```If-yes, goto [q10]. If-no, goto [q9].```
>>>>>>>>> __[q9]__ 110 represents the number 6, as we have 0 * 2^0 + 1 * 2^1 + 1 * 2^2. 001 represents 1, as we have 1 * 2^0 + 0 * 2^1 + 0 * 2^2. We know the first two octal digits are 67, and we need to concatenate the final two digits to this number. What is our final answer when concatenating 61 to 67?
>>>>>>>>> ``` If-yes, goto [done]. If-no, goto [q10].```
>>>>>>>>>> __[q10]__ Final Answer
>>>>>>>>>> Taking the binary number 1101 1111 0001, we can split this into 4 groups of 3 bits, as each number in base-8 can be represented by 3 bits. Our first sequence of 3 bits is 110, our second is 111, our third is 110 and our last sequence is 001. We can convert these sequences to their octal format.
>>>>>>>>>> 110 = 0 * 2^0 + 1 * 2^1 + 1 * 2^2 = 6
>>>>>>>>>> 111 = 1 * 2^0 + 1 * 2^1 + 1 * 2^2 = 7
>>>>>>>>>> 110 = 0 * 2^0 + 1 * 2^1 + 1 * 2^2 = 6
>>>>>>>>>> 001 = 1 * 2^0 + 0 * 2^1 + 0 * 2^2 = 1
>>>>>>>>>> Our last step is to concatenate these numbers to get our final answer in base 8. So, 1101 1111 0001 in base-8 is 6761.
>>>>>>>>>> ** Remember, if the binary number given cannot be split into even groups of 3, you can add leading 0s to the binary number in order to group the bits.
>>>>>>>>>> ```goto [done]```
<a name='example-3'></a>
## Example 3 <sub>[(back to top)](#top)</sub>
__[q0]__ Convert this 8-bit binary number to its base-16 (hexadecimal) equivalent: 0010 1110.
```If-yes, goto [done]. If-no, goto [q1].```
>__[q1]__ Remember that in base-16, there are only 16 digits (0 to 9 and A to F). We know that we can represent any base-16 number using 4 bits in binary. How many groups of 4 bits can we make from the binary number in this example?
>```If-yes, goto [q3]. If-no, goto [q2].```
>> __[q2]__ Because we are given an 8-bit binary number, we know that we can make two groups of 4 bits. 0010 and 1110. Let's start with 0010. What hexadecimal number does this represent?
>> ```If-yes, goto [q4]. If-no, goto [q5].```
>>> __[q3]__ Great work! We know that there are two groups of 4 bits in this binary number. We can know convert these to hexadecimal digits. Let's start with 0010. What hexadecimal number does this represent?
>>> ```If-yes, goto [q4]. If-no, goto [q5].```
>>>> __[q4]__ Great work! We know that the first sequence of 4 bits represents 2 in the hexadecimal number system. Let's move on to 1110. Keep in mind that any number over 9 is reprented by digits A to F. 10 is represented by A, 11 is represented by B, and so on. What hexadecimal number does 1110 represent?
>>>> ```If-yes, goto [q6]. If-no, goto [q7]. ```
>>>>> __[q5]__ 0010 represents 2 in the hexadecimal number system. Remember how we convert binary numbers to base-10. Starting from the right, each bit represents 2 raised to a power, starting with 2 raised to the power of 0 and increasing by one as we move right to left. So, starting from the right, we have 2^0, 2^1, 2^2, and 2^3. We then have to multiply these values by the value of the bit. We then get: 0 * 2^0, 1 * 2^1, 0 * 2^2 and 0 * 2^3. Our final step is to add these values together. So, 0 * 2^0 + 1 * 2^1 + 0 * 2^2 + 0 * 2^3 = 2. Now, using this same format, let's move on to 1110. Keep in mind that any number over 9 is represented by digits A to F. 10 is represented by A, 11 is represented by B, and so on. What hexadecimal number does 1110 represent?
>>>>> ```If-yes, goto [q6]. If-no, goto [q7].```
>>>>>> __[q6]__ Great work! You now know that our first hexadecimal digit is 2 and our second hexadecimal digit is E. Our final step is to simply concatenate these two digits. What is our final answer?
>>>>>> ```If-yes, goto [done]. If-no, goto [q8].```
>>>>>>> __[q7]__ 1110 represents the hexadecimal digit E. We know this because its numeric representation is 14, which we get from: 0 * 2^0 + 1 * 2^1 + 1 * 2^2 + 1 * 2^3. Every hexadecimal digit over 9 is represented by the digits A to F, starting with A = 10, B = 11, C = 12 and so on. We now know that our two hexadecimal digits are 2 and F. Our final step is to simply concatenate these two digits. What is our final answer?
>>>>>>> ``` If-yes, goto [done]. If-no, goto [q8].```
>>>>>>>> __[q8]__ Final Answer
>>>>>>>> We know that we can group the binary number 0010 1110 into two groups of 4 bits. The first group, 0010, represents the hexadecimal digit 2, which we get from converting binary to a simple base-10 number by doing the following: 0 * 2^0 + 1 * 2^1 + 0 * 2^2 + 0 * 2^3. Following a similar format, we know that 1110 is equal to the base-10 number 14 by doing the following: 0 * 2^0 + 1 * 2^1 + 1 * 2^2 + 1 * 2^3. But, we must consider that any number over 9 is represented by the digits A to F in the hexadecimal number system. So, starting with A represents 10, B represents 11, C represents 12, D reprsents 13, E represents 14, and F represents 15. So, our second hexadecimal digit is E. Our final step is to concatenate these two hexadecimal digits to get our final answer of 2E.
>>>>>>>> ```goto [done]```
<a name='example-4'></a>
## Example 4 <sub>[(back to top)](#top)</sub>
__[q0]__ Convert this 3 digit base 8 number to its base 10 equivalent: 361.
```If-yes, goto [done]. If-no, goto [q1].```
<a name='example-5'></a>
## Example 5 <sub>[(back to top)](#top)</sub>
__[q0]__ Convert this 3 digit base 8 number to base 16 (hexadecimal form): 712.
```If-yes, goto [done]. If-no, goto [q1]```
>__[q1]__ Each bit in the base 8 (octal) numbering system represents a number from 0 to 7, while each bit in the base 16 (hexadecimal) numbering system represents a number from 0 to 15. There is a common base that octal and hexadecimal numbers can both be converted into that makes conversion between octal and hexadecimal simpler. Can you convert this number into that base?
>```If-yes, goto [done]. If-no, goto [q2]```
>> __[q2]__ Numbers in base 8 and base 16 can both be converted into base 2 (binary) numbers, then reconverted into octal or hexadecimal numbers as desired. Can you convert this number from octal to binary?
>> ```If-yes, goto [q5]. If-no, goto [q3]```
>>> __[q3]__ To convert from base 8 to base 2, start with the rightmost digit and convert it into binary. Keep doing this with each octal digit moving from right to left until no more digits remain. What is this number?
>>> ```If-yes, goto [done]. If-no, goto [q4]```
>>>> __[q4]__ To convert this number into base 2, we begin at the left and convert each number into a 3 bit binary sequence, concatenating all the sequences at the end. The leftmost bit is 7, which is 111 in binary. Next we have 1, which is 001. Finally, we have the digit 2, which is 010. Concatenating these together gives us 111 001 010.
>>>> ```goto [q5]```
>>>>> __[q5]__ Now that you've converted 712 in base 8 into 111 001 010 in binary, it's time to convert to hexadecimal. Notice that to convert from octal to binary, each octal digit became 3 binary digits. This is because to represent the highest possible octal digit (7), we would need 3 binary bits. The highest possible octal digit is 16. How many bits should be grouped together to convert our binary number into base 16?
>>>>> ```If-yes, goto [q7]. If-no, goto [q6]```
>>>>>> __[q6]__ We need to group bits into groups of 4. Because the highest number we can represent in hexadecimal is 15 and we would need 4 bits to represent 15, grouping our binary number into sets of 4 helps us convert to hexadecimal.
>>>>>> ```goto [q7]```
>>>>>>> __[q7]__ Our binary number is 111001010. Grouping these bits into sets of 4 gives us 0001 1100 1010. Note the leading 0s added to the left to ensure that the entire number could be split into even sets of 4. Can you now convert this into hexadecimal?
>>>>>>> ```If-yes, goto [done]. If-no, goto [q8]```
>>>>>>>> __[q8]__ We can convert 0001 1100 1010 into hexadecimal by individually converting each group of 4 bits into a hexadecimal digit, and concatenating the digits. 0001 in binary is 1 in hexadecimal. 1100 is C hexadecimal. Finally, 1010 is A in hexadecimal. All together, this is 1CA in hexadecimal.
>>>>>>>> ```goto [done]```
<a name='example-6'></a>
## Example 6 <sub>[(back to top)](#top)</sub>
__[q0]__ Convert this 4 digit base 16 number to base 2 (binary): 2FA.
```If-yes, goto [done]. If-no, goto [q1]```
> __[q1]__ Before we begin converting, let's consider how many total bits we might need in our binary number. Remember that a digit in base 16 can take on any value between 0 and 15 (inclusive), and that bits must be either 1 or 0. How many bits can we use to represent this hexadecimal number in binary?
> ```If yes, goto [q5]. If no, goto [q2]```
> __[q2]__ Consider the amount of bits needed to go from one hexadecimal digit into binary. Hexadecimal digits can take on a value as high as 15. How many bits are needed to represent the number 15?
>> ```If yes, goto [q5], If no, go to [q3]```
>> __[q3]__ We would need 4 bits to represent the highest possible hexadecimal digit value, 15. This would be 1111 in binary. If 4 bits are needed to represent 1 hex digit, how many bits are needed to represent this number?
>>> If yes, goto [q5], If no, goto [q4]
>>>> __[q4]__ This hex number would need 12 bits to represent, as the number is 3 digits long, and it takes 4 bits to represent 1 hex digit. As such, we need 4 * 3 = 12 bits.
>>>> ```Goto [q5]```
>>>>> __[q5]__ Now that we know how many bits we will have in our number, let's begin converting this number from hexadecimal to binary. What would 2 in hexadecimal become when it is converted to binary?
>>>>>> ```If yes, goto [q7], If no, goto [q6]```
>>>>>> __[q6]__ Remember, converting digits 0-9 in hexadecimal to binary is the same as converting them from 0-9 in decimal to binary. Consider the 4 bit representation of the binary number. In this case, 2 simply converts into 0010.
>>>>>>> ```goto [q7]```
>>>>>>>> __[q7]__ Now that we've converted 2 from hexadecimal to binary, let's tackle the next digit, F. Can you convert this from decimal to binary?
>>>>>>>>>> ```If yes, goto [q11], If no, goto [q8]```
>>>>>>>>>> __[q8]__ F, unlike the digits 1-9, does not have a clear decimal representation. Remember that in hexadecimal, digits begin with A after the number 9, and continue all the way up to F. What decimal number does F represent?
>>>>>>>>>> ```If yes, goto [q11], If no, goto [q9]```
>>>>>>>>>>> __[q9]__ 0-9 are represented in hexadecimal just as they are in decimal. After the number 9 in decimal, though, hexadecimal digits are denoted by letters. A is 10, B is 11, C is 12, D is 13, E is 14, and F is 15. Can you now convert F from hexadecimal to binary?
>>>>>>>>>>> ```If yes, goto [q11], If no, goto [q10]```
>>>>>>>>>>>> __[q10]__ To convert F into binary, consider F's equivalent decimal representation: 15. 15, in binary, is 1111: 1 * 2^3 + 1 * 2^2 + 1 * 2^1 + 1 * 2^0.
>>>>>>>>>>>> ```goto [q11]```
>>>>>>>>>>>>> __[q11]__ All that's left is converting the last digit, A into binary. What would this be?
>>>>>>>>>>>>> ```If yes, goto [q13], if no, goto [q12]```
>>>>>>>>>>>>>> __[q12]__ A has a decimal representation of 10. In binary, this is 1010.
>>>>>>>>>>>>>> ```goto [q13]```
>>>>>>>>>>>>>>> Putting all of these bits together, we get our final answer: 0010 1111 1010
<a name='example-7'></a>
## Example 7 <sub>[(back to top)](#top)</sub>
__[q0]__ Convert this base 10 number to its base 2 equivalent: 25.
```If-yes, goto [done]. If-no, goto [q1].```
>__[q1]__
## ToDo
- Base 8 to 10 - Christina
- Base 10 to 2 (Mod)
- Base 10 to 8 (Mod)
- Base 10 to 16 (Mod)
- Base 16 to 8 - Sudhan
- Base 16 to 10 - Sudhan