Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
introduction_to_digital_systems:number_systems [2021/09/19 03:05] – tfischer | introduction_to_digital_systems:number_systems [2024/10/29 11:54] (aktuell) – mexleadmin | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | ====== 2. Number | + | ====== 2 Number |
+ | ===== 2.1 Types of Number Systems ===== | ||
- | ===== 2.1 Number System ===== | + | In the previous chapter, we had a look at the way a processor (and a computer) can deal with the digital values '$0$' and '$1$'. |
- | + | However, we haven' | |
- | In the previous chapter we had a look onto the way a processor (and a computer) can deal with the digital values ' | + | |
- | However, we haven' | + | |
==== 2.1.1 Addition Systems ==== | ==== 2.1.1 Addition Systems ==== | ||
- | The first used number system | + | The first used number system |
+ | These are also still in use: when enjoying a German beer in the beer garden the waiter is counting the ' | ||
<WRAP center> | <WRAP center> | ||
Zeile 18: | Zeile 18: | ||
</ | </ | ||
- | In ancient Rome these systems were deeper elaborated. | + | In ancient Rome, these systems were deeper elaborated. |
- | * $I = 1$ | + | * $\rm I = 1$ |
- | * $V = 5$ | + | * $\rm V = 5$ |
- | * $X = 10$ | + | * $\rm X = 10$ |
- | * $L = 50$ | + | * $\rm L = 50$ |
- | * $C = 100$ | + | * $\rm C = 100$ |
- | * $D = 500$ | + | * $\rm D = 500$ |
- | * $M = 1000$ | + | * $\rm M = 1000$ |
- | Besides this represenation for quantities also the position of the symbol in the **numeral** | + | Besides this representation of quantities also the position of the symbol in the **numeral** |
- | * In general: the letters have to be arranged decreasing from left to right. For example $MDCI = 1601$ | + | * In general: the letters have to be arranged decreasing from left to right. For example $\rm MDCI = 1601$ |
- | * There are deviations of this rule: When up to three of the a lower symbol is written to the left, these have to be subtracted. Sounds complicated? | + | * There are deviations of this rule: When up to three of the lower symbols are written to the left, these have to be subtracted. Sounds complicated? |
- | It becomes even more complicated, when trying to calculate with the numbers: what is the result of the multiplication $CCMXXXVII \cdot DDDIIX$$? | + | It becomes even more complicated when trying to calculate with the numbers: what is the result of the multiplication $\rm CCMXXXVII \cdot DDDIIX$? |
==== 2.1.2 Decimal and Binary ==== | ==== 2.1.2 Decimal and Binary ==== | ||
Zeile 39: | Zeile 39: | ||
We 'just know' what a number like $23$ means. However, for understanding how the computer works we have to investigate this gut feeling and put some technical terms onto it. | We 'just know' what a number like $23$ means. However, for understanding how the computer works we have to investigate this gut feeling and put some technical terms onto it. | ||
- | - We are accustomed to count with our fingers from $1$ to $10$. For this we have 10 symbols to count: $0, | + | - We are accustomed to counting |
- The amount of the digits is called **base** $B$. We are used to the decimal base $B=10$, in logic we used binary (also called dual) $B=2$. | - The amount of the digits is called **base** $B$. We are used to the decimal base $B=10$, in logic we used binary (also called dual) $B=2$. | ||
- When we count beyond the maximum number we are used to ' | - When we count beyond the maximum number we are used to ' | ||
- | - Each position gets numbered: the ones count 0, the tens 1, the hundreds 2, the thousands 3 and so on. This ' | + | - Each position gets numbered: the ones count 0, the tens 1, the hundreds 2, the thousands 3, and so on. This ' |
- Knowing the index, also the 'worth of the position' | - Knowing the index, also the 'worth of the position' | ||
- A **numeral** as a group of digits represents what is commonly known as a number. | - A **numeral** as a group of digits represents what is commonly known as a number. | ||
- A **code** or **encoding** means a way to translate one way to display information into another. E.g. A decimal numeral into a Binary, or an idea of an algorithm into a computer language. | - A **code** or **encoding** means a way to translate one way to display information into another. E.g. A decimal numeral into a Binary, or an idea of an algorithm into a computer language. | ||
- | In order to recapitulate this for $B=10$ we will calculate the amount of a dezimal | + | To recapitulate this for $B=10$ we will calculate the amount of a decimal |
{{url> | {{url> | ||
Zeile 56: | Zeile 56: | ||
So, what did we find out? | So, what did we find out? | ||
- | * The shown process is a relatively simple way to convert binary numerals to decimal. | + | * The shown process is a relatively simple way to convert binary numerals to decimals. |
- | * A 8 digit binary numeral is equal an up to 3 digit decimal numeral. | + | * A 8-digit binary numeral is equal to 3 digit decimal numeral. |
- | Therefore, | + | Therefore, it would be better to have a more structured way of presenting the numerals which are used in the processor. |
- | Internally, the processor just knows 0's and 1's. But investigating a huge bunch of these (e.g. when analyzing the internal memory or a file) is not really | + | Internally, the processor just knows 0's and 1's. But investigating a huge bunch of these (e.g. when analyzing the internal memory or a file) is not catchy in order to understand anything. |
The first step is to group the bits: | The first step is to group the bits: | ||
* 4 bits are called a **nibble** (the name derives from 'to bit' and 'to nibble' | * 4 bits are called a **nibble** (the name derives from 'to bit' and 'to nibble' | ||
* 8 bits are called a **byte** | * 8 bits are called a **byte** | ||
- | * 16 bits are (usually) called a **word**. In details, this depends on the processor. | + | * 16 bits are (usually) called a **word**. In detail, this depends on the processor. |
* 32 bits are (usually) called a **double word**. Like the word this also depends on the processor. | * 32 bits are (usually) called a **double word**. Like the word this also depends on the processor. | ||
By this, one can separate parts of information (e.g. in a file) better. | By this, one can separate parts of information (e.g. in a file) better. | ||
- | It is also important to mark the order of the bits. For decimal numerals like $42$ the rightmost digit has always the lowest value. The technical term for the ' | + | It is also important to mark the order of the bits. For decimal numerals like $42$ the rightmost digit has always the lowest value. The technical term for the ' |
What is still missing are expressions for large amounts of data. We can describe these using prefixes and the powers of two. | What is still missing are expressions for large amounts of data. We can describe these using prefixes and the powers of two. | ||
You may already know the prefixes such as kilo and mega, but it is worth brushing up on the powers of two. | You may already know the prefixes such as kilo and mega, but it is worth brushing up on the powers of two. | ||
The easiest way to illustrate this is with a chessboard. Maybe you know the legend of the inventor of the chessboard, who was granted a wish by the king. | The easiest way to illustrate this is with a chessboard. Maybe you know the legend of the inventor of the chessboard, who was granted a wish by the king. | ||
- | His wish was that the king would give him one grain of rice on the first chessboard square and every time twice as much on each subsequent | + | His wish was that the king would give him one grain of rice on the first chessboard square and every time twice as much on each subsequent |
We'll play through this briefly, here to write down the powers of two: | We'll play through this briefly, here to write down the powers of two: | ||
* in the first square we enter two to the power of 0, which results in 1. | * in the first square we enter two to the power of 0, which results in 1. | ||
* In the second square, two to the power of 1, resulting in two. | * In the second square, two to the power of 1, resulting in two. | ||
- | * Then 4, 8, 16, 32, 64, 128, 256, 512 and then in the next line two to the power of ten, resulting in 1024. | + | * Then 4, 8, 16, 32, 64, 128, 256, 512, and then in the next line two to the power of ten, resulting in 1024. |
- | You should definitely remember | + | You should definitely remember |
They are not only important for the exam, but also for the following semesters and computer science. | They are not only important for the exam, but also for the following semesters and computer science. | ||
Zeile 89: | Zeile 89: | ||
</ | </ | ||
- | 1024 bit is also called **kbit** or **kilobit** in the semiconductor industry. You will notice here that the kilo is slightly more than 1000. To make it easier to distinguish, | + | 1024 bit is also called **kbit** or **kilobit** in the semiconductor industry. You will notice here that the kilo is slightly more than 1000. To make it easier to distinguish, |
The nomenclature continues in the same way for 2^30 and 2^40: gibibit and tebibit. | The nomenclature continues in the same way for 2^30 and 2^40: gibibit and tebibit. | ||
==== 2.1.3 Other bases ==== | ==== 2.1.3 Other bases ==== | ||
- | When looking | + | When looking |
Already in the chapter before a numeral like $110$ could either be $110$ in decimal or $110$ in binary, which is $6$ in decimal. \\ | Already in the chapter before a numeral like $110$ could either be $110$ in decimal or $110$ in binary, which is $6$ in decimal. \\ | ||
In the following the base will be written as a subscript: $110_2 = 6_{10}$. | In the following the base will be written as a subscript: $110_2 = 6_{10}$. | ||
- | In the end of this subchapter we will also see other ways to mark the base. | + | At the end of this subchapter, we will also see other ways to mark the base. |
=== Hexadecimal === | === Hexadecimal === | ||
- | With the ideas of the prevoius | + | With the ideas of the previous |
- | One important base is $16$ for hexadecimal numerals. There, we need 16 distinguishable symbols: $0, | + | One important base is $16$ for hexadecimal numerals. There, we need 16 distinguishable symbols: $0, 1, 2, 3, 4, 5, 6, 7, 8, 9, \rm A, B, C, D, E, F$. |
- | With this digits it is possible to encode (=rewrite by other means) exactly 4 bit or one nibble. | + | With these digits, it is possible to encode (=rewrite by other means) exactly 4 bits or one nibble. |
The encoding will be as follows: | The encoding will be as follows: | ||
Zeile 119: | Zeile 119: | ||
|$1000_2$ | $8_{10}$ | |$1000_2$ | $8_{10}$ | ||
|$1001_2$ | $9_{10}$ | |$1001_2$ | $9_{10}$ | ||
- | |$1010_2$ | $10_{10}$ | $A_{16}$ | + | |$1010_2$ | $10_{10}$ |
- | |$1011_2$ | $11_{10}$ | $B_{16}$ | + | |$1011_2$ | $11_{10}$ |
- | |$1100_2$ | $12_{10}$ | $C_{16}$ | + | |$1100_2$ | $12_{10}$ |
- | |$1101_2$ | $13_{10}$ | $D_{16}$ | + | |$1101_2$ | $13_{10}$ |
- | |$1110_2$ | $14_{10}$ | $E_{16}$ | + | |$1110_2$ | $14_{10}$ |
- | |$1111_2$ | $15_{10}$ | $F_{16}$ | + | |$1111_2$ | $15_{10}$ |
</ | </ | ||
This directly reduces the necessary amount of digits to show data. | This directly reduces the necessary amount of digits to show data. | ||
The hexadecimal representation is for example used in the file type '' | The hexadecimal representation is for example used in the file type '' | ||
- | This is the output file of an embedded c compiler and contains code in an machine-readable representation. | + | This is the output file of an embedded c compiler and contains code in a machine-readable representation. |
- | An example of a c code and its hex-file is shown in <imgref pic02>. | + | An example of a c code and its hex file is shown in <imgref pic02>. |
- | In the hex-file the bytes (= 2 nibbles = 2 digits) are visibly grouped. | + | In the hex file the bytes (= 2 nibbles = 2 digits) are visibly grouped. |
<WRAP center> | <WRAP center> | ||
< | < | ||
</ | </ | ||
- | {{: | + | {{grundlagen_der_digitaltechnik: |
- | {{: | + | {{grundlagen_der_digitaltechnik: |
</ | </ | ||
The bytes in the first line are: | The bytes in the first line are: | ||
- | ^Byte0 | + | ^Byte0 |
- | | $10_{16}$ | $00_{16}$ | $00_{16}$ | $00_{16}$ | $19_{16}$ | $C0_{16}$ | ... | | + | | $10_{16}$ | $00_{16}$ | $00_{16}$ | $00_{16}$ | $19_{16}$ | $\rm C0_{16}$ | ... | |
- | But what the decimal value of this numerals? \\ This transfer is also possible the process shown in 2.1.1: | + | But what is the decimal value of these numerals? \\ This transfer is also possible |
$ Z_{10}=\sum_{i=-n}^m z_i \cdot B^i$, where $z_i$ is the digit on position i. | $ Z_{10}=\sum_{i=-n}^m z_i \cdot B^i$, where $z_i$ is the digit on position i. | ||
Zeile 151: | Zeile 151: | ||
As an example the Byte4 shall be written in decimal: | As an example the Byte4 shall be written in decimal: | ||
- | $ Z_{10}(Byte4)= Z_{10}(19_{16}) \\ =\sum_{i=0}^1 z_i \cdot 16^i = \\ 1 \cdot 16^1 + 9 \cdot 16^0 = 16 + 9 = 25_{10}$ | + | $ Z_{10}({\rm Byte}4)= Z_{10}(19_{16}) \\ =\sum_{i=0}^1 z_i \cdot 16^i = \\ 1 \cdot 16^1 + 9 \cdot 16^0 = 16 + 9 = 25_{10}$ |
=== Octal === | === Octal === | ||
Another - much less common - base is 8. This number system is called [[https:// | Another - much less common - base is 8. This number system is called [[https:// | ||
- | In this case only $0, | + | In this case, only $0, |
- | Similar to the number systems before, the following formula applies for the transfer in decimal: $ Z_{8}(X)= \sum_{i=-n}^m z_i \cdot 8^i | + | Similar to the number systems before, the following formula applies for the transfer in decimal: $ Z_{8}(X)= \sum_{i=-n}^m z_i \cdot 8^i$ |
==== 2.1.4 From decimal to other bases ==== | ==== 2.1.4 From decimal to other bases ==== | ||
+ | === Fast Approach: Try and Subtract === | ||
Up to this point chapter, we only have converted other $B$-based numerals to decimal. \\ | Up to this point chapter, we only have converted other $B$-based numerals to decimal. \\ | ||
- | Now we want to try to convert a decimal into hexadecimal, | + | Now we want to try to convert a decimal into hexadecimal, |
One way is to first convert to binary by "try and subtract" | One way is to first convert to binary by "try and subtract" | ||
Zeile 171: | Zeile 172: | ||
315_{10} &- 256_{10} &= 59_{10} \quad \quad 2^8\quad | 315_{10} &- 256_{10} &= 59_{10} \quad \quad 2^8\quad | ||
- | 59_{10} & | + | 59_{10} & |
- | 27_{10} & | + | 27_{10} & |
- | 11_{10} & | + | 11_{10} & |
- | 3_{10} & | + | 3_{10} & |
- | 1_{10} & | + | 1_{10} & |
\end{align*} | \end{align*} | ||
- | The number $307_{10}$ is similar to $2^8 + 2^5 + 2^4 + 2^3 + 2^1 + 2^0$. This is equal to $100111011_2$ or $0001\; | + | The number $315_{10}$ is similar to $2^8 + 2^5 + 2^4 + 2^3 + 2^1 + 2^0$. This is equal to $100111011_2$ or $0001\; |
+ | |||
+ | === Works every time: Repeated Division / Multiplication === | ||
But how can we convert a decimal numeral like $452.12_{10}$ e.g. to hexadecimal? | But how can we convert a decimal numeral like $452.12_{10}$ e.g. to hexadecimal? | ||
Zeile 187: | Zeile 190: | ||
<callout type=" | <callout type=" | ||
\\ | \\ | ||
- | The integer decimal $z$ can be converted the abovementioned method. | + | The integer decimal $z$ can be converted |
A different way is via repeatedly dividing by the base (here $B=16$) and using the remainder: | A different way is via repeatedly dividing by the base (here $B=16$) and using the remainder: | ||
- | \begin{align*} | + | {{drawio> |
- | + | ||
- | | + | |
- | \color{blue}{28} | + | |
- | \color{green}{1} | + | |
- | | + | |
- | ... &: 16 = ... | + | |
- | \end{align*} | + | |
- | For each of the shown steps, the integer of the division of the step before is used ($\color{blue}{28}$, $\color{green}{1}$). \\ | + | For each of the shown steps, the integer of the division of the step before is used ($28$, $1$). \\ |
The last steps (and any following) result in a remainder of $0$. \\ | The last steps (and any following) result in a remainder of $0$. \\ | ||
- | For hexadecimal | + | For hexadecimal |
- | ^each position in decimal | + | ^each position in decimal |
- | |each position in hexadecimal | 1 | C | 4 | | + | |each position in hexadecimal | $\color{red}{1}$ | $\color{green}{\rm |
- | The result is $1C4_{16}$ | + | The result is $\color{red}{1}\color{green}{\rm C}\color{blue}{4}_{16}$ |
</ | </ | ||
Zeile 213: | Zeile 209: | ||
The decimal places $0.12$ can be converted by repeatedly multiplying by the base (here $B=16$) and using the integer: | The decimal places $0.12$ can be converted by repeatedly multiplying by the base (here $B=16$) and using the integer: | ||
- | \begin{align*} | + | {{drawio> |
- | | + | For each of the shown steps, the decimal places of the multiplication of the step before are used ($.92 \rightarrow |
- | \color{blue }{0.92} &\cdot 16 = \boldsymbol{14}\color{green}{.72} \\ | + | The decimal place in the second last line is equal to the first one. Therefore also any further places will also be equal. That leads to repeating decimals. \\ |
- | \color{green}{0.72} &\cdot 16 = \boldsymbol{11}\color{brown}{.52} \\ | + | For hexadecimal numerals, we have to focus on the integer from top to below and convert the value into hexadecimal digits. |
- | \color{brown}{0.52} &\cdot 16 = \boldsymbol{ 8}\color{red | + | |
- | \color{red | + | |
- | \color{grey }{0.12} &\cdot 16 = \boldsymbol{ 1}\color{blue}{.92} \\ | + | |
- | \color{blue }{0.92} &\cdot 16 = \boldsymbol{14}\color{green}{.72} | + | |
- | ...& \quad = ... \\ | + | |
- | \end{align*} | + | |
- | For each of the shown steps, the decimal | + | ^each position in decimal |
- | Decimal place in the second last line is equal to the first one. Therefore also any further places will also be equal. That leads to repeating decimals. \\ | + | |each position in hexadecimal | $\color{blue }{1}$ | $\color{green}{\rm E}$ | $\color{brown}{\rm B}$ | $\color{red |
- | For hexadecimal numeral we have to focus on the integar from top to below and convert the value into hexadecimal digits. | + | |
- | ^each position in decimal | + | The result is $\rm 0.\overline{ |
- | |each position in hexadecimal | 1 | E | B | 8 | 5 | 1 | E | | | + | |
- | + | ||
- | The result is $0.\overline{1EB85}$ | + | |
</ | </ | ||
Results: | Results: | ||
- A decimal numeral has to be separated into integer decimal and decimal places. | - A decimal numeral has to be separated into integer decimal and decimal places. | ||
- | - By dividing / multiplying with the base the integer decimal / decimal places can be converted to another base. This works for all other bases like $2$ or $8$. | + | - By dividing/ |
- | - The results have to be converted (as least for base $B > 10$, like hexadecimal). | + | - The results have to be converted (at least for the base $\rm B > 10$, like hexadecimal). |
- | - A small decimal | + | - A small decimal |
- | Especially the last result has a mayor impact | + | Especially the last result has a major impact |
- | The internal logic is only based on binary, which also show this problem. However, the internal memory for a numeral is limited. | + | The internal logic is only based on binary, which also shows this problem. However, the internal memory for a numeral is limited. |
- | Even when stored in 32bit - it is not possible to exactly convert the $0.12_{10}$ to binary. In the following table the $n$-bit equivalent of $0.12_{10}$ in binary and hexadecimal system is shown. | + | Even when stored in 32bit - it is not possible to exactly convert the $0.12_{10}$ to binary. |
+ | In the following table, the $n$-bit equivalent of $0.12_{10}$ in the binary and hexadecimal system is shown. | ||
+ | Additionally, this value is also re-converted to a decimal numeral. | ||
- | ^number \\ of bits $n$ ^ number \\ system ^ numeral | + | ^number \\ of bits $n$ ^ number \\ system ^ numeral |
| $8 $ | binary | | $8 $ | binary | ||
- | | ::: | hex | $0.1F_{16} | + | | ::: | hex | $\rm 0.1F_{16} |
| ::: | equiv. dec | $0.12109375_{10} $ | | ::: | equiv. dec | $0.12109375_{10} $ | ||
| $16 $ | binary | | $16 $ | binary | ||
- | | ::: | hex | $0.1EB8_{16} | + | | ::: | hex | $\rm 0.1EB8_{16} |
| ::: | equiv. dec | $0.11999511718_{10} | | ::: | equiv. dec | $0.11999511718_{10} | ||
| $24 $ | binary | | $24 $ | binary | ||
- | | ::: | hex | $0.1EB851_{16} | + | | ::: | hex | $\rm 0.1EB851_{16} |
| ::: | equiv. dec | $0.11999994516..._{10}$ | | ::: | equiv. dec | $0.11999994516..._{10}$ | ||
| $32 $ | binary | | $32 $ | binary | ||
- | | ::: | hex | $0.1EB851EC_{16} $ | + | | ::: | hex | $\rm 0.1EB851EC_{16} $ |
| ::: | equiv. dec | $0.12000000011..._{10}$ | | ::: | equiv. dec | $0.12000000011..._{10}$ | ||
- | This might seem as a little issue. But there are a lot of areas, where exact decimal places are mandatory, like banking, or simulations. \\ | + | This might seem like a little issue. But there are a lot of areas, where exact decimal places are mandatory, like banking, or simulations. \\ |
+ | This situation even arises, when using [[https:// | ||
==== 2.1.5 Binary Coded Decimals ==== | ==== 2.1.5 Binary Coded Decimals ==== | ||
The first approach to this was the development of **B**inary **C**oded **D**ecimals. | The first approach to this was the development of **B**inary **C**oded **D**ecimals. | ||
- | The encoding algorithm from a decimal numeral into BCD is simple: " | + | The encoding algorithm from a decimal numeral into BCD is simple: |
- | This means the decimal numeral $391.21_{10}$ is encoded to $391.21_{BCD}$. Inside the processor each digit is handled as hexadecimal number: | + | This means the decimal numeral $391.21_{10}$ is encoded to $391.21_{\rm BCD}$. Inside the processor, each digit is handled as a hexadecimal number: |
- | $3\; | + | $3\; |
- | The main disadvantage is the ineffective storage management and more complex algorithms for addition, subtraction | + | The main disadvantage is the ineffective storage management and more complex algorithms for addition, subtraction, and so on. |
==== 2.1.6 Marking the base of a numeral ==== | ==== 2.1.6 Marking the base of a numeral ==== | ||
Up to here, the marking was done by the subscript. | Up to here, the marking was done by the subscript. | ||
- | Poxtfixes | + | Postfixes |
- | Alternative ways for the marking are the following: | + | Alternative ways for marking are the following: |
- | ^base ^subscripted | + | ^base ^subscripted |
- | |2 (binary) | + | |2 (binary) |
- | |10 (decimal) | + | |10 (decimal) |
- | |8 (octal) | + | |8 (octal) |
- | |8 (hexadecimal) | $27D_{16}$ | + | |8 (hexadecimal) | $\rm 27D_{16}$ |
Be aware, that in the code '' | Be aware, that in the code '' | ||
Zeile 291: | Zeile 279: | ||
When programming code for an embedded system, the system will always see 0's and 1's after compiling. | When programming code for an embedded system, the system will always see 0's and 1's after compiling. | ||
So, the microprocessor does not have to be considered. \\ | So, the microprocessor does not have to be considered. \\ | ||
- | In some cases on the other hand, the binary or hexadecimal numeral is much more convenient to read: | + | In some cases, on the other hand, the binary or hexadecimal numeral is much more convenient to read: |
- | + | ||
- | Here an example, where the binary numeral shows a smiley (e.g. for writing this on the screen), but the hexadecimal (or decimal) value does not give a clue what the output will be: | + | |
- | <sxh c; first-line: 1> | + | Here is an example, where the binary numeral shows a smiley (e.g. for writing this on the screen), but the hexadecimal (or decimal) value does not give a clue what the output will be: |
- | int disp1[8] = { | + | |
- | 0b00000000, | + | |
- | 0b01000100, | + | |
- | 0b01000100, | + | |
- | 0b00000000, | + | |
- | 0b10000001, | + | |
- | 0b01000010, | + | |
- | 0b00111100, | + | |
- | 0b00000000 | + | |
- | }; | + | |
- | int disp1[4] = { | + | |
- | 0x00, 0x44, 0x44, 0x00, | + | |
- | 0x81, 0x42, 0x3C, 0x00 | + | |
- | }; | + | |
- | </sxh> | + | {{drawio>shortcode.svg}} |
===== 2.2 basic arithmetic operations in binary and hexadecimal ===== | ===== 2.2 basic arithmetic operations in binary and hexadecimal ===== | ||
- | In this subchapter we will have a look onto the way how the arithmetic operations have to be executed manually in other bases. | + | In this subchapter, we will have a look at the way how the arithmetic operations have to be executed manually in other bases. |
- | For math it does not matter in which number system one calculates: a calculation like $2_{10} + 5_{10} = 7_{10}$, will be in binary $0010_2 + 0101_2 = 0111_2$. The values behind the numerals are still the same, they are only encoded differently. | + | For math, it does not matter in which number system one calculates: a calculation like $2_{10} + 5_{10} = 7_{10}$, will be in binary $0010_2 + 0101_2 = 0111_2$. The values behind the numerals are still the same, they are only encoded differently. |
- | The execution is similar to the well known experience of calculating in the decimal system. | + | The execution is similar to the well-known experience of calculating in the decimal system. |
Important for all of the operations: Never forget the [[https:// | Important for all of the operations: Never forget the [[https:// | ||
Zeile 326: | Zeile 298: | ||
\color{white}{+}192 \\ | \color{white}{+}192 \\ | ||
- | +378 \\ | + | +378 \\ |
- | +672 \\ | + | |
\hline | \hline | ||
- | 1\overset{\color{red}{2}}{1}\overset{\color{red}{1}}{4}\overset{}{2} | + | 1\overset{\color{red}{2}}{2}\overset{\color{red}{1}}{4}\overset{}{2} |
\end{align*} | \end{align*} | ||
- | The red numbers are the carry over of the calculation to the right. If a calculation exceeds the limits of the base, a new digit is added and the carry is taken into account in it. Similar | + | The red numbers are the carry-over of the calculation to the right. If a calculation exceeds the limits of the base, a new digit is added and the carry is taken into account in it. A similar |
==== 2.2.1 Addition ==== | ==== 2.2.1 Addition ==== | ||
=== In Binary === | === In Binary === | ||
- | In the following | + | The following examples shall show the concept |
\begin{align*} | \begin{align*} | ||
Zeile 367: | Zeile 339: | ||
The four simplest examples show the carry only for $d) \quad 1_2 + 1_2$. | The four simplest examples show the carry only for $d) \quad 1_2 + 1_2$. | ||
Now we can also connect the number system with the logic gates! The addends are called $A$ and $B$ (e.g. $A=1_2$, $B=1_2$), the sum is the numeral $CS_2$ (e.g. $C=1$, $S=0 \rightarrow CS_2=10_2$). \\ | Now we can also connect the number system with the logic gates! The addends are called $A$ and $B$ (e.g. $A=1_2$, $B=1_2$), the sum is the numeral $CS_2$ (e.g. $C=1$, $S=0 \rightarrow CS_2=10_2$). \\ | ||
- | When analysing | + | When analyzing |
- | For the carry $C$ we only get $1$, when both of the inputs are one: Here a AND gate have to be used. | + | For the carry $C$ we only get $1$, when both of the inputs are one: Here an AND gate has to be used. |
- | THe <imgref pic1> | + | The <imgref pic1> |
< | < | ||
< | < | ||
- | {{url> | + | {{url> |
</ | </ | ||
Zeile 385: | Zeile 357: | ||
\end{align*} | \end{align*} | ||
- | The rightmost, first step is easy, sind we already had this in the examples before. | + | The rightmost, first step is easy since we already had this in the examples before. |
The next step (marked in bold) is differing: not only do we have to consider the digits from $A$ and $B$, but also the carry from the calculation before. | The next step (marked in bold) is differing: not only do we have to consider the digits from $A$ and $B$, but also the carry from the calculation before. | ||
The carry from before has to be added for all the next steps similarly. | The carry from before has to be added for all the next steps similarly. | ||
- | For the calculation by hand, we did four individual additions from right to the left. The processor has to do the same. But first we have to expand the half adder. For a complete addition step we need a logic with the inputs $A$, $B$ and and carry from the step before $Cin$. The output will be still $S$ and $C$. | + | For the calculation by hand, we did four individual additions from the right to the left. The processor has to do the same. |
+ | But first, we have to expand the half-adder. For a complete addition step, we need a logic with the inputs $A$, and $B$ and carry from the step before $Cin$. The output will be still $S$ and $C$. | ||
< | < | ||
< | < | ||
- | {{url> | + | {{url> |
</ | </ | ||
Zeile 399: | Zeile 372: | ||
- first add one bit from $A$ to one bit from $B$ (half adder). The result is in $S'$ and $C' | - first add one bit from $A$ to one bit from $B$ (half adder). The result is in $S'$ and $C' | ||
- Then, take the result $S'$ and add it with $Cin$ (second half adder). The result is in $S$ and $C'' | - Then, take the result $S'$ and add it with $Cin$ (second half adder). The result is in $S$ and $C'' | ||
- | - For the carry output we need to consider both carries $C'$ and $C'' | + | - For the carry output we need to consider both carries $C'$ and $C'' |
- | This full adder can now be stacked together | + | This full adder can now be stacked together to add multiple bits $A0, A1, ...$ to $B0, B1; ...$. |
< | < | ||
- | < | + | < |
- | {{url> | + | {{url> |
</ | </ | ||
Zeile 414: | Zeile 387: | ||
\begin{align*} | \begin{align*} | ||
\begin{array}{lll} | \begin{array}{lll} | ||
- | { | + | {a) \\ |
- | a) \\ | + | \color{white} |
- | \color{white}{+}5_{16} \\ | + | |
- | +3_{16}\\ | + | |
\, | \, | ||
- | }&{ | + | }&{ b) \\ |
- | b) \\ | + | \color{white} |
- | \color{white}{+}7_{16} \\ | + | |
- | +3_{16}\\ | + | \, |
- | \, | + | }&{ c) \\ |
- | }&{ | + | \color{white} |
- | c) \\ | + | |
- | \color{white}{+}\, | + | \, |
- | +D_{16}\\ | + | }&{ d) \\ |
- | \, | + | \color{white} |
- | }&{ | + | |
- | d) \\ | + | |
- | \color{white}{+}E_{16} \\ | + | |
- | +A_{16}\\ | + | |
\; | \; | ||
} | } | ||
Zeile 439: | Zeile 408: | ||
a) This one is simple: looks like a decimal formula.. \\ | a) This one is simple: looks like a decimal formula.. \\ | ||
- | b) Here, the summands look like decimal numerals, but the result $7_{10} + 3_{10} = 10_{10}$ is still within the range ob the base. The correct symbol would be $10_{10} = A_{16}$ \\ | + | b) Here, the summands look like decimal numerals, but the result $7_{10} + 3_{10} = 10_{10}$ is still within the range of the base. The correct symbol would be $10_{10} = \rm A_{16}$ \\ |
- | c) Now, the summands are a 'bit more hexadecimal' | + | c) Now, the summands are a "bit more hexadecimally" |
- | d) Also for this calculation the described way is beneficial: $E_{16} + A_{16} = 14_{10} + 10_{10} = 24_{10}$. The result is larger than the base, and therefore the value has to be separated in more digits: $24_{10} = 16_{10} + 8_{10} = 10_{16} + 8_{16} = 18_{10}$ \\ | + | d) Also for this calculation the described way is beneficial: $\rm E_{16} + A_{16} = 14_{10} + 10_{10} = 24_{10}$. The result is larger than the base, and therefore the value has to be separated in more digits: $24_{10} = 16_{10} + 8_{10} = 10_{16} + 8_{16} = 18_{10}$ \\ |
- | For a hexadecimal value with more digits the carry of the calculation before has to be added - otherwise every step remains the same. | + | For a hexadecimal value with more digits, the carry of the calculation before has to be added - otherwise, every step remains the same. |
==== 2.2.2 Subtraction ==== | ==== 2.2.2 Subtraction ==== | ||
Zeile 449: | Zeile 418: | ||
=== In Binary === | === In Binary === | ||
- | In the following | + | The following examples shall show the concept |
\begin{align*} | \begin{align*} | ||
\begin{array}{lll} | \begin{array}{lll} | ||
- | { | + | { a) \\ |
- | a) \\ | + | \color{white} |
- | \color{white}{-}0_2 \\ | + | |
- | -0_2\\ | + | |
\, | \, | ||
- | }&{ | + | }&{ b) \\ |
- | b) \\ | + | \color{white} |
- | \color{white}{-}1_2 \\ | + | |
- | -1_2\\ | + | |
\, | \, | ||
- | }&{ | + | }&{ c) \\ |
- | c) \\ | + | \color{white} |
- | \color{white}{-}1_2 \\ | + | |
- | -0_2\\ | + | |
\, | \, | ||
- | }&{ | + | }&{ d) \\ |
- | d) \\ | + | \color{white} |
- | \color{white}{-}0_2 \\ | + | |
- | -1_2\\ | + | |
\; | \; | ||
} | } | ||
Zeile 479: | Zeile 444: | ||
The calculation for $d)$ shows the carry, which here has to borrow a bit from the next upper digit. This is similar to the calculation: | The calculation for $d)$ shows the carry, which here has to borrow a bit from the next upper digit. This is similar to the calculation: | ||
\begin{align*} | \begin{align*} | ||
- | \color{white}{-}42_{10} \\ | + | \color{white} |
- | -23_{10}\\ | + | - 23_{10}\\ |
- | \; | + | \; |
\end{align*} | \end{align*} | ||
Zeile 487: | Zeile 452: | ||
\begin{align*} | \begin{align*} | ||
- | \color{white}{-}\, | + | \color{white} |
- | -01\boldsymbol{1}1_2\\ | + | - |
\, | \, | ||
\end{align*} | \end{align*} | ||
Zeile 495: | Zeile 460: | ||
\begin{align*} | \begin{align*} | ||
- | \color{white}{-}\, | + | \color{white} |
- | -\boldsymbol{1}_2\\ | + | |
\overline{ \overset{\color{red}{1}} {\color{white}{0}} \overset{\color{red}{\boldsymbol{1}}} {\boldsymbol{1}}_2 } | \overline{ \overset{\color{red}{1}} {\color{white}{0}} \overset{\color{red}{\boldsymbol{1}}} {\boldsymbol{1}}_2 } | ||
\end{align*} | \end{align*} | ||
- | The calculation has to be executed as follows: $\boldsymbol{1}_2 - (\boldsymbol{1}_2 + \color{red}{\small{\boldsymbol{1}}} ) = {\boldsymbol{1}}_2 $. Additionally, | + | The calculation has to be executed as follows: $\boldsymbol{1}_2 - (\boldsymbol{1}_2 + \color{red}{\small{\boldsymbol{1}}} ) = {\boldsymbol{1}}_2 $. |
+ | Additionally, | ||
=== In Hexadecimal === | === In Hexadecimal === | ||
The calculation in hexadecimal is conceptually again the same. Some examples, which we will discuss below: | The calculation in hexadecimal is conceptually again the same. Some examples, which we will discuss below: | ||
- | |||
\begin{align*} | \begin{align*} | ||
+ | \rm | ||
\begin{array}{lll} | \begin{array}{lll} | ||
- | { | + | { a) \\ |
- | a) \\ | + | |
- | \color{white}{-}15_{16} \\ | + | |
- | -\color{white}{1}3_{16}\\ | + | |
\, | \, | ||
- | }&{ | + | }&{ b) \\ |
- | b) \\ | + | |
- | \color{white}{-}23_{16} \\ | + | |
- | -\color{white}{B}6_{16}\\ | + | \overline{\color{white}{-}\overset{\color{red}{1}}{1} |
- | \overline{\color{white}{-}\overset{\color{red}{1}}{1}D_{16}} | + | }&{ c) \\ |
- | }&{ | + | |
- | c) \\ | + | -1{\rm A}_{16}\\ |
- | \color{white}{-}3F_{16} \\ | + | |
- | -1A_{16}\\ | + | |
\color{white}{-}\overline{\color{white}{B}\overset{}{25_{16}}} | \color{white}{-}\overline{\color{white}{B}\overset{}{25_{16}}} | ||
- | }&{ | + | }&{ d) \\ |
- | d) \\ | + | |
- | \color{white}{+}\, | + | - 1{\rm A}_{16}\\ |
- | +1A_{16}\\ | + | \color{white}{+}\overline{\overset{\color{red}{1}}{1}\overset{}{{\rm E}_{16}}} |
- | \color{white}{+}\overline{\overset{\color{red}{1}}{1}\overset{}{B_{16}}} | + | |
} | } | ||
\end{array} | \end{array} | ||
Zeile 534: | Zeile 496: | ||
a) This one is (again) simple: looks (again) like a decimal formula.. \\ | a) This one is (again) simple: looks (again) like a decimal formula.. \\ | ||
- | b) Here, the both hexadecimal numerals look like decimal numerals, but the result $3_{10} - 6_{10} = 7_{10} + C$ lead to an underflow, where we have to take a carry of $C=-10_{10}$ for a decimal calculation. In hexadecimal the carry differs. A better way to solve such a subtraction in hexadecimal is to add the carry before: $3_{16} - 6_{16} + \color{red}{10_{16}}$. Then the calculation can be converted to decimal: | + | b) Here, both hexadecimal numerals look like decimal numerals, but the result $3_{10} - 6_{10} = 7_{10} + C$ lead to an underflow, where we have to take a carry of $C=-10_{10}$ for a decimal calculation. In hexadecimal, the carry differs. A better way to solve such a subtraction in hexadecimal is to add the carry before: $3_{16} - 6_{16} + \color{red}{10_{16}}$. Then the calculation can be converted to decimal: |
- | c) For this formula the idea from b) helps, too: for each digit by digit subtraction, | + | c) For this formula the idea from b) helps, too: for each digit-by-digit subtraction, |
- | d) We can do the same thing here, for $8_{16}-A_{16}$: | + | d) We can do the same thing here, for $8_{16}-A_{16}$: |
<WRAP column 100%> <panel type=" | <WRAP column 100%> <panel type=" | ||
- | The subtraction within the processor is done a bit differently, | + | The subtraction within the processor is done a bit differently, |
</ | </ | ||
Zeile 547: | Zeile 509: | ||
The multiplication table for binary is simple: | The multiplication table for binary is simple: | ||
- | |||
\begin{align*} | \begin{align*} | ||
Zeile 556: | Zeile 517: | ||
\end{align*} | \end{align*} | ||
- | Multiplication of longer binary numerals | + | Multiplication of longer binary numerals |
\begin{align*} | \begin{align*} | ||
\begin{array}{lll} | \begin{array}{lll} | ||
- | 1011_2 \cdot 1101_2 | + | 1011_2 \cdot\, \color{blue}{1}\color{brown}{1}0\color{violet}{1}_2 |
\hline | \hline | ||
- | | + | \;\ \ \ \ \ \ \color{blue |
- | +\color{white}{\ \ \ \ \ \ }1011 \\ | + | +\;\ \ \ \ \ |
- | +\color{white}{\ \ \ \ \ \ \ \ }1011 \\ | + | +\;\ \ \ \ \ \ \ \ \ \color{violet}{1011} \\ |
\hline | \hline | ||
- | \ \ \ \ \ \ \overset{\color{red}{1}}{1}\overset{\color{red}{1}}{1}\overset{\color{red}{1}}{1}\overset{\color{red}{1}}{1}\overset{\color{red}{1}}{1}\overset{\color{red}{1}}{1} | + | \ \ \ \ \ \overset{\color{red}{1}}{1}\overset{\color{red}{1}}{0}\overset{\color{red}{1}}{0}\overset{\color{red}{1}}{0}\overset{}{1}\overset{}{1}\overset{}{1}\overset{}{1}_2 |
\end{array} | \end{array} | ||
\end{align*} | \end{align*} | ||
+ | The division is also relatively simple: | ||
+ | \begin{align*} | ||
+ | 0_2 : 1_2 = 0_2 \\ | ||
+ | 1_2 : 1_2 = 1_2 | ||
+ | \end{align*} | ||
- | ======= further links ======= | + | With longer numerals it looks like the following: |
+ | <WRAP center> | ||
+ | < | ||
+ | </ | ||
+ | {{drawio> | ||
+ | </ | ||
+ | |||
+ | ======= related Links ======= | ||
* [[https:// | * [[https:// | ||
+ | * The [[https:// | ||
+ | * A short video on the " | ||
+ | |||
+ | ==== Exercises ==== | ||
+ | |||
+ | <panel type=" | ||
+ | |||
+ | fill out the tables and remember the numerals! | ||
+ | |||
+ | <WRAP center> | ||
+ | < | ||
+ | {{drawio> | ||
+ | </ | ||
+ | |||
+ | <WRAP center> | ||
+ | < | ||
+ | {{drawio> | ||
+ | </ | ||
+ | |||
+ | <WRAP center> | ||
+ | < | ||
+ | {{drawio> | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | <panel type=" | ||
+ | |||
+ | Convert the following hexadecimal numerals to the binary system. \\ | ||
+ | Tip: Convert nibble by nibble to binary. | ||
+ | |||
+ | - $\rm 4321H$ | ||
+ | - $\rm 0xCAFE$ | ||
+ | - $ \$ $$\rm 4be79 $ | ||
+ | - $ \$ $$\rm 398B4C $ | ||
+ | - $\rm 0x3a4f.4ecd $ | ||
+ | - $\$\rm 1.3DAF $ | ||
+ | |||
+ | </ | ||
+ | |||
+ | <panel type=" | ||
+ | |||
+ | Convert the following numerals from dual to hexadecimal and decimal numerals. \\ | ||
+ | Tip: Use the numerals from Exercise 2.3.1. | ||
+ | |||
+ | - $\rm 0b10110110$ | ||
+ | - $\%10100101$ | ||
+ | - $\rm 1111\, 0111\, 0001\, 0011B$ | ||
+ | - $\rm 0110111.0101b$ | ||
+ | - $\rm 0.10101b$ | ||
+ | - $\rm 0.1001\, 1001\, ... b$ | ||
+ | |||
+ | </ | ||
+ | |||
+ | <panel type=" | ||
+ | |||
+ | Convert the following numerals from decimal to hexadecimal and dual numerals. \\ | ||
+ | Tip: Use the numerals from Exercise 2.3.1. | ||
+ | |||
+ | - $123_{10}$ | ||
+ | - $637_{10}$ | ||
+ | - $1777_{10}$ | ||
+ | - $8999_{10}$ | ||
+ | - $41.4_{10}$ | ||
+ | - $0.9_{10}$ | ||
+ | - $11.3_{10}$ | ||
+ | |||
+ | </ | ||
+ | |||
+ | <panel type=" | ||
+ | |||
+ | Execute the following operations manually with the given dual numerals. \\ | ||
+ | |||
+ | - $\rm 01110111B + 11010101B$ | ||
+ | - $\rm 01011001B + 10011111B$ | ||
+ | - $\rm 01100111B - 01001100B$ | ||
+ | - $\rm 10101110B - 10000111B$ | ||
+ | - $\rm 0011.111B - 0011.100B$ | ||
+ | - $\rm 001111.011B + 1.010011B$ | ||
+ | - $\rm 01101.100B - 01000.111B$ | ||
+ | - $\rm 100101.11B - 110001.10B$ | ||
+ | |||
+ | </ | ||
+ | |||
+ | <panel type=" | ||
+ | |||
+ | Execute the following operations manually with the given dual numerals. \\ | ||
+ | |||
+ | - $\rm 1011B \cdot 0101B$ | ||
+ | - $\rm 01100101B \cdot 110B$ | ||
+ | - $\rm 10101111B : 101B$ | ||
+ | - $\rm 11110000B : 1000B$ | ||
+ | </ | ||
+ | |||
+ | <panel type=" | ||
+ | |||
+ | Execute the following operations manually with the given dual numerals. \\ | ||
+ | |||
+ | - $\rm 1334H + 07ABH$ | ||
+ | - $\rm 0DC43H - 0BD19H$ | ||
+ | - $\rm 1F23H + 90E8H$ | ||
+ | - $\rm 98C5H - 84CAH$ | ||
+ | - $\rm 234AH + 7EE6H$ | ||
+ | - $\rm 0F10CH - 0ED43H$ | ||
+ | |||
+ | </ | ||
+ | |||
+ | <panel type=" | ||
+ | |||
+ | - Write down the decimal numeral for the first 4 hexadecimal places | ||
+ | - Which possible value range can be covered with an 8-bit variable? | ||
+ | |||
+ | </ | ||
+ | |||
+ | <panel type=" | ||
+ | |||
+ | In the simulation in <imgref pic70> the two's complement of a 4bit value is shown. \\ | ||
+ | On initialization, | ||
+ | * The value for variable $A$ is $1010_2 = 10_{10}$. The decimal value '' | ||
+ | * The value for variable $B$ is $1110_2 = 14_{10}$. The decimal value '' | ||
+ | * The addition $A+B$ leads to $S = 1000_2 = 8_{10}$. The decimal value '' | ||
+ | * There is another value in the second display from the right. This is called $B'$, and it is equal to $0010_2 = 2_{10}$. | ||
+ | |||
+ | < | ||
+ | < | ||
+ | {{url> | ||
+ | </ | ||
+ | |||
+ | - Why is '' | ||
+ | - Try to analyze how '' | ||
+ | - Have a look at the wiki page of the {{wp> | ||
+ | - Imagine that you have to rescue data from an old storage device. The interesting bits are given by the boxed area in <imgref picex01> | ||
+ | - What are these values in an unsigned integer? | ||
+ | - What are these values in signed integers based on the Two's complement? | ||
+ | |||
+ | |||
+ | <WRAP center> | ||
+ | < | ||
+ | </ | ||
+ | {{drawio> | ||
+ | <fs 70%> | ||
+ | From: [[https:// | ||
+ | </fs> | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | |||