http://www.categories.acsl.org/wiki/api.php?action=feedcontributions&user=Marc&feedformat=atomACSL Category Descriptions - User contributions [en]2020-09-22T08:07:42ZUser contributionsMediaWiki 1.26.3http://www.categories.acsl.org/wiki/index.php?title=Computer_Number_Systems&diff=837Computer Number Systems2020-09-02T23:38:40Z<p>Marc: /* Converting to Decimal */</p>
<hr />
<div>All digital computers, from supercomputers to your smartphone, are electronic devices and ultimately can do one thing: detect whether an electrical signal is on or off. That basic information, called a ''bit'' ('''bi'''nary digi'''t'''), has two values: a 1 (or ''true'') when the signal is on, and a 0 (of ''false'') when the signal is off. <br />
<br />
Larger values can be stored by a group of bits. For example, there are 4 different values stored by 2 bits (00, 01, 10, and 11), 8 values for 3 bits (000, 001, 010, 011, 100, 101, 110, and 111), 16 values for 4 bits (0000, 0001, ..., 1111), and so on. However, large numbers, using 0s and 1s only, are quite unwieldy for humans. For example, a computer would need 19 bits to store the numbers up to 500,000! We use of different number systems to work with large binary strings that represent numbers within computers.<br />
<br />
== Different Number Systems ==<br />
<br />
A ''number system'' is the way we name and represent numbers. The number system we use in our daily life is known as '''decimal number system''' or '''base 10''' because it is based on 10 different digits: 0, 1, 2, ..., 8, and 9. The base of number is written as subscript to a number. For example, the decimal number "twelve thousand three hundred and forty-five" is written as $12345_{10}$. Without a base or explicit context, it's assumed that a number is in base 10. <br />
<br />
In computer science, apart from the decimal system, three additional number systems are commonly used: '''binary''' (base-2), '''octal''' (base-8), and '''hexadecimal''' or just '''hex''' (base-16). Binary numbers are important because that is how numbers are stored in the computer. Octal and hexadecimal are used to represent binary numbers in a user-friendly way. Each octal symbol represents 3 binary bits and each hex digit represents 4 binary bits. For example, the decimal number 53201, stored as in the computer as the binary number 10000001111000101100, is represented by the octal number 2017054, and the hex number 81E2C. The table below compares the number systems: <br />
:{| class="wikitable"<br />
!Number System<br />
!Base<br />
!Digits Used<br />
!Examples<br />
|-<br />
|Binary<br />
|2<br />
|0,1<br />
|$10110_{2}$, $10110011_{2}$<br />
|-<br />
|Octal<br />
|8<br />
|0,1,2,3,4,5,6,7<br />
|$75021_{8}$, $231_{8}$, $60012_{8}$<br />
|-<br />
|Decimal<br />
|10<br />
|0,1,2,3,4,5,6,7,8,9<br />
|$97425_{10}$ or simply 97425<br />
|-<br />
|Hexadecimal<br />
|16<br />
|0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F<br />
|$54A2DD0F_{16}$<br />
|}<br />
<br />
<br />
In general, $N$ bits have $2^N$ different values. The computers aboard the Apollo spacecraft had 8-bit words of memory. Each word of memory could store 256 different values, and the contents were displayed using 2 hex characters.<br />
<br />
The following table shows the first 20 numbers in decimal, binary, octal, and hexadecimal:<br />
:{| class="wikitable" style="text-align:center"<br />
!Decimal<br />
!Binary<br />
!Octal<br />
!Hexadecimal<br />
|-<br />
|0<br />
|0<br />
|0<br />
|0 <br />
|-<br />
|1<br />
|1<br />
|1<br />
|1 <br />
|-<br />
|2<br />
|10<br />
|2<br />
|2 <br />
|-<br />
|3<br />
|11<br />
|3<br />
|3 <br />
|-<br />
|4<br />
|100<br />
|4<br />
|4 <br />
|-<br />
|5<br />
|101<br />
|5<br />
|5 <br />
|-<br />
|6<br />
|110<br />
|6<br />
|6 <br />
|-<br />
|7<br />
|111<br />
|7<br />
|7 <br />
|-<br />
|8<br />
|1000<br />
|10<br />
|8<br />
|-<br />
|9<br />
|1001<br />
|11<br />
|9<br />
|-<br />
|10<br />
|1010<br />
|12<br />
|A<br />
|-<br />
|11<br />
|1011<br />
|13<br />
|B<br />
|-<br />
|12<br />
|1100<br />
|14<br />
|C <br />
|-<br />
|13<br />
|1101<br />
|15<br />
|D <br />
|-<br />
|14<br />
|1110<br />
|16<br />
|E <br />
|-<br />
|15<br />
|1111<br />
|17<br />
|F <br />
|-<br />
|16<br />
|10000<br />
|20<br />
|10 <br />
|-<br />
|17<br />
|10001<br />
|21<br />
|11<br />
|-<br />
|18<br />
|10010<br />
|22<br />
|12<br />
|-<br />
|19<br />
|10011<br />
|23<br />
|13<br />
|-<br />
|20<br />
|10100<br />
|24<br />
|14<br />
|}<br />
<br />
== Converting to Decimal ==<br />
<br />
As we learned in the first or second grade, the decimal value of a decimal number is simply sum of each digit multiplied by its place value:<br />
<br />
:$12345 = 1×10^{4}+ 2×10^{3}+ 3×10^{2}+ 4×10^{1}+ 5×10^{0} = 10000 + 2000+ 300 +40+5 = 12345$<br />
<br />
:$3079 = 3×10^{3}+ 0×10^{2}+ 7×10^{1}+ 9×10^{0} = 3000 + 70 + 9 = 3079$<br />
<br />
Analogously, the decimal value of a number in an arbitrary base is the sum of each digit multiplied by its place value. Here are some examples of converting from one base into base 10:<br />
<br />
:$1101_{2} = 1 × 2^3 + 1 × 2^2 + 0 × 2^1 + 1 × 2^0 = 8 + 4 + 0 + 1 = 13_{10}$ <br />
<br />
:$175_{8} = 1 × 8^2 + 7 × 8^1 + 5 × 8^0 = 1 × 64 + 7 × 8 + 5 × 1 = 64 + 56 + 5 = 125_{10}$<br />
<br />
:$A5E_{16} = 10 × 16^2 + 5 × 16^1 + 14 × 16^0 = 10 × 256 + 5 × 16 + 14 × 1 = 2560 + 80 + 14 = 2654_{10}$<br />
<br />
== Converting from Decimal ==<br />
<br />
The algorithm to convert a number from an arbitrary base requires finding how many times successive powers of the base go into the number, starting with the largest power of the base less than the starting number. For example, converting 3206 from base 10 to octal proceeds as follows:<br />
<br />
1) The largest power of 8 that is less than or equal to 3206 is 512 ($8^3$). Divide 3206 by 512:<br />
:$3206 = 6*8^3 + 234$ <br />
<br />
2) The next power of 8 is 64 ($8^2$). Divide 234 by 64:<br />
:$234 = 3 *8^2 + 42$<br />
<br />
3) The next smaller power of 8 is 8 ($8^1$). Divide 42 by 8:<br />
:$42 = 5*8^1 + 2$<br />
<br />
4) Finally, the next smaller power of 8 is 1 ($8^0$). Divide 2 by 1:<br />
:$2 = 2 * 8^0$<br />
<br />
The answer is $6352_8$.<br />
<br />
== Converting between Binary, Octal, and Hexadecimal == <br />
<br />
Converting from octal to binary is simple: replace each octal digit by its corresponding 3 binary bits. For example:<br />
<br />
:$375_{8} = \ \ 011\ \ \ 111\ \ \ 101_{2} = 11111101_2$<br />
<br />
Converting from hex to binary is also simple: replace each hex digit by its corresponding 4 binary bits. For example:<br />
<br />
:$FD_{16} = \ \ 1111\ \ \ 1101_{2} = 11111101_2$<br />
<br />
Converting from binary to either octal or hex is pretty simple as well: group the bits by 3s or 4s (starting at the right), and convert each group:<br />
<br />
:$10000001111000101100 = 10\ 000\ 001\ 111\ 000\ 101 \ 100 = 2017054_8$<br />
<br />
:$10000001111000101100 = 1000\ 0001\ 1110\ 0010\ 1100 = 81E2C_{16}$<br />
<br />
Converting between base 8 and 16 is easy by expressing the number in base 2 (easy to do!) and then converting that number from base 2 (another easy operation)! This is shown below in Sample Problem #1.<br />
<br />
== Using Hexadecimal Numbers to Represent Colors ==<br />
<br />
Computers use hexadecimal numbers to represent various colors in computer graphics because all computer screens use combinations of red, green, and blue light or RGB to represent thousands of different colors. Two digits are used for each so the hexadecimal number “#FF0000” represents the color red, “#00FF00” represents green, and “#0000FF” represents blue. The color black is “#000000” and white is “#FFFFFF”. <br />
<br />
The hash tag or number sign is used to denote a hexadecimal number. $FF_{16} = F (15) × 16 + F (15) × 1 = 240 + 15 = 255_{10}$ so there are 0 to 255 or 256 different shades of each color or $256^{3} = 16,777,216$ different colors. <br />
<br />
The following web site has nearly every color name, along with its hex code and decimal values: <br />
<br />
:https://www.rapidtables.com/web/color/RGB_Color.html <br />
<br />
For example “salmon” is “#FA8072” which represents the decimal numbers 250 (hex FA), 128 (hex 80), and 114 (hex 72).<br />
<br />
== Resources ==<br />
<br />
[https://ryanstutorials.net/ Ryan's Tutorials] covers this topic beautifully. Rather than trying to duplicate that work, we'll point you to the different sections:<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/ 1. Number Systems] - An introduction to what numbers systems are all about, with emphasis on decimal, binary, octal, and hexadecimal. ACSL will typically identify the base of a number using a subscript. For example, $123_8$ is an octal number, whereas $123_{16}$ is a hexadecimal number. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-conversions.php 2. Binary Conversions] - This section shows how to convert between binary, decimal, hexadecimal and octal numbers. In the [https://ryanstutorials.net/binary-tutorial/binary-conversions.php#activities ''Activities''] section, you can practice converting numbers. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-arithmetic.php 3. Binary Arithmetic] - Describes how to perform various arithmetic operations (addition, subtraction, multiplication, and division) with binary numbers. ACSL problems will also cover basic arithmetic in other bases, such as adding and subtracting together 2 hexadecimal numbers. ACSL problems will not cover division in other bases.<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php 4. Negative Numbers] - ACSL problems will not cover how negative numbers are represented in binary. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-floating-point.php 5. Binary Fractions and Floating Point] - The first part of this section is relevant to ACSL: fractions in other bases. ACSL will not cover floating point numbers in other basis. So, focus on the section [https://ryanstutorials.net/binary-tutorial/binary-floating-point.php#convertfraction Converting to a Binary Fraction], but keep in mind that ACSL problems may also cover octal and hexadecimal fractions.<br />
<br />
The [https://coolconversion.com/math/binary-octal-hexa-decimal/ CoolConversion.com online calculator] is another online app for practicing conversion from/to decimal, hexadecimal, octal and binary; this tool shows the steps that one goes through in the conversion. <br />
<br />
The AskNumbers.com site has a nice description of the conversions between [https://www.asknumbers.com/hex-to-octal.aspx binary, octal, decimal and hexadecimal] numbers. <br />
<br />
== Format of ACSL Problems ==<br />
<br />
The problems in this category will focus on converting between binary, octal, decimal, and hexadecimal, basic arithmetic of numbers in those bases, and, occasionally, fractions in those bases.<br />
<br />
To be successful in this category, you must know the following facts cold:<br />
<br />
# The binary value of each octal digit 0, 1, ..., 7<br />
# The binary value of each hex digit 0, 1, ..., 9, A, B, C, D, E, F<br />
# The decimal value of each hex digit 0, 1, ..., F <br />
# Powers of 2, up to 4096<br />
# Powers of 8, up to 4096<br />
# Powers of 16, up to 65,536<br />
<br />
== Sample Problems ==<br />
<br />
=== Sample Problem 1 ===<br />
<br />
Solve for $x$ where $x_{16}=3676_8$.<br />
<br />
'''Solution:''' One method of solution is to convert $3676_8$ into base 10, and then convert that number into base 16 to yield the value of $x$.<br />
<br />
An easier solution, less prone to arithmetic mistakes, is to convert from octal (base 8) to hexadecimal (base 16) through the binary (base 2) representation of the number:<br />
<br />
$$\begin{align}<br />
3676_8 &= 011 ~ 110 ~ 111 ~ 110_2 & \text{convert each octal digit into base 2}\hfill\cr <br />
&= 0111 ~ 1011 ~ 1110_2 & \text{group by 4 bits, from right-to-left}\hfill\cr<br />
&= 7 ~ \text{B} ~ \text{E}_{16} & \text{convert each group of 4 bits into a hex digit}\cr<br />
\end{align}$$<br />
<br />
=== Sample Problem 2 ===<br />
<br />
Solve for $x$ in the following hexadecimal equation: $ x= \text{F5AD}_{16} - \text{69EB}_{16}$<br />
<br />
'''Solution:''' One could convert the hex numbers into base 10, perform the subtraction, and then convert the answer back to base 16. However, <br />
working directly in base 16 isn't too hard. <br />
As in conventional decimal arithmetic, one works from right-to-left, from the least significant digits to the most. <br />
<br />
:The rightmost digit becomes 2, because D-B=2. <br />
:The next column is A-E. We need to ''borrow'' a one from the 5 column, and 1A-E=C <br />
:In the next column, 4-9=B, again, borrowing a 1 from the next column.<br />
:Finally, the leftmost column, E-6=8<br />
<br />
Combining these results of each column, we get a final answer of $8BC2_{16}$.<br />
<br />
=== Sample Problem 3 ===<br />
<br />
How many numbers from 100 to 200 in base 10 consist of distinct<br />
ascending digits and also have distinct ascending hex digits when<br />
converted to base 16? <br />
<br />
'''Solution:''' There are 13 numbers that have ascending digits in both bases from 100<br />
to 200. They are (in base 10):<br />
123 (7B), 124, 125, 126, 127 (7F), 137 (89), 138, 139 (8B), 156 (9C), 157, 158, 159 (9F), 189 (BD)<br />
<br />
== Video Resources ==<br />
<br />
There are many YouTube videos about computer number systems. Here are a handful that cover the topic nicely, without too many ads:<br />
<br />
{|<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/aW3qCcH6Dao</youtube><br />
| [https://youtu.be/aW3qCcH6Dao ''Number Systems - Converting Decimal, Binary and Hexadecimal'' ('''Joe James''')]<br />
<br />
An introduction to number systems, and how to convert between decimal, binary and hexadecimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/m1JtWKuTLR0</youtube><br />
| [https://youtu.be/m1JtWKuTLR0 ''Lesson 2.3 : Hexadecimal Tutorial'' ('''Carl Herold''')]<br />
<br />
The video focuses on hexadecimal numbers: their relationship to binary numbers and how to convert to decimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/WR67syBDzew</youtube><br />
| [https://youtu.be/WR67syBDzew ''Hexes and the Magic of Base 16 - Vaidehi Joshi - May 2017' ('''DonutJS''')]<br />
<br />
A fun introduction to hexadecimal numbers, focusing a bit <br />
on using hex numbers for specifying RGB colors. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jvx-NrILgpE</youtube><br />
| [https://youtu.be/jvx-NrILgpE ''Collins Lab: Binary & Hex'' ('''Adafruit Industries''')]<br />
<br />
A professionally produced video that explains the number systems, how and why binary numbers are fundamental to computer science, and why hexadecimal is important to computer programmers. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Computer_Number_Systems&diff=836Computer Number Systems2020-09-02T23:33:20Z<p>Marc: /* Converting to Decimal */</p>
<hr />
<div>All digital computers, from supercomputers to your smartphone, are electronic devices and ultimately can do one thing: detect whether an electrical signal is on or off. That basic information, called a ''bit'' ('''bi'''nary digi'''t'''), has two values: a 1 (or ''true'') when the signal is on, and a 0 (of ''false'') when the signal is off. <br />
<br />
Larger values can be stored by a group of bits. For example, there are 4 different values stored by 2 bits (00, 01, 10, and 11), 8 values for 3 bits (000, 001, 010, 011, 100, 101, 110, and 111), 16 values for 4 bits (0000, 0001, ..., 1111), and so on. However, large numbers, using 0s and 1s only, are quite unwieldy for humans. For example, a computer would need 19 bits to store the numbers up to 500,000! We use of different number systems to work with large binary strings that represent numbers within computers.<br />
<br />
== Different Number Systems ==<br />
<br />
A ''number system'' is the way we name and represent numbers. The number system we use in our daily life is known as '''decimal number system''' or '''base 10''' because it is based on 10 different digits: 0, 1, 2, ..., 8, and 9. The base of number is written as subscript to a number. For example, the decimal number "twelve thousand three hundred and forty-five" is written as $12345_{10}$. Without a base or explicit context, it's assumed that a number is in base 10. <br />
<br />
In computer science, apart from the decimal system, three additional number systems are commonly used: '''binary''' (base-2), '''octal''' (base-8), and '''hexadecimal''' or just '''hex''' (base-16). Binary numbers are important because that is how numbers are stored in the computer. Octal and hexadecimal are used to represent binary numbers in a user-friendly way. Each octal symbol represents 3 binary bits and each hex digit represents 4 binary bits. For example, the decimal number 53201, stored as in the computer as the binary number 10000001111000101100, is represented by the octal number 2017054, and the hex number 81E2C. The table below compares the number systems: <br />
:{| class="wikitable"<br />
!Number System<br />
!Base<br />
!Digits Used<br />
!Examples<br />
|-<br />
|Binary<br />
|2<br />
|0,1<br />
|$10110_{2}$, $10110011_{2}$<br />
|-<br />
|Octal<br />
|8<br />
|0,1,2,3,4,5,6,7<br />
|$75021_{8}$, $231_{8}$, $60012_{8}$<br />
|-<br />
|Decimal<br />
|10<br />
|0,1,2,3,4,5,6,7,8,9<br />
|$97425_{10}$ or simply 97425<br />
|-<br />
|Hexadecimal<br />
|16<br />
|0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F<br />
|$54A2DD0F_{16}$<br />
|}<br />
<br />
<br />
In general, $N$ bits have $2^N$ different values. The computers aboard the Apollo spacecraft had 8-bit words of memory. Each word of memory could store 256 different values, and the contents were displayed using 2 hex characters.<br />
<br />
The following table shows the first 20 numbers in decimal, binary, octal, and hexadecimal:<br />
:{| class="wikitable" style="text-align:center"<br />
!Decimal<br />
!Binary<br />
!Octal<br />
!Hexadecimal<br />
|-<br />
|0<br />
|0<br />
|0<br />
|0 <br />
|-<br />
|1<br />
|1<br />
|1<br />
|1 <br />
|-<br />
|2<br />
|10<br />
|2<br />
|2 <br />
|-<br />
|3<br />
|11<br />
|3<br />
|3 <br />
|-<br />
|4<br />
|100<br />
|4<br />
|4 <br />
|-<br />
|5<br />
|101<br />
|5<br />
|5 <br />
|-<br />
|6<br />
|110<br />
|6<br />
|6 <br />
|-<br />
|7<br />
|111<br />
|7<br />
|7 <br />
|-<br />
|8<br />
|1000<br />
|10<br />
|8<br />
|-<br />
|9<br />
|1001<br />
|11<br />
|9<br />
|-<br />
|10<br />
|1010<br />
|12<br />
|A<br />
|-<br />
|11<br />
|1011<br />
|13<br />
|B<br />
|-<br />
|12<br />
|1100<br />
|14<br />
|C <br />
|-<br />
|13<br />
|1101<br />
|15<br />
|D <br />
|-<br />
|14<br />
|1110<br />
|16<br />
|E <br />
|-<br />
|15<br />
|1111<br />
|17<br />
|F <br />
|-<br />
|16<br />
|10000<br />
|20<br />
|10 <br />
|-<br />
|17<br />
|10001<br />
|21<br />
|11<br />
|-<br />
|18<br />
|10010<br />
|22<br />
|12<br />
|-<br />
|19<br />
|10011<br />
|23<br />
|13<br />
|-<br />
|20<br />
|10100<br />
|24<br />
|14<br />
|}<br />
<br />
== Converting to Decimal ==<br />
<br />
As we learned in the first or second grade, the decimal value of a decimal number is simply sum of each digit multiplied by its place value:<br />
<br />
:$12345 = 1×10^{4}+ 2×10^{3}+ 3×10^{2}+ 4×10^{1}+ 5×10{0} = 1×10000 + 2×1000 + 3×100 + 4×10 + 5×1 = 10000 + 2000+ 300 +40+5 = 12345$<br />
<br />
:$3079 = 3×10^{3}+ 0×10^{2}+ 7×10^{1}+ 9×10^{0} = 3000 + 70 + 9 = 3079$<br />
<br />
Analogously, the decimal value of a number in an arbitrary base is the sum of each digit multiplied by its place value. Here are some examples of converting from one base into base 10:<br />
<br />
:$1101_{2} = 1 × 2^3 + 1 × 2^2 + 0 × 2^1 + 1 × 2^0 = 8 + 4 + 0 + 1 = 13_{10}$ <br />
<br />
:$175_{8} = 1 × 8^2 + 7 × 8^1 + 5 × 8^0 = 1 × 64 + 7 × 8 + 5 × 1 = 64 + 56 + 5 = 125_{10}$<br />
<br />
:$A5E_{16} = 10 × 16^2 + 5 × 16^1 + 14 × 16^0 = 10 × 256 + 5 × 16 + 14 × 1 = 2560 + 80 + 14 = 2654_{10}$<br />
<br />
== Converting from Decimal ==<br />
<br />
The algorithm to convert a number from an arbitrary base requires finding how many times successive powers of the base go into the number, starting with the largest power of the base less than the starting number. For example, converting 3206 from base 10 to octal proceeds as follows:<br />
<br />
1) The largest power of 8 that is less than or equal to 3206 is 512 ($8^3$). Divide 3206 by 512:<br />
:$3206 = 6*8^3 + 234$ <br />
<br />
2) The next power of 8 is 64 ($8^2$). Divide 234 by 64:<br />
:$234 = 3 *8^2 + 42$<br />
<br />
3) The next smaller power of 8 is 8 ($8^1$). Divide 42 by 8:<br />
:$42 = 5*8^1 + 2$<br />
<br />
4) Finally, the next smaller power of 8 is 1 ($8^0$). Divide 2 by 1:<br />
:$2 = 2 * 8^0$<br />
<br />
The answer is $6352_8$.<br />
<br />
== Converting between Binary, Octal, and Hexadecimal == <br />
<br />
Converting from octal to binary is simple: replace each octal digit by its corresponding 3 binary bits. For example:<br />
<br />
:$375_{8} = \ \ 011\ \ \ 111\ \ \ 101_{2} = 11111101_2$<br />
<br />
Converting from hex to binary is also simple: replace each hex digit by its corresponding 4 binary bits. For example:<br />
<br />
:$FD_{16} = \ \ 1111\ \ \ 1101_{2} = 11111101_2$<br />
<br />
Converting from binary to either octal or hex is pretty simple as well: group the bits by 3s or 4s (starting at the right), and convert each group:<br />
<br />
:$10000001111000101100 = 10\ 000\ 001\ 111\ 000\ 101 \ 100 = 2017054_8$<br />
<br />
:$10000001111000101100 = 1000\ 0001\ 1110\ 0010\ 1100 = 81E2C_{16}$<br />
<br />
Converting between base 8 and 16 is easy by expressing the number in base 2 (easy to do!) and then converting that number from base 2 (another easy operation)! This is shown below in Sample Problem #1.<br />
<br />
== Using Hexadecimal Numbers to Represent Colors ==<br />
<br />
Computers use hexadecimal numbers to represent various colors in computer graphics because all computer screens use combinations of red, green, and blue light or RGB to represent thousands of different colors. Two digits are used for each so the hexadecimal number “#FF0000” represents the color red, “#00FF00” represents green, and “#0000FF” represents blue. The color black is “#000000” and white is “#FFFFFF”. <br />
<br />
The hash tag or number sign is used to denote a hexadecimal number. $FF_{16} = F (15) × 16 + F (15) × 1 = 240 + 15 = 255_{10}$ so there are 0 to 255 or 256 different shades of each color or $256^{3} = 16,777,216$ different colors. <br />
<br />
The following web site has nearly every color name, along with its hex code and decimal values: <br />
<br />
:https://www.rapidtables.com/web/color/RGB_Color.html <br />
<br />
For example “salmon” is “#FA8072” which represents the decimal numbers 250 (hex FA), 128 (hex 80), and 114 (hex 72).<br />
<br />
== Resources ==<br />
<br />
[https://ryanstutorials.net/ Ryan's Tutorials] covers this topic beautifully. Rather than trying to duplicate that work, we'll point you to the different sections:<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/ 1. Number Systems] - An introduction to what numbers systems are all about, with emphasis on decimal, binary, octal, and hexadecimal. ACSL will typically identify the base of a number using a subscript. For example, $123_8$ is an octal number, whereas $123_{16}$ is a hexadecimal number. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-conversions.php 2. Binary Conversions] - This section shows how to convert between binary, decimal, hexadecimal and octal numbers. In the [https://ryanstutorials.net/binary-tutorial/binary-conversions.php#activities ''Activities''] section, you can practice converting numbers. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-arithmetic.php 3. Binary Arithmetic] - Describes how to perform various arithmetic operations (addition, subtraction, multiplication, and division) with binary numbers. ACSL problems will also cover basic arithmetic in other bases, such as adding and subtracting together 2 hexadecimal numbers. ACSL problems will not cover division in other bases.<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php 4. Negative Numbers] - ACSL problems will not cover how negative numbers are represented in binary. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-floating-point.php 5. Binary Fractions and Floating Point] - The first part of this section is relevant to ACSL: fractions in other bases. ACSL will not cover floating point numbers in other basis. So, focus on the section [https://ryanstutorials.net/binary-tutorial/binary-floating-point.php#convertfraction Converting to a Binary Fraction], but keep in mind that ACSL problems may also cover octal and hexadecimal fractions.<br />
<br />
The [https://coolconversion.com/math/binary-octal-hexa-decimal/ CoolConversion.com online calculator] is another online app for practicing conversion from/to decimal, hexadecimal, octal and binary; this tool shows the steps that one goes through in the conversion. <br />
<br />
The AskNumbers.com site has a nice description of the conversions between [https://www.asknumbers.com/hex-to-octal.aspx binary, octal, decimal and hexadecimal] numbers. <br />
<br />
== Format of ACSL Problems ==<br />
<br />
The problems in this category will focus on converting between binary, octal, decimal, and hexadecimal, basic arithmetic of numbers in those bases, and, occasionally, fractions in those bases.<br />
<br />
To be successful in this category, you must know the following facts cold:<br />
<br />
# The binary value of each octal digit 0, 1, ..., 7<br />
# The binary value of each hex digit 0, 1, ..., 9, A, B, C, D, E, F<br />
# The decimal value of each hex digit 0, 1, ..., F <br />
# Powers of 2, up to 4096<br />
# Powers of 8, up to 4096<br />
# Powers of 16, up to 65,536<br />
<br />
== Sample Problems ==<br />
<br />
=== Sample Problem 1 ===<br />
<br />
Solve for $x$ where $x_{16}=3676_8$.<br />
<br />
'''Solution:''' One method of solution is to convert $3676_8$ into base 10, and then convert that number into base 16 to yield the value of $x$.<br />
<br />
An easier solution, less prone to arithmetic mistakes, is to convert from octal (base 8) to hexadecimal (base 16) through the binary (base 2) representation of the number:<br />
<br />
$$\begin{align}<br />
3676_8 &= 011 ~ 110 ~ 111 ~ 110_2 & \text{convert each octal digit into base 2}\hfill\cr <br />
&= 0111 ~ 1011 ~ 1110_2 & \text{group by 4 bits, from right-to-left}\hfill\cr<br />
&= 7 ~ \text{B} ~ \text{E}_{16} & \text{convert each group of 4 bits into a hex digit}\cr<br />
\end{align}$$<br />
<br />
=== Sample Problem 2 ===<br />
<br />
Solve for $x$ in the following hexadecimal equation: $ x= \text{F5AD}_{16} - \text{69EB}_{16}$<br />
<br />
'''Solution:''' One could convert the hex numbers into base 10, perform the subtraction, and then convert the answer back to base 16. However, <br />
working directly in base 16 isn't too hard. <br />
As in conventional decimal arithmetic, one works from right-to-left, from the least significant digits to the most. <br />
<br />
:The rightmost digit becomes 2, because D-B=2. <br />
:The next column is A-E. We need to ''borrow'' a one from the 5 column, and 1A-E=C <br />
:In the next column, 4-9=B, again, borrowing a 1 from the next column.<br />
:Finally, the leftmost column, E-6=8<br />
<br />
Combining these results of each column, we get a final answer of $8BC2_{16}$.<br />
<br />
=== Sample Problem 3 ===<br />
<br />
How many numbers from 100 to 200 in base 10 consist of distinct<br />
ascending digits and also have distinct ascending hex digits when<br />
converted to base 16? <br />
<br />
'''Solution:''' There are 13 numbers that have ascending digits in both bases from 100<br />
to 200. They are (in base 10):<br />
123 (7B), 124, 125, 126, 127 (7F), 137 (89), 138, 139 (8B), 156 (9C), 157, 158, 159 (9F), 189 (BD)<br />
<br />
== Video Resources ==<br />
<br />
There are many YouTube videos about computer number systems. Here are a handful that cover the topic nicely, without too many ads:<br />
<br />
{|<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/aW3qCcH6Dao</youtube><br />
| [https://youtu.be/aW3qCcH6Dao ''Number Systems - Converting Decimal, Binary and Hexadecimal'' ('''Joe James''')]<br />
<br />
An introduction to number systems, and how to convert between decimal, binary and hexadecimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/m1JtWKuTLR0</youtube><br />
| [https://youtu.be/m1JtWKuTLR0 ''Lesson 2.3 : Hexadecimal Tutorial'' ('''Carl Herold''')]<br />
<br />
The video focuses on hexadecimal numbers: their relationship to binary numbers and how to convert to decimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/WR67syBDzew</youtube><br />
| [https://youtu.be/WR67syBDzew ''Hexes and the Magic of Base 16 - Vaidehi Joshi - May 2017' ('''DonutJS''')]<br />
<br />
A fun introduction to hexadecimal numbers, focusing a bit <br />
on using hex numbers for specifying RGB colors. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jvx-NrILgpE</youtube><br />
| [https://youtu.be/jvx-NrILgpE ''Collins Lab: Binary & Hex'' ('''Adafruit Industries''')]<br />
<br />
A professionally produced video that explains the number systems, how and why binary numbers are fundamental to computer science, and why hexadecimal is important to computer programmers. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Computer_Number_Systems&diff=835Computer Number Systems2020-09-02T23:05:07Z<p>Marc: /* Decimal Values */</p>
<hr />
<div>All digital computers, from supercomputers to your smartphone, are electronic devices and ultimately can do one thing: detect whether an electrical signal is on or off. That basic information, called a ''bit'' ('''bi'''nary digi'''t'''), has two values: a 1 (or ''true'') when the signal is on, and a 0 (of ''false'') when the signal is off. <br />
<br />
Larger values can be stored by a group of bits. For example, there are 4 different values stored by 2 bits (00, 01, 10, and 11), 8 values for 3 bits (000, 001, 010, 011, 100, 101, 110, and 111), 16 values for 4 bits (0000, 0001, ..., 1111), and so on. However, large numbers, using 0s and 1s only, are quite unwieldy for humans. For example, a computer would need 19 bits to store the numbers up to 500,000! We use of different number systems to work with large binary strings that represent numbers within computers.<br />
<br />
== Different Number Systems ==<br />
<br />
A ''number system'' is the way we name and represent numbers. The number system we use in our daily life is known as '''decimal number system''' or '''base 10''' because it is based on 10 different digits: 0, 1, 2, ..., 8, and 9. The base of number is written as subscript to a number. For example, the decimal number "twelve thousand three hundred and forty-five" is written as $12345_{10}$. Without a base or explicit context, it's assumed that a number is in base 10. <br />
<br />
In computer science, apart from the decimal system, three additional number systems are commonly used: '''binary''' (base-2), '''octal''' (base-8), and '''hexadecimal''' or just '''hex''' (base-16). Binary numbers are important because that is how numbers are stored in the computer. Octal and hexadecimal are used to represent binary numbers in a user-friendly way. Each octal symbol represents 3 binary bits and each hex digit represents 4 binary bits. For example, the decimal number 53201, stored as in the computer as the binary number 10000001111000101100, is represented by the octal number 2017054, and the hex number 81E2C. The table below compares the number systems: <br />
:{| class="wikitable"<br />
!Number System<br />
!Base<br />
!Digits Used<br />
!Examples<br />
|-<br />
|Binary<br />
|2<br />
|0,1<br />
|$10110_{2}$, $10110011_{2}$<br />
|-<br />
|Octal<br />
|8<br />
|0,1,2,3,4,5,6,7<br />
|$75021_{8}$, $231_{8}$, $60012_{8}$<br />
|-<br />
|Decimal<br />
|10<br />
|0,1,2,3,4,5,6,7,8,9<br />
|$97425_{10}$ or simply 97425<br />
|-<br />
|Hexadecimal<br />
|16<br />
|0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F<br />
|$54A2DD0F_{16}$<br />
|}<br />
<br />
<br />
In general, $N$ bits have $2^N$ different values. The computers aboard the Apollo spacecraft had 8-bit words of memory. Each word of memory could store 256 different values, and the contents were displayed using 2 hex characters.<br />
<br />
The following table shows the first 20 numbers in decimal, binary, octal, and hexadecimal:<br />
:{| class="wikitable" style="text-align:center"<br />
!Decimal<br />
!Binary<br />
!Octal<br />
!Hexadecimal<br />
|-<br />
|0<br />
|0<br />
|0<br />
|0 <br />
|-<br />
|1<br />
|1<br />
|1<br />
|1 <br />
|-<br />
|2<br />
|10<br />
|2<br />
|2 <br />
|-<br />
|3<br />
|11<br />
|3<br />
|3 <br />
|-<br />
|4<br />
|100<br />
|4<br />
|4 <br />
|-<br />
|5<br />
|101<br />
|5<br />
|5 <br />
|-<br />
|6<br />
|110<br />
|6<br />
|6 <br />
|-<br />
|7<br />
|111<br />
|7<br />
|7 <br />
|-<br />
|8<br />
|1000<br />
|10<br />
|8<br />
|-<br />
|9<br />
|1001<br />
|11<br />
|9<br />
|-<br />
|10<br />
|1010<br />
|12<br />
|A<br />
|-<br />
|11<br />
|1011<br />
|13<br />
|B<br />
|-<br />
|12<br />
|1100<br />
|14<br />
|C <br />
|-<br />
|13<br />
|1101<br />
|15<br />
|D <br />
|-<br />
|14<br />
|1110<br />
|16<br />
|E <br />
|-<br />
|15<br />
|1111<br />
|17<br />
|F <br />
|-<br />
|16<br />
|10000<br />
|20<br />
|10 <br />
|-<br />
|17<br />
|10001<br />
|21<br />
|11<br />
|-<br />
|18<br />
|10010<br />
|22<br />
|12<br />
|-<br />
|19<br />
|10011<br />
|23<br />
|13<br />
|-<br />
|20<br />
|10100<br />
|24<br />
|14<br />
|}<br />
<br />
== Converting to Decimal ==<br />
<br />
The decimal value of a number in an arbitrary base is the sum of each digit multiplied by its place value. Here are some examples of converting from one base into base 10:<br />
<br />
:$12345 = 1×10000 + 2×1000 + 3×100 + 4×10 + 5×1 = 1×10^{4}+ 2×10^{3}+ 3×10^{2}+ 4×10^{1}+ 5×10{0}$<br />
<br />
:$1101_{2} = 1 × 2^3 + 1 × 2^2 + 0 × 2^1 + 1 × 2^0 = 8 + 4 + 0 + 1 = 13_{10}$ <br />
<br />
:$175_{8} = 1 × 8^2 + 7 × 8^1 + 5 × 8^0 = 1 × 64 + 7 × 8 + 5 × 1 = 64 + 56 + 5 = 125_{10}$<br />
<br />
:$A5E_{16} = 10 × 16^2 + 5 × 16^1 + 14 × 16^0 = 10 × 256 + 5 × 16 + 14 × 1 = 2560 + 80 + 14 = 2654_{10}$<br />
<br />
== Converting between Binary, Octal, and Hexadecimal == <br />
<br />
Converting from octal to binary is simple: replace each octal digit by its corresponding 3 binary bits. For example:<br />
<br />
:$375_{8} = \ \ 011\ \ \ 111\ \ \ 101_{2} = 11111101_2$<br />
<br />
Converting from hex to binary is also simple: replace each hex digit by its corresponding 4 binary bits. For example:<br />
<br />
:$FD_{16} = \ \ 1111\ \ \ 1101_{2} = 11111101_2$<br />
<br />
Converting from binary to either octal or hex is pretty simple as well: group the bits by 3s or 4s (starting at the right), and convert each group:<br />
<br />
:$10000001111000101100 = 10\ 000\ 001\ 111\ 000\ 101 \ 100 = 2017054_8$<br />
<br />
:$10000001111000101100 = 1000\ 0001\ 1110\ 0010\ 1100 = 81E2C_{16}$<br />
<br />
Converting between base 8 and 16 is easy by expressing the number in base 2 (easy to do!) and then converting that number from base 2 (another easy operation)! This is shown below in Sample Problem #1.<br />
<br />
== Using Hexadecimal Numbers to Represent Colors ==<br />
<br />
Computers use hexadecimal numbers to represent various colors in computer graphics because all computer screens use combinations of red, green, and blue light or RGB to represent thousands of different colors. Two digits are used for each so the hexadecimal number “#FF0000” represents the color red, “#00FF00” represents green, and “#0000FF” represents blue. The color black is “#000000” and white is “#FFFFFF”. <br />
<br />
The hash tag or number sign is used to denote a hexadecimal number. $FF_{16} = F (15) × 16 + F (15) × 1 = 240 + 15 = 255_{10}$ so there are 0 to 255 or 256 different shades of each color or $256^{3} = 16,777,216$ different colors. <br />
<br />
The following web site has nearly every color name, along with its hex code and decimal values: <br />
<br />
:https://www.rapidtables.com/web/color/RGB_Color.html <br />
<br />
For example “salmon” is “#FA8072” which represents the decimal numbers 250 (hex FA), 128 (hex 80), and 114 (hex 72).<br />
<br />
== Resources ==<br />
<br />
[https://ryanstutorials.net/ Ryan's Tutorials] covers this topic beautifully. Rather than trying to duplicate that work, we'll point you to the different sections:<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/ 1. Number Systems] - An introduction to what numbers systems are all about, with emphasis on decimal, binary, octal, and hexadecimal. ACSL will typically identify the base of a number using a subscript. For example, $123_8$ is an octal number, whereas $123_{16}$ is a hexadecimal number. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-conversions.php 2. Binary Conversions] - This section shows how to convert between binary, decimal, hexadecimal and octal numbers. In the [https://ryanstutorials.net/binary-tutorial/binary-conversions.php#activities ''Activities''] section, you can practice converting numbers. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-arithmetic.php 3. Binary Arithmetic] - Describes how to perform various arithmetic operations (addition, subtraction, multiplication, and division) with binary numbers. ACSL problems will also cover basic arithmetic in other bases, such as adding and subtracting together 2 hexadecimal numbers. ACSL problems will not cover division in other bases.<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php 4. Negative Numbers] - ACSL problems will not cover how negative numbers are represented in binary. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-floating-point.php 5. Binary Fractions and Floating Point] - The first part of this section is relevant to ACSL: fractions in other bases. ACSL will not cover floating point numbers in other basis. So, focus on the section [https://ryanstutorials.net/binary-tutorial/binary-floating-point.php#convertfraction Converting to a Binary Fraction], but keep in mind that ACSL problems may also cover octal and hexadecimal fractions.<br />
<br />
The [https://coolconversion.com/math/binary-octal-hexa-decimal/ CoolConversion.com online calculator] is another online app for practicing conversion from/to decimal, hexadecimal, octal and binary; this tool shows the steps that one goes through in the conversion. <br />
<br />
The AskNumbers.com site has a nice description of the conversions between [https://www.asknumbers.com/hex-to-octal.aspx binary, octal, decimal and hexadecimal] numbers. <br />
<br />
== Format of ACSL Problems ==<br />
<br />
The problems in this category will focus on converting between binary, octal, decimal, and hexadecimal, basic arithmetic of numbers in those bases, and, occasionally, fractions in those bases.<br />
<br />
To be successful in this category, you must know the following facts cold:<br />
<br />
# The binary value of each octal digit 0, 1, ..., 7<br />
# The binary value of each hex digit 0, 1, ..., 9, A, B, C, D, E, F<br />
# The decimal value of each hex digit 0, 1, ..., F <br />
# Powers of 2, up to 4096<br />
# Powers of 8, up to 4096<br />
# Powers of 16, up to 65,536<br />
<br />
== Sample Problems ==<br />
<br />
=== Sample Problem 1 ===<br />
<br />
Solve for $x$ where $x_{16}=3676_8$.<br />
<br />
'''Solution:''' One method of solution is to convert $3676_8$ into base 10, and then convert that number into base 16 to yield the value of $x$.<br />
<br />
An easier solution, less prone to arithmetic mistakes, is to convert from octal (base 8) to hexadecimal (base 16) through the binary (base 2) representation of the number:<br />
<br />
$$\begin{align}<br />
3676_8 &= 011 ~ 110 ~ 111 ~ 110_2 & \text{convert each octal digit into base 2}\hfill\cr <br />
&= 0111 ~ 1011 ~ 1110_2 & \text{group by 4 bits, from right-to-left}\hfill\cr<br />
&= 7 ~ \text{B} ~ \text{E}_{16} & \text{convert each group of 4 bits into a hex digit}\cr<br />
\end{align}$$<br />
<br />
=== Sample Problem 2 ===<br />
<br />
Solve for $x$ in the following hexadecimal equation: $ x= \text{F5AD}_{16} - \text{69EB}_{16}$<br />
<br />
'''Solution:''' One could convert the hex numbers into base 10, perform the subtraction, and then convert the answer back to base 16. However, <br />
working directly in base 16 isn't too hard. <br />
As in conventional decimal arithmetic, one works from right-to-left, from the least significant digits to the most. <br />
<br />
:The rightmost digit becomes 2, because D-B=2. <br />
:The next column is A-E. We need to ''borrow'' a one from the 5 column, and 1A-E=C <br />
:In the next column, 4-9=B, again, borrowing a 1 from the next column.<br />
:Finally, the leftmost column, E-6=8<br />
<br />
Combining these results of each column, we get a final answer of $8BC2_{16}$.<br />
<br />
=== Sample Problem 3 ===<br />
<br />
How many numbers from 100 to 200 in base 10 consist of distinct<br />
ascending digits and also have distinct ascending hex digits when<br />
converted to base 16? <br />
<br />
'''Solution:''' There are 13 numbers that have ascending digits in both bases from 100<br />
to 200. They are (in base 10):<br />
123 (7B), 124, 125, 126, 127 (7F), 137 (89), 138, 139 (8B), 156 (9C), 157, 158, 159 (9F), 189 (BD)<br />
<br />
== Video Resources ==<br />
<br />
There are many YouTube videos about computer number systems. Here are a handful that cover the topic nicely, without too many ads:<br />
<br />
{|<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/aW3qCcH6Dao</youtube><br />
| [https://youtu.be/aW3qCcH6Dao ''Number Systems - Converting Decimal, Binary and Hexadecimal'' ('''Joe James''')]<br />
<br />
An introduction to number systems, and how to convert between decimal, binary and hexadecimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/m1JtWKuTLR0</youtube><br />
| [https://youtu.be/m1JtWKuTLR0 ''Lesson 2.3 : Hexadecimal Tutorial'' ('''Carl Herold''')]<br />
<br />
The video focuses on hexadecimal numbers: their relationship to binary numbers and how to convert to decimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/WR67syBDzew</youtube><br />
| [https://youtu.be/WR67syBDzew ''Hexes and the Magic of Base 16 - Vaidehi Joshi - May 2017' ('''DonutJS''')]<br />
<br />
A fun introduction to hexadecimal numbers, focusing a bit <br />
on using hex numbers for specifying RGB colors. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jvx-NrILgpE</youtube><br />
| [https://youtu.be/jvx-NrILgpE ''Collins Lab: Binary & Hex'' ('''Adafruit Industries''')]<br />
<br />
A professionally produced video that explains the number systems, how and why binary numbers are fundamental to computer science, and why hexadecimal is important to computer programmers. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Computer_Number_Systems&diff=834Computer Number Systems2020-09-02T18:40:15Z<p>Marc: /* Converting between Binary, Octal, and Hexadecimal */</p>
<hr />
<div>All digital computers, from supercomputers to your smartphone, are electronic devices and ultimately can do one thing: detect whether an electrical signal is on or off. That basic information, called a ''bit'' ('''bi'''nary digi'''t'''), has two values: a 1 (or ''true'') when the signal is on, and a 0 (of ''false'') when the signal is off. <br />
<br />
Larger values can be stored by a group of bits. For example, there are 4 different values stored by 2 bits (00, 01, 10, and 11), 8 values for 3 bits (000, 001, 010, 011, 100, 101, 110, and 111), 16 values for 4 bits (0000, 0001, ..., 1111), and so on. However, large numbers, using 0s and 1s only, are quite unwieldy for humans. For example, a computer would need 19 bits to store the numbers up to 500,000! We use of different number systems to work with large binary strings that represent numbers within computers.<br />
<br />
== Different Number Systems ==<br />
<br />
A ''number system'' is the way we name and represent numbers. The number system we use in our daily life is known as '''decimal number system''' or '''base 10''' because it is based on 10 different digits: 0, 1, 2, ..., 8, and 9. The base of number is written as subscript to a number. For example, the decimal number "twelve thousand three hundred and forty-five" is written as $12345_{10}$. Without a base or explicit context, it's assumed that a number is in base 10. <br />
<br />
In computer science, apart from the decimal system, three additional number systems are commonly used: '''binary''' (base-2), '''octal''' (base-8), and '''hexadecimal''' or just '''hex''' (base-16). Binary numbers are important because that is how numbers are stored in the computer. Octal and hexadecimal are used to represent binary numbers in a user-friendly way. Each octal symbol represents 3 binary bits and each hex digit represents 4 binary bits. For example, the decimal number 53201, stored as in the computer as the binary number 10000001111000101100, is represented by the octal number 2017054, and the hex number 81E2C. The table below compares the number systems: <br />
:{| class="wikitable"<br />
!Number System<br />
!Base<br />
!Digits Used<br />
!Examples<br />
|-<br />
|Binary<br />
|2<br />
|0,1<br />
|$10110_{2}$, $10110011_{2}$<br />
|-<br />
|Octal<br />
|8<br />
|0,1,2,3,4,5,6,7<br />
|$75021_{8}$, $231_{8}$, $60012_{8}$<br />
|-<br />
|Decimal<br />
|10<br />
|0,1,2,3,4,5,6,7,8,9<br />
|$97425_{10}$ or simply 97425<br />
|-<br />
|Hexadecimal<br />
|16<br />
|0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F<br />
|$54A2DD0F_{16}$<br />
|}<br />
<br />
<br />
In general, $N$ bits have $2^N$ different values. The computers aboard the Apollo spacecraft had 8-bit words of memory. Each word of memory could store 256 different values, and the contents were displayed using 2 hex characters.<br />
<br />
The following table shows the first 20 numbers in decimal, binary, octal, and hexadecimal:<br />
:{| class="wikitable" style="text-align:center"<br />
!Decimal<br />
!Binary<br />
!Octal<br />
!Hexadecimal<br />
|-<br />
|0<br />
|0<br />
|0<br />
|0 <br />
|-<br />
|1<br />
|1<br />
|1<br />
|1 <br />
|-<br />
|2<br />
|10<br />
|2<br />
|2 <br />
|-<br />
|3<br />
|11<br />
|3<br />
|3 <br />
|-<br />
|4<br />
|100<br />
|4<br />
|4 <br />
|-<br />
|5<br />
|101<br />
|5<br />
|5 <br />
|-<br />
|6<br />
|110<br />
|6<br />
|6 <br />
|-<br />
|7<br />
|111<br />
|7<br />
|7 <br />
|-<br />
|8<br />
|1000<br />
|10<br />
|8<br />
|-<br />
|9<br />
|1001<br />
|11<br />
|9<br />
|-<br />
|10<br />
|1010<br />
|12<br />
|A<br />
|-<br />
|11<br />
|1011<br />
|13<br />
|B<br />
|-<br />
|12<br />
|1100<br />
|14<br />
|C <br />
|-<br />
|13<br />
|1101<br />
|15<br />
|D <br />
|-<br />
|14<br />
|1110<br />
|16<br />
|E <br />
|-<br />
|15<br />
|1111<br />
|17<br />
|F <br />
|-<br />
|16<br />
|10000<br />
|20<br />
|10 <br />
|-<br />
|17<br />
|10001<br />
|21<br />
|11<br />
|-<br />
|18<br />
|10010<br />
|22<br />
|12<br />
|-<br />
|19<br />
|10011<br />
|23<br />
|13<br />
|-<br />
|20<br />
|10100<br />
|24<br />
|14<br />
|}<br />
<br />
== Decimal Values ==<br />
<br />
The decimal value of a number in an arbitrary base is the sum of each digit multiplied by its place value. Here are some examples of converting from one base into base 10:<br />
<br />
:$12345 = 1×10000 + 2×1000 + 3×100 + 4×10 + 5×1 = 1×10^{4}+ 2×10^{3}+ 3×10^{2}+ 4×10^{1}+ 5×10{0}.$<br />
<br />
:$1101_{2} = 1 × 2^3 + 1 × 2^2 + 0 × 2^1 + 1 × 2^0 = 8 + 4 + 0 + 1 = 13_{10}$ <br />
<br />
:$175_{8} = 1 × 8^2 + 7 × 8^1 + 5 × 8^0 = 1 × 64 + 7 × 8 + 5 × 1 = 64 + 56 + 5 = 125_{10}$. <br />
<br />
:$A5E_{16} = 10 × 16^2 + 5 × 16^1 + 14 × 16^0 = 10 × 256 + 5 × 16 + 14 × 1 = 2560 + 80 + 14 = 2654_{10}$.<br />
<br />
== Converting between Binary, Octal, and Hexadecimal == <br />
<br />
Converting from octal to binary is simple: replace each octal digit by its corresponding 3 binary bits. For example:<br />
<br />
:$375_{8} = \ \ 011\ \ \ 111\ \ \ 101_{2} = 11111101_2$<br />
<br />
Converting from hex to binary is also simple: replace each hex digit by its corresponding 4 binary bits. For example:<br />
<br />
:$FD_{16} = \ \ 1111\ \ \ 1101_{2} = 11111101_2$<br />
<br />
Converting from binary to either octal or hex is pretty simple as well: group the bits by 3s or 4s (starting at the right), and convert each group:<br />
<br />
:$10000001111000101100 = 10\ 000\ 001\ 111\ 000\ 101 \ 100 = 2017054_8$<br />
<br />
:$10000001111000101100 = 1000\ 0001\ 1110\ 0010\ 1100 = 81E2C_{16}$<br />
<br />
Converting between base 8 and 16 is easy by expressing the number in base 2 (easy to do!) and then converting that number from base 2 (another easy operation)! This is shown below in Sample Problem #1.<br />
<br />
== Using Hexadecimal Numbers to Represent Colors ==<br />
<br />
Computers use hexadecimal numbers to represent various colors in computer graphics because all computer screens use combinations of red, green, and blue light or RGB to represent thousands of different colors. Two digits are used for each so the hexadecimal number “#FF0000” represents the color red, “#00FF00” represents green, and “#0000FF” represents blue. The color black is “#000000” and white is “#FFFFFF”. <br />
<br />
The hash tag or number sign is used to denote a hexadecimal number. $FF_{16} = F (15) × 16 + F (15) × 1 = 240 + 15 = 255_{10}$ so there are 0 to 255 or 256 different shades of each color or $256^{3} = 16,777,216$ different colors. <br />
<br />
The following web site has nearly every color name, along with its hex code and decimal values: <br />
<br />
:https://www.rapidtables.com/web/color/RGB_Color.html <br />
<br />
For example “salmon” is “#FA8072” which represents the decimal numbers 250 (hex FA), 128 (hex 80), and 114 (hex 72).<br />
<br />
== Resources ==<br />
<br />
[https://ryanstutorials.net/ Ryan's Tutorials] covers this topic beautifully. Rather than trying to duplicate that work, we'll point you to the different sections:<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/ 1. Number Systems] - An introduction to what numbers systems are all about, with emphasis on decimal, binary, octal, and hexadecimal. ACSL will typically identify the base of a number using a subscript. For example, $123_8$ is an octal number, whereas $123_{16}$ is a hexadecimal number. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-conversions.php 2. Binary Conversions] - This section shows how to convert between binary, decimal, hexadecimal and octal numbers. In the [https://ryanstutorials.net/binary-tutorial/binary-conversions.php#activities ''Activities''] section, you can practice converting numbers. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-arithmetic.php 3. Binary Arithmetic] - Describes how to perform various arithmetic operations (addition, subtraction, multiplication, and division) with binary numbers. ACSL problems will also cover basic arithmetic in other bases, such as adding and subtracting together 2 hexadecimal numbers. ACSL problems will not cover division in other bases.<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php 4. Negative Numbers] - ACSL problems will not cover how negative numbers are represented in binary. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-floating-point.php 5. Binary Fractions and Floating Point] - The first part of this section is relevant to ACSL: fractions in other bases. ACSL will not cover floating point numbers in other basis. So, focus on the section [https://ryanstutorials.net/binary-tutorial/binary-floating-point.php#convertfraction Converting to a Binary Fraction], but keep in mind that ACSL problems may also cover octal and hexadecimal fractions.<br />
<br />
The [https://coolconversion.com/math/binary-octal-hexa-decimal/ CoolConversion.com online calculator] is another online app for practicing conversion from/to decimal, hexadecimal, octal and binary; this tool shows the steps that one goes through in the conversion. <br />
<br />
The AskNumbers.com site has a nice description of the conversions between [https://www.asknumbers.com/hex-to-octal.aspx binary, octal, decimal and hexadecimal] numbers. <br />
<br />
== Format of ACSL Problems ==<br />
<br />
The problems in this category will focus on converting between binary, octal, decimal, and hexadecimal, basic arithmetic of numbers in those bases, and, occasionally, fractions in those bases.<br />
<br />
To be successful in this category, you must know the following facts cold:<br />
<br />
# The binary value of each octal digit 0, 1, ..., 7<br />
# The binary value of each hex digit 0, 1, ..., 9, A, B, C, D, E, F<br />
# The decimal value of each hex digit 0, 1, ..., F <br />
# Powers of 2, up to 4096<br />
# Powers of 8, up to 4096<br />
# Powers of 16, up to 65,536<br />
<br />
== Sample Problems ==<br />
<br />
=== Sample Problem 1 ===<br />
<br />
Solve for $x$ where $x_{16}=3676_8$.<br />
<br />
'''Solution:''' One method of solution is to convert $3676_8$ into base 10, and then convert that number into base 16 to yield the value of $x$.<br />
<br />
An easier solution, less prone to arithmetic mistakes, is to convert from octal (base 8) to hexadecimal (base 16) through the binary (base 2) representation of the number:<br />
<br />
$$\begin{align}<br />
3676_8 &= 011 ~ 110 ~ 111 ~ 110_2 & \text{convert each octal digit into base 2}\hfill\cr <br />
&= 0111 ~ 1011 ~ 1110_2 & \text{group by 4 bits, from right-to-left}\hfill\cr<br />
&= 7 ~ \text{B} ~ \text{E}_{16} & \text{convert each group of 4 bits into a hex digit}\cr<br />
\end{align}$$<br />
<br />
=== Sample Problem 2 ===<br />
<br />
Solve for $x$ in the following hexadecimal equation: $ x= \text{F5AD}_{16} - \text{69EB}_{16}$<br />
<br />
'''Solution:''' One could convert the hex numbers into base 10, perform the subtraction, and then convert the answer back to base 16. However, <br />
working directly in base 16 isn't too hard. <br />
As in conventional decimal arithmetic, one works from right-to-left, from the least significant digits to the most. <br />
<br />
:The rightmost digit becomes 2, because D-B=2. <br />
:The next column is A-E. We need to ''borrow'' a one from the 5 column, and 1A-E=C <br />
:In the next column, 4-9=B, again, borrowing a 1 from the next column.<br />
:Finally, the leftmost column, E-6=8<br />
<br />
Combining these results of each column, we get a final answer of $8BC2_{16}$.<br />
<br />
=== Sample Problem 3 ===<br />
<br />
How many numbers from 100 to 200 in base 10 consist of distinct<br />
ascending digits and also have distinct ascending hex digits when<br />
converted to base 16? <br />
<br />
'''Solution:''' There are 13 numbers that have ascending digits in both bases from 100<br />
to 200. They are (in base 10):<br />
123 (7B), 124, 125, 126, 127 (7F), 137 (89), 138, 139 (8B), 156 (9C), 157, 158, 159 (9F), 189 (BD)<br />
<br />
== Video Resources ==<br />
<br />
There are many YouTube videos about computer number systems. Here are a handful that cover the topic nicely, without too many ads:<br />
<br />
{|<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/aW3qCcH6Dao</youtube><br />
| [https://youtu.be/aW3qCcH6Dao ''Number Systems - Converting Decimal, Binary and Hexadecimal'' ('''Joe James''')]<br />
<br />
An introduction to number systems, and how to convert between decimal, binary and hexadecimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/m1JtWKuTLR0</youtube><br />
| [https://youtu.be/m1JtWKuTLR0 ''Lesson 2.3 : Hexadecimal Tutorial'' ('''Carl Herold''')]<br />
<br />
The video focuses on hexadecimal numbers: their relationship to binary numbers and how to convert to decimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/WR67syBDzew</youtube><br />
| [https://youtu.be/WR67syBDzew ''Hexes and the Magic of Base 16 - Vaidehi Joshi - May 2017' ('''DonutJS''')]<br />
<br />
A fun introduction to hexadecimal numbers, focusing a bit <br />
on using hex numbers for specifying RGB colors. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jvx-NrILgpE</youtube><br />
| [https://youtu.be/jvx-NrILgpE ''Collins Lab: Binary & Hex'' ('''Adafruit Industries''')]<br />
<br />
A professionally produced video that explains the number systems, how and why binary numbers are fundamental to computer science, and why hexadecimal is important to computer programmers. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Computer_Number_Systems&diff=833Computer Number Systems2020-09-02T16:33:15Z<p>Marc: /* Different Number Systems */</p>
<hr />
<div>All digital computers, from supercomputers to your smartphone, are electronic devices and ultimately can do one thing: detect whether an electrical signal is on or off. That basic information, called a ''bit'' ('''bi'''nary digi'''t'''), has two values: a 1 (or ''true'') when the signal is on, and a 0 (of ''false'') when the signal is off. <br />
<br />
Larger values can be stored by a group of bits. For example, there are 4 different values stored by 2 bits (00, 01, 10, and 11), 8 values for 3 bits (000, 001, 010, 011, 100, 101, 110, and 111), 16 values for 4 bits (0000, 0001, ..., 1111), and so on. However, large numbers, using 0s and 1s only, are quite unwieldy for humans. For example, a computer would need 19 bits to store the numbers up to 500,000! We use of different number systems to work with large binary strings that represent numbers within computers.<br />
<br />
== Different Number Systems ==<br />
<br />
A ''number system'' is the way we name and represent numbers. The number system we use in our daily life is known as '''decimal number system''' or '''base 10''' because it is based on 10 different digits: 0, 1, 2, ..., 8, and 9. The base of number is written as subscript to a number. For example, the decimal number "twelve thousand three hundred and forty-five" is written as $12345_{10}$. Without a base or explicit context, it's assumed that a number is in base 10. <br />
<br />
In computer science, apart from the decimal system, three additional number systems are commonly used: '''binary''' (base-2), '''octal''' (base-8), and '''hexadecimal''' or just '''hex''' (base-16). Binary numbers are important because that is how numbers are stored in the computer. Octal and hexadecimal are used to represent binary numbers in a user-friendly way. Each octal symbol represents 3 binary bits and each hex digit represents 4 binary bits. For example, the decimal number 53201, stored as in the computer as the binary number 10000001111000101100, is represented by the octal number 2017054, and the hex number 81E2C. The table below compares the number systems: <br />
:{| class="wikitable"<br />
!Number System<br />
!Base<br />
!Digits Used<br />
!Examples<br />
|-<br />
|Binary<br />
|2<br />
|0,1<br />
|$10110_{2}$, $10110011_{2}$<br />
|-<br />
|Octal<br />
|8<br />
|0,1,2,3,4,5,6,7<br />
|$75021_{8}$, $231_{8}$, $60012_{8}$<br />
|-<br />
|Decimal<br />
|10<br />
|0,1,2,3,4,5,6,7,8,9<br />
|$97425_{10}$ or simply 97425<br />
|-<br />
|Hexadecimal<br />
|16<br />
|0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F<br />
|$54A2DD0F_{16}$<br />
|}<br />
<br />
<br />
In general, $N$ bits have $2^N$ different values. The computers aboard the Apollo spacecraft had 8-bit words of memory. Each word of memory could store 256 different values, and the contents were displayed using 2 hex characters.<br />
<br />
The following table shows the first 20 numbers in decimal, binary, octal, and hexadecimal:<br />
:{| class="wikitable" style="text-align:center"<br />
!Decimal<br />
!Binary<br />
!Octal<br />
!Hexadecimal<br />
|-<br />
|0<br />
|0<br />
|0<br />
|0 <br />
|-<br />
|1<br />
|1<br />
|1<br />
|1 <br />
|-<br />
|2<br />
|10<br />
|2<br />
|2 <br />
|-<br />
|3<br />
|11<br />
|3<br />
|3 <br />
|-<br />
|4<br />
|100<br />
|4<br />
|4 <br />
|-<br />
|5<br />
|101<br />
|5<br />
|5 <br />
|-<br />
|6<br />
|110<br />
|6<br />
|6 <br />
|-<br />
|7<br />
|111<br />
|7<br />
|7 <br />
|-<br />
|8<br />
|1000<br />
|10<br />
|8<br />
|-<br />
|9<br />
|1001<br />
|11<br />
|9<br />
|-<br />
|10<br />
|1010<br />
|12<br />
|A<br />
|-<br />
|11<br />
|1011<br />
|13<br />
|B<br />
|-<br />
|12<br />
|1100<br />
|14<br />
|C <br />
|-<br />
|13<br />
|1101<br />
|15<br />
|D <br />
|-<br />
|14<br />
|1110<br />
|16<br />
|E <br />
|-<br />
|15<br />
|1111<br />
|17<br />
|F <br />
|-<br />
|16<br />
|10000<br />
|20<br />
|10 <br />
|-<br />
|17<br />
|10001<br />
|21<br />
|11<br />
|-<br />
|18<br />
|10010<br />
|22<br />
|12<br />
|-<br />
|19<br />
|10011<br />
|23<br />
|13<br />
|-<br />
|20<br />
|10100<br />
|24<br />
|14<br />
|}<br />
<br />
== Decimal Values ==<br />
<br />
The decimal value of a number in an arbitrary base is the sum of each digit multiplied by its place value. Here are some examples of converting from one base into base 10:<br />
<br />
:$12345 = 1×10000 + 2×1000 + 3×100 + 4×10 + 5×1 = 1×10^{4}+ 2×10^{3}+ 3×10^{2}+ 4×10^{1}+ 5×10{0}.$<br />
<br />
:$1101_{2} = 1 × 2^3 + 1 × 2^2 + 0 × 2^1 + 1 × 2^0 = 8 + 4 + 0 + 1 = 13_{10}$ <br />
<br />
:$175_{8} = 1 × 8^2 + 7 × 8^1 + 5 × 8^0 = 1 × 64 + 7 × 8 + 5 × 1 = 64 + 56 + 5 = 125_{10}$. <br />
<br />
:$A5E_{16} = 10 × 16^2 + 5 × 16^1 + 14 × 16^0 = 10 × 256 + 5 × 16 + 14 × 1 = 2560 + 80 + 14 = 2654_{10}$.<br />
<br />
== Converting between Binary, Octal, and Hexadecimal == <br />
<br />
Converting from octal to base 2 is simple: replace each octal digit by its corresponding 3 binary bits. For example:<br />
<br />
:$375_{8} = \ \ 011\ \ \ 111\ \ \ 101_{2} = 11111101_2$<br />
<br />
Converting from hex to base is also simple: replace each hex digit by its corresponding 4 binary bits. For example:<br />
<br />
:$FD_{16} = \ \ 1111\ \ \ 1101_{2} = 11111101_2$<br />
<br />
Converting from binary to either octal or hex is pretty simple as well: group the bit by 3s or 4s (starting at the right), and convert each group:<br />
<br />
:$10000001111000101100 = 10\ 000\ 001\ 111\ 000\ 101 \ 100 = 2017054_8$<br />
<br />
:$10000001111000101100 = 1000\ 0001\ 1110\ 0010\ 1100 = 81E2C_{16}$<br />
<br />
You can convert between base 8 and 16 by expressing the number in base 2 (easy to do!) and then converting that number from base 2 (another easy operation)! This is shown below in Sample Problem #1.<br />
<br />
== Using Hexadecimal Numbers to Represent Colors ==<br />
<br />
Computers use hexadecimal numbers to represent various colors in computer graphics because all computer screens use combinations of red, green, and blue light or RGB to represent thousands of different colors. Two digits are used for each so the hexadecimal number “#FF0000” represents the color red, “#00FF00” represents green, and “#0000FF” represents blue. The color black is “#000000” and white is “#FFFFFF”. <br />
<br />
The hash tag or number sign is used to denote a hexadecimal number. $FF_{16} = F (15) × 16 + F (15) × 1 = 240 + 15 = 255_{10}$ so there are 0 to 255 or 256 different shades of each color or $256^{3} = 16,777,216$ different colors. <br />
<br />
The following web site has nearly every color name, along with its hex code and decimal values: <br />
<br />
:https://www.rapidtables.com/web/color/RGB_Color.html <br />
<br />
For example “salmon” is “#FA8072” which represents the decimal numbers 250 (hex FA), 128 (hex 80), and 114 (hex 72).<br />
<br />
== Resources ==<br />
<br />
[https://ryanstutorials.net/ Ryan's Tutorials] covers this topic beautifully. Rather than trying to duplicate that work, we'll point you to the different sections:<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/ 1. Number Systems] - An introduction to what numbers systems are all about, with emphasis on decimal, binary, octal, and hexadecimal. ACSL will typically identify the base of a number using a subscript. For example, $123_8$ is an octal number, whereas $123_{16}$ is a hexadecimal number. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-conversions.php 2. Binary Conversions] - This section shows how to convert between binary, decimal, hexadecimal and octal numbers. In the [https://ryanstutorials.net/binary-tutorial/binary-conversions.php#activities ''Activities''] section, you can practice converting numbers. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-arithmetic.php 3. Binary Arithmetic] - Describes how to perform various arithmetic operations (addition, subtraction, multiplication, and division) with binary numbers. ACSL problems will also cover basic arithmetic in other bases, such as adding and subtracting together 2 hexadecimal numbers. ACSL problems will not cover division in other bases.<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php 4. Negative Numbers] - ACSL problems will not cover how negative numbers are represented in binary. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-floating-point.php 5. Binary Fractions and Floating Point] - The first part of this section is relevant to ACSL: fractions in other bases. ACSL will not cover floating point numbers in other basis. So, focus on the section [https://ryanstutorials.net/binary-tutorial/binary-floating-point.php#convertfraction Converting to a Binary Fraction], but keep in mind that ACSL problems may also cover octal and hexadecimal fractions.<br />
<br />
The [https://coolconversion.com/math/binary-octal-hexa-decimal/ CoolConversion.com online calculator] is another online app for practicing conversion from/to decimal, hexadecimal, octal and binary; this tool shows the steps that one goes through in the conversion. <br />
<br />
The AskNumbers.com site has a nice description of the conversions between [https://www.asknumbers.com/hex-to-octal.aspx binary, octal, decimal and hexadecimal] numbers. <br />
<br />
== Format of ACSL Problems ==<br />
<br />
The problems in this category will focus on converting between binary, octal, decimal, and hexadecimal, basic arithmetic of numbers in those bases, and, occasionally, fractions in those bases.<br />
<br />
To be successful in this category, you must know the following facts cold:<br />
<br />
# The binary value of each octal digit 0, 1, ..., 7<br />
# The binary value of each hex digit 0, 1, ..., 9, A, B, C, D, E, F<br />
# The decimal value of each hex digit 0, 1, ..., F <br />
# Powers of 2, up to 4096<br />
# Powers of 8, up to 4096<br />
# Powers of 16, up to 65,536<br />
<br />
== Sample Problems ==<br />
<br />
=== Sample Problem 1 ===<br />
<br />
Solve for $x$ where $x_{16}=3676_8$.<br />
<br />
'''Solution:''' One method of solution is to convert $3676_8$ into base 10, and then convert that number into base 16 to yield the value of $x$.<br />
<br />
An easier solution, less prone to arithmetic mistakes, is to convert from octal (base 8) to hexadecimal (base 16) through the binary (base 2) representation of the number:<br />
<br />
$$\begin{align}<br />
3676_8 &= 011 ~ 110 ~ 111 ~ 110_2 & \text{convert each octal digit into base 2}\hfill\cr <br />
&= 0111 ~ 1011 ~ 1110_2 & \text{group by 4 bits, from right-to-left}\hfill\cr<br />
&= 7 ~ \text{B} ~ \text{E}_{16} & \text{convert each group of 4 bits into a hex digit}\cr<br />
\end{align}$$<br />
<br />
=== Sample Problem 2 ===<br />
<br />
Solve for $x$ in the following hexadecimal equation: $ x= \text{F5AD}_{16} - \text{69EB}_{16}$<br />
<br />
'''Solution:''' One could convert the hex numbers into base 10, perform the subtraction, and then convert the answer back to base 16. However, <br />
working directly in base 16 isn't too hard. <br />
As in conventional decimal arithmetic, one works from right-to-left, from the least significant digits to the most. <br />
<br />
:The rightmost digit becomes 2, because D-B=2. <br />
:The next column is A-E. We need to ''borrow'' a one from the 5 column, and 1A-E=C <br />
:In the next column, 4-9=B, again, borrowing a 1 from the next column.<br />
:Finally, the leftmost column, E-6=8<br />
<br />
Combining these results of each column, we get a final answer of $8BC2_{16}$.<br />
<br />
=== Sample Problem 3 ===<br />
<br />
How many numbers from 100 to 200 in base 10 consist of distinct<br />
ascending digits and also have distinct ascending hex digits when<br />
converted to base 16? <br />
<br />
'''Solution:''' There are 13 numbers that have ascending digits in both bases from 100<br />
to 200. They are (in base 10):<br />
123 (7B), 124, 125, 126, 127 (7F), 137 (89), 138, 139 (8B), 156 (9C), 157, 158, 159 (9F), 189 (BD)<br />
<br />
== Video Resources ==<br />
<br />
There are many YouTube videos about computer number systems. Here are a handful that cover the topic nicely, without too many ads:<br />
<br />
{|<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/aW3qCcH6Dao</youtube><br />
| [https://youtu.be/aW3qCcH6Dao ''Number Systems - Converting Decimal, Binary and Hexadecimal'' ('''Joe James''')]<br />
<br />
An introduction to number systems, and how to convert between decimal, binary and hexadecimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/m1JtWKuTLR0</youtube><br />
| [https://youtu.be/m1JtWKuTLR0 ''Lesson 2.3 : Hexadecimal Tutorial'' ('''Carl Herold''')]<br />
<br />
The video focuses on hexadecimal numbers: their relationship to binary numbers and how to convert to decimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/WR67syBDzew</youtube><br />
| [https://youtu.be/WR67syBDzew ''Hexes and the Magic of Base 16 - Vaidehi Joshi - May 2017' ('''DonutJS''')]<br />
<br />
A fun introduction to hexadecimal numbers, focusing a bit <br />
on using hex numbers for specifying RGB colors. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jvx-NrILgpE</youtube><br />
| [https://youtu.be/jvx-NrILgpE ''Collins Lab: Binary & Hex'' ('''Adafruit Industries''')]<br />
<br />
A professionally produced video that explains the number systems, how and why binary numbers are fundamental to computer science, and why hexadecimal is important to computer programmers. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Karnaugh_Maps&diff=831Karnaugh Maps2020-09-02T16:31:07Z<p>Marc: Marc moved page Karnaugh Map to Karnaugh Maps</p>
<hr />
<div>= Introduction =<br />
Logic design implies the analysis, synthesis, minimization, and implementation of binary functions.<br />
Combinational logic refers to networks whose output is strictly dependent on the inputs. The analysis of such networks requires first the writing of the Boolean algebraic equation representative of the network, and then the complete characterization of the output as a result of all the possible combinations of the inputs.<br />
Minimization involves reducing the Boolean algebraic expression to some minimal form. <br /><br />
Any minimization tool in Boolean is based on the algebraic theorems. Algebraic reduction of Boolean functions is not easy and requires considerable experience, judgement and luck. It becomes more apparent as the complexity of the function increases.<br />
<br />
For example the two input variable expression as $ A \overline{B} + \overline{A} \overline{B}$ is obviously dependent only on $ \overline{B} $. But it is not so obvious that the following three input expression $ A \overline{B} + \overline{B} C + \overline{A} C $ is represented by $ A \overline{B} + \overline{A} C $ . <br /><br />
<br />
As a result extensive effort has been devoted toward developing techniques, aids or tools, that will allow the logic designer to minimize a function. The Venn diagram, Veitch diagram, Karnaugh map, Quine-McCluskey method, and other techniques have all been developed with but one objective-to allow the designed to arrive at a minimal expression as rapidly as possible with the least amount of effort.<br />
<br /><br />
<br />
Among these methods the '''Karnaugh Maps''' (made by G. Karnaugh in 1953) will be presented bellow.<br />
<br />
The Karnaugh map is an orderly arrangement of squares with assignments such that the difference between any two adjacent squares is a one-variable change. The map must contain a square or cell for each unique combination of input variables. A map for two variables A and B must have four cells, because there are 2<sup>2</sup> different combinations of two input variables. A map for three variables A, B and C must contain 2<sup>3</sup> or eight cells, and a map on n variables must contain 2<sup>n</sup> cells. An assignment of 1 for uncomplemented variable and 0 for a complemented variable is made. For instance, a term $ A \overline{B} C $ is equivalent to the binary notations 101. The table bellow shows the ways to define each cell of two, three and four variables functions. <br/><br />
The alphabetical input variables are identified at the upper left-hand corner of the box. For example, for three input variable function, the diagonal lines indicates that the variables A and B are represented by the binary notations across the top of the matrix (which is a Gray Code with the unique property of only a single bit change when going from one state to the next) and are contained in the vertical column below each assignment. The variable C is designed down the side of the matrix and is represented by horizontal rows within the matrix. Each cell represents a unique combination of the variables. <br />
<br />
The '''Karnaugh map''' technique is thought to be the most valuable tool available for dealing with Boolean functions. It provides instant recognition of the basic patterns, can be used to obtain all possible combinations and minimal terms, and is easily applied to all varieties of complex problems. Minimization with the map is accomplished through recognition of basic patterns.<br /><br />
The appearance of 1’s in adjacent cells immediately identifies the presence of a redundant variable. <br /><br />
The following table illustrates some examples of minimizing with two, three, four and five-variable map.<sup>[1]</sup><br />
<br /><br />
<br />
This table illustrates the grouping of one, two, four, eight and sixteen cells, depending on the number of inputs. For example, in a four-variable map, the grouping of two cells eliminates one variable, the grouping of four cells eliminates two variables, and the grouping of eight cells eliminates three variables. The rule is the same for different number of input variables. <br/><br />
Any given grouping of 1’s in the Karnaugh map is identified by a product form. <br/><br />
Minimization involves the gathering of the various groups in the most efficient manner. The mapping of a sum of product terms is accomplished by inserting a 1 in each cell of a map containing a particular product term. <br/><br />
The advantage of the map is that it gives a clear and complete picture of the function. It can immediately be determined if the reduction is unique or if there exist other minimal expressions.<br /><br />
<br />
<br />
<br />
= Karnaugh Map examples =<br />
{| class="wikitable"<br />
|-<br />
|<br />
[[File:T21.jpg|152px]]<br />
|| <br />
[[File:T22.jpg|152px]]<br />
||<br />
[[File:T23.jpg|152px]]<br />
|| <br />
[[File:T24.jpg|152px]]<br />
|-<br />
|-<br />
|<br />
[[File:T31.jpg|256px]]<br />
|| <br />
[[File:T32.jpg|256px]]<br />
||<br />
[[File:T33.jpg|256px]]<br />
|| <br />
[[File:T34.jpg|256px]]<br />
|-<br />
|-<br />
|<br />
[[File:T35.jpg|256px]]<br />
|| <br />
[[File:T36.jpg|256px]]<br />
||<br />
[[File:T37.jpg|256px]]<br />
|| <br />
[[File:T38.jpg|256px]]<br />
|-<br />
|-<br />
|<br />
[[File:T41.jpg|256px]]<br />
|| <br />
[[File:T42.jpg|256px]]<br />
||<br />
[[File:T43.jpg|256px]]<br />
|| <br />
[[File:T44.jpg|256px]]<br />
|-<br />
|-<br />
|<br />
[[File:T45.jpg|256px]]<br />
|| <br />
[[File:T47.jpg|256px]]<br />
||<br />
[[File:T48.jpg|256px]]<br />
|| <br />
[[File:T49.jpg|256px]]<br />
|-<br />
|-<br />
|<br />
[[File:T410.jpg|256px]]<br />
|| <br />
[[File:T411.jpg|256px]]<br />
||<br />
[[File:T412.jpg|256px]]<br />
|| <br />
[[File:T413.jpg|256px]]<br />
|-<br />
|}<br />
{| class="wikitable"<br />
|-<br />
|width="525pt"|[[File:T71.jpg|469px]] ||width="525pt"| [[File:T72.jpg|469px]]<br />
|-<br />
|width="525pt"|[[File:T73.jpg|469px]] ||width="525pt"| [[File:T75.jpg|469px]] <br />
|}<br />
<br />
''' RULES SUMMARY'''<br /><br />
<br />
No zeros allowed.<br /><br />
<br />
No diagonals.<br /><br />
<br />
Only power of 2 number of cells in each group.<br /><br />
<br />
Groups should be as large as possible.<br /><br />
<br />
Every one must be in at least one group.<br /><br />
<br />
Overlapping allowed.<br /><br />
<br />
Wrap around allowed.<br /><br />
<br />
Fewest number of groups possible.<br /><br />
<br /><br />
= Sample problems =<br />
== Sample problem 1 ==<br />
''' Karnaugh Map to minimize a digital circuit '''<br />
{| class="wikitable"<br />
|-<br />
! Initial circuit !! Karnaugh Map !! AND/OR circuit<br />
|-<br />
<br />
|width="425pt"|<center> [[File:C2IN1.jpg|380px]]</center> ||width="288pt"| [[File:T2IN1.jpg|160px]] ||width="380pt"| <center>[[File:C2OUT1.jpg|190px]]</center><br />
|}<br />
== Sample problem 2 ==<br />
''' Karnaugh Map to minimize a digital circuit '''<br />
{| class="wikitable"<br />
|-<br />
! Initial circuit !! Karnaugh Map !! AND/OR circuit<br />
|-<br />
<br />
|| [[File:CIR6_A.jpg|420px]] || [[File:T6n.jpg|290px]] || [[File:c6b.jpg|380px]]<br />
|}<br />
<br />
== Sample problem 3 ==<br />
''' Karnaugh Map to minimize a digital circuit '''<br />
{| class="wikitable"<br />
|-<br />
! Initial circuit !! Karnaugh Map !! AND/OR circuit<br />
|-<br />
<br />
|| [[File:C4IN1.jpg|460px]] || [[File:T4IN1N.jpg|286px]] ||width="346pt"| <center> [[File:C4OUT1.jpg|230px]]</center><br />
|}<br />
<br />
== Sample problem 4 ==<br />
''' Simplify the following Boolean three variable equation using Karnaugh map. '''<br />
{| class="wikitable"<br />
|-<br />
! Initial function !! Karnaugh Map !! Minimal function<br />
|-<br />
<br />
|| $ f = \overline{A} \overline{B} \overline{C} + A B \overline{C} +A \overline{B} + \overline{A} \overline{C} $ || [[File:TEX4.jpg|286px]] || $ f = \overline{C} + A \overline{B} $ <br />
|}<br />
<br />
== Sample problem 5 ==<br />
''' Simplify the following Boolean four variable equation using Karnaugh map. '''<br />
{| class="wikitable"<br />
|-<br />
! Initial function !! Karnaugh Map !! Minimal function<br />
|-<br />
<br />
|| $ f = A \overline{B} \overline{D} + A B \overline{C} \overline{D} + A B C + B \overline{C} D + \overline{A} B C D $ || [[File:TEX5.jpg|286px]] || $ f = B D + A \overline{D} $ <br />
|}<br />
1. William E. Wickes, Logic Design with Integrated Circuits, John Willey & Sons, Inc, New York-London-Sydney</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Karnaugh_Map&diff=832Karnaugh Map2020-09-02T16:31:07Z<p>Marc: Marc moved page Karnaugh Map to Karnaugh Maps</p>
<hr />
<div>#REDIRECT [[Karnaugh Maps]]</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Computer_Number_Systems&diff=830Computer Number Systems2020-09-02T16:19:31Z<p>Marc: /* Decimal Values */</p>
<hr />
<div>All digital computers, from supercomputers to your smartphone, are electronic devices and ultimately can do one thing: detect whether an electrical signal is on or off. That basic information, called a ''bit'' ('''bi'''nary digi'''t'''), has two values: a 1 (or ''true'') when the signal is on, and a 0 (of ''false'') when the signal is off. <br />
<br />
Larger values can be stored by a group of bits. For example, there are 4 different values stored by 2 bits (00, 01, 10, and 11), 8 values for 3 bits (000, 001, 010, 011, 100, 101, 110, and 111), 16 values for 4 bits (0000, 0001, ..., 1111), and so on. However, large numbers, using 0s and 1s only, are quite unwieldy for humans. For example, a computer would need 19 bits to store the numbers up to 500,000! We use of different number systems to work with large binary strings that represent numbers within computers.<br />
<br />
== Different Number Systems ==<br />
<br />
A ''number system'' is the way we name and represent numbers. The number system we use in our daily life is known as '''decimal number system''' or '''base 10''' because it is based on 10 different digits: 0, 1, 2, ..., 8, and 9. The base of number is written as subscript to a number. For example, the decimal number "twelve thousand three hundred and forty-five" is written as $12345_{10}$. Without a base, it's assumed that a number is in base 10. <br />
<br />
In computer science, apart from the decimal system, three additional number systems are commonly used: '''binary''' (base-2), '''octal''' (base-8), and '''hexadecimal''' or just '''hex''' (base-16). Binary numbers are important because that is how number are stored in the computer. Octal and hexadecimal are used to represent binary numbers is a user-friendly way. Each octal symbol represents 3 binary bits and each hex digit represents 4 binary bits. For example, the decimal number 53201, stored as in the computer as the binary number 10000001111000101100, is represented by the octal number 2017054, and the hex number 81E2C. The table below compares the number systems: <br />
:{| class="wikitable"<br />
!Number System<br />
!Base<br />
!Digits Used<br />
!Examples<br />
|-<br />
|Binary<br />
|2<br />
|0,1<br />
|$10110_{2}$, $10110011_{2}$<br />
|-<br />
|Octal<br />
|8<br />
|0,1,2,3,4,5,6,7<br />
|$75021_{8}$, $231_{8}$, $60012_{8}$<br />
|-<br />
|Decimal<br />
|10<br />
|0,1,2,3,4,5,6,7,8,9<br />
|$97425_{10}$ or simply 97425<br />
|-<br />
|Hexadecimal<br />
|16<br />
|0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F<br />
|$54A2DD0F_{16}$<br />
|}<br />
<br />
<br />
In general, $N$ bits have $2^N$ different values. The computers aboard the Apollo spacecraft had 8-bit words of memory. Each word of memory could store 256 different values, and the contents were displayed using 2 hex characters.<br />
<br />
The following table shows the first 20 numbers in decimal, binary, octal, and hexadecimal:<br />
:{| class="wikitable" style="text-align:center"<br />
!Decimal<br />
!Binary<br />
!Octal<br />
!Hexadecimal<br />
|-<br />
|0<br />
|0<br />
|0<br />
|0 <br />
|-<br />
|1<br />
|1<br />
|1<br />
|1 <br />
|-<br />
|2<br />
|10<br />
|2<br />
|2 <br />
|-<br />
|3<br />
|11<br />
|3<br />
|3 <br />
|-<br />
|4<br />
|100<br />
|4<br />
|4 <br />
|-<br />
|5<br />
|101<br />
|5<br />
|5 <br />
|-<br />
|6<br />
|110<br />
|6<br />
|6 <br />
|-<br />
|7<br />
|111<br />
|7<br />
|7 <br />
|-<br />
|8<br />
|1000<br />
|10<br />
|8<br />
|-<br />
|9<br />
|1001<br />
|11<br />
|9<br />
|-<br />
|10<br />
|1010<br />
|12<br />
|A<br />
|-<br />
|11<br />
|1011<br />
|13<br />
|B<br />
|-<br />
|12<br />
|1100<br />
|14<br />
|C <br />
|-<br />
|13<br />
|1101<br />
|15<br />
|D <br />
|-<br />
|14<br />
|1110<br />
|16<br />
|E <br />
|-<br />
|15<br />
|1111<br />
|17<br />
|F <br />
|-<br />
|16<br />
|10000<br />
|20<br />
|10 <br />
|-<br />
|17<br />
|10001<br />
|21<br />
|11<br />
|-<br />
|18<br />
|10010<br />
|22<br />
|12<br />
|-<br />
|19<br />
|10011<br />
|23<br />
|13<br />
|-<br />
|20<br />
|10100<br />
|24<br />
|14<br />
|}<br />
<br />
== Decimal Values ==<br />
<br />
The decimal value of a number in an arbitrary base is the sum of each digit multiplied by its place value. Here are some examples of converting from one base into base 10:<br />
<br />
:$12345 = 1×10000 + 2×1000 + 3×100 + 4×10 + 5×1 = 1×10^{4}+ 2×10^{3}+ 3×10^{2}+ 4×10^{1}+ 5×10{0}.$<br />
<br />
:$1101_{2} = 1 × 2^3 + 1 × 2^2 + 0 × 2^1 + 1 × 2^0 = 8 + 4 + 0 + 1 = 13_{10}$ <br />
<br />
:$175_{8} = 1 × 8^2 + 7 × 8^1 + 5 × 8^0 = 1 × 64 + 7 × 8 + 5 × 1 = 64 + 56 + 5 = 125_{10}$. <br />
<br />
:$A5E_{16} = 10 × 16^2 + 5 × 16^1 + 14 × 16^0 = 10 × 256 + 5 × 16 + 14 × 1 = 2560 + 80 + 14 = 2654_{10}$.<br />
<br />
== Converting between Binary, Octal, and Hexadecimal == <br />
<br />
Converting from octal to base 2 is simple: replace each octal digit by its corresponding 3 binary bits. For example:<br />
<br />
:$375_{8} = \ \ 011\ \ \ 111\ \ \ 101_{2} = 11111101_2$<br />
<br />
Converting from hex to base is also simple: replace each hex digit by its corresponding 4 binary bits. For example:<br />
<br />
:$FD_{16} = \ \ 1111\ \ \ 1101_{2} = 11111101_2$<br />
<br />
Converting from binary to either octal or hex is pretty simple as well: group the bit by 3s or 4s (starting at the right), and convert each group:<br />
<br />
:$10000001111000101100 = 10\ 000\ 001\ 111\ 000\ 101 \ 100 = 2017054_8$<br />
<br />
:$10000001111000101100 = 1000\ 0001\ 1110\ 0010\ 1100 = 81E2C_{16}$<br />
<br />
You can convert between base 8 and 16 by expressing the number in base 2 (easy to do!) and then converting that number from base 2 (another easy operation)! This is shown below in Sample Problem #1.<br />
<br />
== Using Hexadecimal Numbers to Represent Colors ==<br />
<br />
Computers use hexadecimal numbers to represent various colors in computer graphics because all computer screens use combinations of red, green, and blue light or RGB to represent thousands of different colors. Two digits are used for each so the hexadecimal number “#FF0000” represents the color red, “#00FF00” represents green, and “#0000FF” represents blue. The color black is “#000000” and white is “#FFFFFF”. <br />
<br />
The hash tag or number sign is used to denote a hexadecimal number. $FF_{16} = F (15) × 16 + F (15) × 1 = 240 + 15 = 255_{10}$ so there are 0 to 255 or 256 different shades of each color or $256^{3} = 16,777,216$ different colors. <br />
<br />
The following web site has nearly every color name, along with its hex code and decimal values: <br />
<br />
:https://www.rapidtables.com/web/color/RGB_Color.html <br />
<br />
For example “salmon” is “#FA8072” which represents the decimal numbers 250 (hex FA), 128 (hex 80), and 114 (hex 72).<br />
<br />
== Resources ==<br />
<br />
[https://ryanstutorials.net/ Ryan's Tutorials] covers this topic beautifully. Rather than trying to duplicate that work, we'll point you to the different sections:<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/ 1. Number Systems] - An introduction to what numbers systems are all about, with emphasis on decimal, binary, octal, and hexadecimal. ACSL will typically identify the base of a number using a subscript. For example, $123_8$ is an octal number, whereas $123_{16}$ is a hexadecimal number. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-conversions.php 2. Binary Conversions] - This section shows how to convert between binary, decimal, hexadecimal and octal numbers. In the [https://ryanstutorials.net/binary-tutorial/binary-conversions.php#activities ''Activities''] section, you can practice converting numbers. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-arithmetic.php 3. Binary Arithmetic] - Describes how to perform various arithmetic operations (addition, subtraction, multiplication, and division) with binary numbers. ACSL problems will also cover basic arithmetic in other bases, such as adding and subtracting together 2 hexadecimal numbers. ACSL problems will not cover division in other bases.<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php 4. Negative Numbers] - ACSL problems will not cover how negative numbers are represented in binary. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-floating-point.php 5. Binary Fractions and Floating Point] - The first part of this section is relevant to ACSL: fractions in other bases. ACSL will not cover floating point numbers in other basis. So, focus on the section [https://ryanstutorials.net/binary-tutorial/binary-floating-point.php#convertfraction Converting to a Binary Fraction], but keep in mind that ACSL problems may also cover octal and hexadecimal fractions.<br />
<br />
The [https://coolconversion.com/math/binary-octal-hexa-decimal/ CoolConversion.com online calculator] is another online app for practicing conversion from/to decimal, hexadecimal, octal and binary; this tool shows the steps that one goes through in the conversion. <br />
<br />
The AskNumbers.com site has a nice description of the conversions between [https://www.asknumbers.com/hex-to-octal.aspx binary, octal, decimal and hexadecimal] numbers. <br />
<br />
== Format of ACSL Problems ==<br />
<br />
The problems in this category will focus on converting between binary, octal, decimal, and hexadecimal, basic arithmetic of numbers in those bases, and, occasionally, fractions in those bases.<br />
<br />
To be successful in this category, you must know the following facts cold:<br />
<br />
# The binary value of each octal digit 0, 1, ..., 7<br />
# The binary value of each hex digit 0, 1, ..., 9, A, B, C, D, E, F<br />
# The decimal value of each hex digit 0, 1, ..., F <br />
# Powers of 2, up to 4096<br />
# Powers of 8, up to 4096<br />
# Powers of 16, up to 65,536<br />
<br />
== Sample Problems ==<br />
<br />
=== Sample Problem 1 ===<br />
<br />
Solve for $x$ where $x_{16}=3676_8$.<br />
<br />
'''Solution:''' One method of solution is to convert $3676_8$ into base 10, and then convert that number into base 16 to yield the value of $x$.<br />
<br />
An easier solution, less prone to arithmetic mistakes, is to convert from octal (base 8) to hexadecimal (base 16) through the binary (base 2) representation of the number:<br />
<br />
$$\begin{align}<br />
3676_8 &= 011 ~ 110 ~ 111 ~ 110_2 & \text{convert each octal digit into base 2}\hfill\cr <br />
&= 0111 ~ 1011 ~ 1110_2 & \text{group by 4 bits, from right-to-left}\hfill\cr<br />
&= 7 ~ \text{B} ~ \text{E}_{16} & \text{convert each group of 4 bits into a hex digit}\cr<br />
\end{align}$$<br />
<br />
=== Sample Problem 2 ===<br />
<br />
Solve for $x$ in the following hexadecimal equation: $ x= \text{F5AD}_{16} - \text{69EB}_{16}$<br />
<br />
'''Solution:''' One could convert the hex numbers into base 10, perform the subtraction, and then convert the answer back to base 16. However, <br />
working directly in base 16 isn't too hard. <br />
As in conventional decimal arithmetic, one works from right-to-left, from the least significant digits to the most. <br />
<br />
:The rightmost digit becomes 2, because D-B=2. <br />
:The next column is A-E. We need to ''borrow'' a one from the 5 column, and 1A-E=C <br />
:In the next column, 4-9=B, again, borrowing a 1 from the next column.<br />
:Finally, the leftmost column, E-6=8<br />
<br />
Combining these results of each column, we get a final answer of $8BC2_{16}$.<br />
<br />
=== Sample Problem 3 ===<br />
<br />
How many numbers from 100 to 200 in base 10 consist of distinct<br />
ascending digits and also have distinct ascending hex digits when<br />
converted to base 16? <br />
<br />
'''Solution:''' There are 13 numbers that have ascending digits in both bases from 100<br />
to 200. They are (in base 10):<br />
123 (7B), 124, 125, 126, 127 (7F), 137 (89), 138, 139 (8B), 156 (9C), 157, 158, 159 (9F), 189 (BD)<br />
<br />
== Video Resources ==<br />
<br />
There are many YouTube videos about computer number systems. Here are a handful that cover the topic nicely, without too many ads:<br />
<br />
{|<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/aW3qCcH6Dao</youtube><br />
| [https://youtu.be/aW3qCcH6Dao ''Number Systems - Converting Decimal, Binary and Hexadecimal'' ('''Joe James''')]<br />
<br />
An introduction to number systems, and how to convert between decimal, binary and hexadecimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/m1JtWKuTLR0</youtube><br />
| [https://youtu.be/m1JtWKuTLR0 ''Lesson 2.3 : Hexadecimal Tutorial'' ('''Carl Herold''')]<br />
<br />
The video focuses on hexadecimal numbers: their relationship to binary numbers and how to convert to decimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/WR67syBDzew</youtube><br />
| [https://youtu.be/WR67syBDzew ''Hexes and the Magic of Base 16 - Vaidehi Joshi - May 2017' ('''DonutJS''')]<br />
<br />
A fun introduction to hexadecimal numbers, focusing a bit <br />
on using hex numbers for specifying RGB colors. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jvx-NrILgpE</youtube><br />
| [https://youtu.be/jvx-NrILgpE ''Collins Lab: Binary & Hex'' ('''Adafruit Industries''')]<br />
<br />
A professionally produced video that explains the number systems, how and why binary numbers are fundamental to computer science, and why hexadecimal is important to computer programmers. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Computer_Number_Systems&diff=829Computer Number Systems2020-09-02T16:18:44Z<p>Marc: /* Converting between Binary, Octal, and Hexadecimal */</p>
<hr />
<div>All digital computers, from supercomputers to your smartphone, are electronic devices and ultimately can do one thing: detect whether an electrical signal is on or off. That basic information, called a ''bit'' ('''bi'''nary digi'''t'''), has two values: a 1 (or ''true'') when the signal is on, and a 0 (of ''false'') when the signal is off. <br />
<br />
Larger values can be stored by a group of bits. For example, there are 4 different values stored by 2 bits (00, 01, 10, and 11), 8 values for 3 bits (000, 001, 010, 011, 100, 101, 110, and 111), 16 values for 4 bits (0000, 0001, ..., 1111), and so on. However, large numbers, using 0s and 1s only, are quite unwieldy for humans. For example, a computer would need 19 bits to store the numbers up to 500,000! We use of different number systems to work with large binary strings that represent numbers within computers.<br />
<br />
== Different Number Systems ==<br />
<br />
A ''number system'' is the way we name and represent numbers. The number system we use in our daily life is known as '''decimal number system''' or '''base 10''' because it is based on 10 different digits: 0, 1, 2, ..., 8, and 9. The base of number is written as subscript to a number. For example, the decimal number "twelve thousand three hundred and forty-five" is written as $12345_{10}$. Without a base, it's assumed that a number is in base 10. <br />
<br />
In computer science, apart from the decimal system, three additional number systems are commonly used: '''binary''' (base-2), '''octal''' (base-8), and '''hexadecimal''' or just '''hex''' (base-16). Binary numbers are important because that is how number are stored in the computer. Octal and hexadecimal are used to represent binary numbers is a user-friendly way. Each octal symbol represents 3 binary bits and each hex digit represents 4 binary bits. For example, the decimal number 53201, stored as in the computer as the binary number 10000001111000101100, is represented by the octal number 2017054, and the hex number 81E2C. The table below compares the number systems: <br />
:{| class="wikitable"<br />
!Number System<br />
!Base<br />
!Digits Used<br />
!Examples<br />
|-<br />
|Binary<br />
|2<br />
|0,1<br />
|$10110_{2}$, $10110011_{2}$<br />
|-<br />
|Octal<br />
|8<br />
|0,1,2,3,4,5,6,7<br />
|$75021_{8}$, $231_{8}$, $60012_{8}$<br />
|-<br />
|Decimal<br />
|10<br />
|0,1,2,3,4,5,6,7,8,9<br />
|$97425_{10}$ or simply 97425<br />
|-<br />
|Hexadecimal<br />
|16<br />
|0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F<br />
|$54A2DD0F_{16}$<br />
|}<br />
<br />
<br />
In general, $N$ bits have $2^N$ different values. The computers aboard the Apollo spacecraft had 8-bit words of memory. Each word of memory could store 256 different values, and the contents were displayed using 2 hex characters.<br />
<br />
The following table shows the first 20 numbers in decimal, binary, octal, and hexadecimal:<br />
:{| class="wikitable" style="text-align:center"<br />
!Decimal<br />
!Binary<br />
!Octal<br />
!Hexadecimal<br />
|-<br />
|0<br />
|0<br />
|0<br />
|0 <br />
|-<br />
|1<br />
|1<br />
|1<br />
|1 <br />
|-<br />
|2<br />
|10<br />
|2<br />
|2 <br />
|-<br />
|3<br />
|11<br />
|3<br />
|3 <br />
|-<br />
|4<br />
|100<br />
|4<br />
|4 <br />
|-<br />
|5<br />
|101<br />
|5<br />
|5 <br />
|-<br />
|6<br />
|110<br />
|6<br />
|6 <br />
|-<br />
|7<br />
|111<br />
|7<br />
|7 <br />
|-<br />
|8<br />
|1000<br />
|10<br />
|8<br />
|-<br />
|9<br />
|1001<br />
|11<br />
|9<br />
|-<br />
|10<br />
|1010<br />
|12<br />
|A<br />
|-<br />
|11<br />
|1011<br />
|13<br />
|B<br />
|-<br />
|12<br />
|1100<br />
|14<br />
|C <br />
|-<br />
|13<br />
|1101<br />
|15<br />
|D <br />
|-<br />
|14<br />
|1110<br />
|16<br />
|E <br />
|-<br />
|15<br />
|1111<br />
|17<br />
|F <br />
|-<br />
|16<br />
|10000<br />
|20<br />
|10 <br />
|-<br />
|17<br />
|10001<br />
|21<br />
|11<br />
|-<br />
|18<br />
|10010<br />
|22<br />
|12<br />
|-<br />
|19<br />
|10011<br />
|23<br />
|13<br />
|-<br />
|20<br />
|10100<br />
|24<br />
|14<br />
|}<br />
<br />
== Decimal Values ==<br />
<br />
The value of a any number is the sum of each digit multiplied by its place value. Here are some examples of converting from one base into base 10:<br />
<br />
:$12345 = 1×10000 + 2×1000 + 3×100 + 4×10 + 5×1 = 1×10^{4}+ 2×10^{3}+ 3×10^{2}+ 4×10^{1}+ 5×10{0}.$<br />
<br />
:$1101_{2} = 1 × 2^3 + 1 × 2^2 + 0 × 2^1 + 1 × 2^0 = 8 + 4 + 0 + 1 = 13_{10}$ <br />
<br />
:$175_{8} = 1 × 8^2 + 7 × 8^1 + 5 × 8^0 = 1 × 64 + 7 × 8 + 5 × 1 = 64 + 56 + 5 = 125_{10}$. <br />
<br />
:$A5E_{16} = 10 × 16^2 + 5 × 16^1 + 14 × 16^0 = 10 × 256 + 5 × 16 + 14 × 1 = 2560 + 80 + 14 = 2654_{10}$.<br />
<br />
== Converting between Binary, Octal, and Hexadecimal == <br />
<br />
Converting from octal to base 2 is simple: replace each octal digit by its corresponding 3 binary bits. For example:<br />
<br />
:$375_{8} = \ \ 011\ \ \ 111\ \ \ 101_{2} = 11111101_2$<br />
<br />
Converting from hex to base is also simple: replace each hex digit by its corresponding 4 binary bits. For example:<br />
<br />
:$FD_{16} = \ \ 1111\ \ \ 1101_{2} = 11111101_2$<br />
<br />
Converting from binary to either octal or hex is pretty simple as well: group the bit by 3s or 4s (starting at the right), and convert each group:<br />
<br />
:$10000001111000101100 = 10\ 000\ 001\ 111\ 000\ 101 \ 100 = 2017054_8$<br />
<br />
:$10000001111000101100 = 1000\ 0001\ 1110\ 0010\ 1100 = 81E2C_{16}$<br />
<br />
You can convert between base 8 and 16 by expressing the number in base 2 (easy to do!) and then converting that number from base 2 (another easy operation)! This is shown below in Sample Problem #1.<br />
<br />
== Using Hexadecimal Numbers to Represent Colors ==<br />
<br />
Computers use hexadecimal numbers to represent various colors in computer graphics because all computer screens use combinations of red, green, and blue light or RGB to represent thousands of different colors. Two digits are used for each so the hexadecimal number “#FF0000” represents the color red, “#00FF00” represents green, and “#0000FF” represents blue. The color black is “#000000” and white is “#FFFFFF”. <br />
<br />
The hash tag or number sign is used to denote a hexadecimal number. $FF_{16} = F (15) × 16 + F (15) × 1 = 240 + 15 = 255_{10}$ so there are 0 to 255 or 256 different shades of each color or $256^{3} = 16,777,216$ different colors. <br />
<br />
The following web site has nearly every color name, along with its hex code and decimal values: <br />
<br />
:https://www.rapidtables.com/web/color/RGB_Color.html <br />
<br />
For example “salmon” is “#FA8072” which represents the decimal numbers 250 (hex FA), 128 (hex 80), and 114 (hex 72).<br />
<br />
== Resources ==<br />
<br />
[https://ryanstutorials.net/ Ryan's Tutorials] covers this topic beautifully. Rather than trying to duplicate that work, we'll point you to the different sections:<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/ 1. Number Systems] - An introduction to what numbers systems are all about, with emphasis on decimal, binary, octal, and hexadecimal. ACSL will typically identify the base of a number using a subscript. For example, $123_8$ is an octal number, whereas $123_{16}$ is a hexadecimal number. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-conversions.php 2. Binary Conversions] - This section shows how to convert between binary, decimal, hexadecimal and octal numbers. In the [https://ryanstutorials.net/binary-tutorial/binary-conversions.php#activities ''Activities''] section, you can practice converting numbers. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-arithmetic.php 3. Binary Arithmetic] - Describes how to perform various arithmetic operations (addition, subtraction, multiplication, and division) with binary numbers. ACSL problems will also cover basic arithmetic in other bases, such as adding and subtracting together 2 hexadecimal numbers. ACSL problems will not cover division in other bases.<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php 4. Negative Numbers] - ACSL problems will not cover how negative numbers are represented in binary. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-floating-point.php 5. Binary Fractions and Floating Point] - The first part of this section is relevant to ACSL: fractions in other bases. ACSL will not cover floating point numbers in other basis. So, focus on the section [https://ryanstutorials.net/binary-tutorial/binary-floating-point.php#convertfraction Converting to a Binary Fraction], but keep in mind that ACSL problems may also cover octal and hexadecimal fractions.<br />
<br />
The [https://coolconversion.com/math/binary-octal-hexa-decimal/ CoolConversion.com online calculator] is another online app for practicing conversion from/to decimal, hexadecimal, octal and binary; this tool shows the steps that one goes through in the conversion. <br />
<br />
The AskNumbers.com site has a nice description of the conversions between [https://www.asknumbers.com/hex-to-octal.aspx binary, octal, decimal and hexadecimal] numbers. <br />
<br />
== Format of ACSL Problems ==<br />
<br />
The problems in this category will focus on converting between binary, octal, decimal, and hexadecimal, basic arithmetic of numbers in those bases, and, occasionally, fractions in those bases.<br />
<br />
To be successful in this category, you must know the following facts cold:<br />
<br />
# The binary value of each octal digit 0, 1, ..., 7<br />
# The binary value of each hex digit 0, 1, ..., 9, A, B, C, D, E, F<br />
# The decimal value of each hex digit 0, 1, ..., F <br />
# Powers of 2, up to 4096<br />
# Powers of 8, up to 4096<br />
# Powers of 16, up to 65,536<br />
<br />
== Sample Problems ==<br />
<br />
=== Sample Problem 1 ===<br />
<br />
Solve for $x$ where $x_{16}=3676_8$.<br />
<br />
'''Solution:''' One method of solution is to convert $3676_8$ into base 10, and then convert that number into base 16 to yield the value of $x$.<br />
<br />
An easier solution, less prone to arithmetic mistakes, is to convert from octal (base 8) to hexadecimal (base 16) through the binary (base 2) representation of the number:<br />
<br />
$$\begin{align}<br />
3676_8 &= 011 ~ 110 ~ 111 ~ 110_2 & \text{convert each octal digit into base 2}\hfill\cr <br />
&= 0111 ~ 1011 ~ 1110_2 & \text{group by 4 bits, from right-to-left}\hfill\cr<br />
&= 7 ~ \text{B} ~ \text{E}_{16} & \text{convert each group of 4 bits into a hex digit}\cr<br />
\end{align}$$<br />
<br />
=== Sample Problem 2 ===<br />
<br />
Solve for $x$ in the following hexadecimal equation: $ x= \text{F5AD}_{16} - \text{69EB}_{16}$<br />
<br />
'''Solution:''' One could convert the hex numbers into base 10, perform the subtraction, and then convert the answer back to base 16. However, <br />
working directly in base 16 isn't too hard. <br />
As in conventional decimal arithmetic, one works from right-to-left, from the least significant digits to the most. <br />
<br />
:The rightmost digit becomes 2, because D-B=2. <br />
:The next column is A-E. We need to ''borrow'' a one from the 5 column, and 1A-E=C <br />
:In the next column, 4-9=B, again, borrowing a 1 from the next column.<br />
:Finally, the leftmost column, E-6=8<br />
<br />
Combining these results of each column, we get a final answer of $8BC2_{16}$.<br />
<br />
=== Sample Problem 3 ===<br />
<br />
How many numbers from 100 to 200 in base 10 consist of distinct<br />
ascending digits and also have distinct ascending hex digits when<br />
converted to base 16? <br />
<br />
'''Solution:''' There are 13 numbers that have ascending digits in both bases from 100<br />
to 200. They are (in base 10):<br />
123 (7B), 124, 125, 126, 127 (7F), 137 (89), 138, 139 (8B), 156 (9C), 157, 158, 159 (9F), 189 (BD)<br />
<br />
== Video Resources ==<br />
<br />
There are many YouTube videos about computer number systems. Here are a handful that cover the topic nicely, without too many ads:<br />
<br />
{|<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/aW3qCcH6Dao</youtube><br />
| [https://youtu.be/aW3qCcH6Dao ''Number Systems - Converting Decimal, Binary and Hexadecimal'' ('''Joe James''')]<br />
<br />
An introduction to number systems, and how to convert between decimal, binary and hexadecimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/m1JtWKuTLR0</youtube><br />
| [https://youtu.be/m1JtWKuTLR0 ''Lesson 2.3 : Hexadecimal Tutorial'' ('''Carl Herold''')]<br />
<br />
The video focuses on hexadecimal numbers: their relationship to binary numbers and how to convert to decimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/WR67syBDzew</youtube><br />
| [https://youtu.be/WR67syBDzew ''Hexes and the Magic of Base 16 - Vaidehi Joshi - May 2017' ('''DonutJS''')]<br />
<br />
A fun introduction to hexadecimal numbers, focusing a bit <br />
on using hex numbers for specifying RGB colors. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jvx-NrILgpE</youtube><br />
| [https://youtu.be/jvx-NrILgpE ''Collins Lab: Binary & Hex'' ('''Adafruit Industries''')]<br />
<br />
A professionally produced video that explains the number systems, how and why binary numbers are fundamental to computer science, and why hexadecimal is important to computer programmers. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Computer_Number_Systems&diff=828Computer Number Systems2020-09-02T16:16:34Z<p>Marc: /* Decimal Values */</p>
<hr />
<div>All digital computers, from supercomputers to your smartphone, are electronic devices and ultimately can do one thing: detect whether an electrical signal is on or off. That basic information, called a ''bit'' ('''bi'''nary digi'''t'''), has two values: a 1 (or ''true'') when the signal is on, and a 0 (of ''false'') when the signal is off. <br />
<br />
Larger values can be stored by a group of bits. For example, there are 4 different values stored by 2 bits (00, 01, 10, and 11), 8 values for 3 bits (000, 001, 010, 011, 100, 101, 110, and 111), 16 values for 4 bits (0000, 0001, ..., 1111), and so on. However, large numbers, using 0s and 1s only, are quite unwieldy for humans. For example, a computer would need 19 bits to store the numbers up to 500,000! We use of different number systems to work with large binary strings that represent numbers within computers.<br />
<br />
== Different Number Systems ==<br />
<br />
A ''number system'' is the way we name and represent numbers. The number system we use in our daily life is known as '''decimal number system''' or '''base 10''' because it is based on 10 different digits: 0, 1, 2, ..., 8, and 9. The base of number is written as subscript to a number. For example, the decimal number "twelve thousand three hundred and forty-five" is written as $12345_{10}$. Without a base, it's assumed that a number is in base 10. <br />
<br />
In computer science, apart from the decimal system, three additional number systems are commonly used: '''binary''' (base-2), '''octal''' (base-8), and '''hexadecimal''' or just '''hex''' (base-16). Binary numbers are important because that is how number are stored in the computer. Octal and hexadecimal are used to represent binary numbers is a user-friendly way. Each octal symbol represents 3 binary bits and each hex digit represents 4 binary bits. For example, the decimal number 53201, stored as in the computer as the binary number 10000001111000101100, is represented by the octal number 2017054, and the hex number 81E2C. The table below compares the number systems: <br />
:{| class="wikitable"<br />
!Number System<br />
!Base<br />
!Digits Used<br />
!Examples<br />
|-<br />
|Binary<br />
|2<br />
|0,1<br />
|$10110_{2}$, $10110011_{2}$<br />
|-<br />
|Octal<br />
|8<br />
|0,1,2,3,4,5,6,7<br />
|$75021_{8}$, $231_{8}$, $60012_{8}$<br />
|-<br />
|Decimal<br />
|10<br />
|0,1,2,3,4,5,6,7,8,9<br />
|$97425_{10}$ or simply 97425<br />
|-<br />
|Hexadecimal<br />
|16<br />
|0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F<br />
|$54A2DD0F_{16}$<br />
|}<br />
<br />
<br />
In general, $N$ bits have $2^N$ different values. The computers aboard the Apollo spacecraft had 8-bit words of memory. Each word of memory could store 256 different values, and the contents were displayed using 2 hex characters.<br />
<br />
The following table shows the first 20 numbers in decimal, binary, octal, and hexadecimal:<br />
:{| class="wikitable" style="text-align:center"<br />
!Decimal<br />
!Binary<br />
!Octal<br />
!Hexadecimal<br />
|-<br />
|0<br />
|0<br />
|0<br />
|0 <br />
|-<br />
|1<br />
|1<br />
|1<br />
|1 <br />
|-<br />
|2<br />
|10<br />
|2<br />
|2 <br />
|-<br />
|3<br />
|11<br />
|3<br />
|3 <br />
|-<br />
|4<br />
|100<br />
|4<br />
|4 <br />
|-<br />
|5<br />
|101<br />
|5<br />
|5 <br />
|-<br />
|6<br />
|110<br />
|6<br />
|6 <br />
|-<br />
|7<br />
|111<br />
|7<br />
|7 <br />
|-<br />
|8<br />
|1000<br />
|10<br />
|8<br />
|-<br />
|9<br />
|1001<br />
|11<br />
|9<br />
|-<br />
|10<br />
|1010<br />
|12<br />
|A<br />
|-<br />
|11<br />
|1011<br />
|13<br />
|B<br />
|-<br />
|12<br />
|1100<br />
|14<br />
|C <br />
|-<br />
|13<br />
|1101<br />
|15<br />
|D <br />
|-<br />
|14<br />
|1110<br />
|16<br />
|E <br />
|-<br />
|15<br />
|1111<br />
|17<br />
|F <br />
|-<br />
|16<br />
|10000<br />
|20<br />
|10 <br />
|-<br />
|17<br />
|10001<br />
|21<br />
|11<br />
|-<br />
|18<br />
|10010<br />
|22<br />
|12<br />
|-<br />
|19<br />
|10011<br />
|23<br />
|13<br />
|-<br />
|20<br />
|10100<br />
|24<br />
|14<br />
|}<br />
<br />
== Decimal Values ==<br />
<br />
The value of a any number is the sum of each digit multiplied by its place value. Here are some examples of converting from one base into base 10:<br />
<br />
:$12345 = 1×10000 + 2×1000 + 3×100 + 4×10 + 5×1 = 1×10^{4}+ 2×10^{3}+ 3×10^{2}+ 4×10^{1}+ 5×10{0}.$<br />
<br />
:$1101_{2} = 1 × 2^3 + 1 × 2^2 + 0 × 2^1 + 1 × 2^0 = 8 + 4 + 0 + 1 = 13_{10}$ <br />
<br />
:$175_{8} = 1 × 8^2 + 7 × 8^1 + 5 × 8^0 = 1 × 64 + 7 × 8 + 5 × 1 = 64 + 56 + 5 = 125_{10}$. <br />
<br />
:$A5E_{16} = 10 × 16^2 + 5 × 16^1 + 14 × 16^0 = 10 × 256 + 5 × 16 + 14 × 1 = 2560 + 80 + 14 = 2654_{10}$.<br />
<br />
== Converting between Binary, Octal, and Hexadecimal == <br />
<br />
Converting from octal to base 2 is simple: replace each octal digit by its corresponding 3 binary bits. For example:<br />
:$375_{8} = \ \ 011\ \ \ 111\ \ \ 101_{2}$<br />
Converting from hex to base is also simple: replace each hex digit by its corresponding 4 binary bits. For example:<br />
:$FD_{16} = \ \ 1111\ \ \ 1101_{2}$<br />
Converting from binary to either octal or hex is pretty simple as well: group the bit by 3s or 4s (starting at the right), and convert each group:<br />
:$10000001111000101100 = 10\ 000\ 001\ 111\ 000\ 101 \ 100 = 2017054_8$<br />
<br />
:$10000001111000101100 = 1000\ 0001\ 1110\ 0010\ 1100 = 81E2C_{16}$<br />
You can convert between base 8 and 16 by expressing the number in base 2 (easy to do!) and then converting that number from base 2 (another easy operation)! This is shown below in Sample Problem #1.<br />
<br />
== Using Hexadecimal Numbers to Represent Colors ==<br />
<br />
Computers use hexadecimal numbers to represent various colors in computer graphics because all computer screens use combinations of red, green, and blue light or RGB to represent thousands of different colors. Two digits are used for each so the hexadecimal number “#FF0000” represents the color red, “#00FF00” represents green, and “#0000FF” represents blue. The color black is “#000000” and white is “#FFFFFF”. <br />
<br />
The hash tag or number sign is used to denote a hexadecimal number. $FF_{16} = F (15) × 16 + F (15) × 1 = 240 + 15 = 255_{10}$ so there are 0 to 255 or 256 different shades of each color or $256^{3} = 16,777,216$ different colors. <br />
<br />
The following web site has nearly every color name, along with its hex code and decimal values: <br />
<br />
:https://www.rapidtables.com/web/color/RGB_Color.html <br />
<br />
For example “salmon” is “#FA8072” which represents the decimal numbers 250 (hex FA), 128 (hex 80), and 114 (hex 72).<br />
<br />
== Resources ==<br />
<br />
[https://ryanstutorials.net/ Ryan's Tutorials] covers this topic beautifully. Rather than trying to duplicate that work, we'll point you to the different sections:<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/ 1. Number Systems] - An introduction to what numbers systems are all about, with emphasis on decimal, binary, octal, and hexadecimal. ACSL will typically identify the base of a number using a subscript. For example, $123_8$ is an octal number, whereas $123_{16}$ is a hexadecimal number. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-conversions.php 2. Binary Conversions] - This section shows how to convert between binary, decimal, hexadecimal and octal numbers. In the [https://ryanstutorials.net/binary-tutorial/binary-conversions.php#activities ''Activities''] section, you can practice converting numbers. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-arithmetic.php 3. Binary Arithmetic] - Describes how to perform various arithmetic operations (addition, subtraction, multiplication, and division) with binary numbers. ACSL problems will also cover basic arithmetic in other bases, such as adding and subtracting together 2 hexadecimal numbers. ACSL problems will not cover division in other bases.<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php 4. Negative Numbers] - ACSL problems will not cover how negative numbers are represented in binary. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-floating-point.php 5. Binary Fractions and Floating Point] - The first part of this section is relevant to ACSL: fractions in other bases. ACSL will not cover floating point numbers in other basis. So, focus on the section [https://ryanstutorials.net/binary-tutorial/binary-floating-point.php#convertfraction Converting to a Binary Fraction], but keep in mind that ACSL problems may also cover octal and hexadecimal fractions.<br />
<br />
The [https://coolconversion.com/math/binary-octal-hexa-decimal/ CoolConversion.com online calculator] is another online app for practicing conversion from/to decimal, hexadecimal, octal and binary; this tool shows the steps that one goes through in the conversion. <br />
<br />
The AskNumbers.com site has a nice description of the conversions between [https://www.asknumbers.com/hex-to-octal.aspx binary, octal, decimal and hexadecimal] numbers. <br />
<br />
== Format of ACSL Problems ==<br />
<br />
The problems in this category will focus on converting between binary, octal, decimal, and hexadecimal, basic arithmetic of numbers in those bases, and, occasionally, fractions in those bases.<br />
<br />
To be successful in this category, you must know the following facts cold:<br />
<br />
# The binary value of each octal digit 0, 1, ..., 7<br />
# The binary value of each hex digit 0, 1, ..., 9, A, B, C, D, E, F<br />
# The decimal value of each hex digit 0, 1, ..., F <br />
# Powers of 2, up to 4096<br />
# Powers of 8, up to 4096<br />
# Powers of 16, up to 65,536<br />
<br />
== Sample Problems ==<br />
<br />
=== Sample Problem 1 ===<br />
<br />
Solve for $x$ where $x_{16}=3676_8$.<br />
<br />
'''Solution:''' One method of solution is to convert $3676_8$ into base 10, and then convert that number into base 16 to yield the value of $x$.<br />
<br />
An easier solution, less prone to arithmetic mistakes, is to convert from octal (base 8) to hexadecimal (base 16) through the binary (base 2) representation of the number:<br />
<br />
$$\begin{align}<br />
3676_8 &= 011 ~ 110 ~ 111 ~ 110_2 & \text{convert each octal digit into base 2}\hfill\cr <br />
&= 0111 ~ 1011 ~ 1110_2 & \text{group by 4 bits, from right-to-left}\hfill\cr<br />
&= 7 ~ \text{B} ~ \text{E}_{16} & \text{convert each group of 4 bits into a hex digit}\cr<br />
\end{align}$$<br />
<br />
=== Sample Problem 2 ===<br />
<br />
Solve for $x$ in the following hexadecimal equation: $ x= \text{F5AD}_{16} - \text{69EB}_{16}$<br />
<br />
'''Solution:''' One could convert the hex numbers into base 10, perform the subtraction, and then convert the answer back to base 16. However, <br />
working directly in base 16 isn't too hard. <br />
As in conventional decimal arithmetic, one works from right-to-left, from the least significant digits to the most. <br />
<br />
:The rightmost digit becomes 2, because D-B=2. <br />
:The next column is A-E. We need to ''borrow'' a one from the 5 column, and 1A-E=C <br />
:In the next column, 4-9=B, again, borrowing a 1 from the next column.<br />
:Finally, the leftmost column, E-6=8<br />
<br />
Combining these results of each column, we get a final answer of $8BC2_{16}$.<br />
<br />
=== Sample Problem 3 ===<br />
<br />
How many numbers from 100 to 200 in base 10 consist of distinct<br />
ascending digits and also have distinct ascending hex digits when<br />
converted to base 16? <br />
<br />
'''Solution:''' There are 13 numbers that have ascending digits in both bases from 100<br />
to 200. They are (in base 10):<br />
123 (7B), 124, 125, 126, 127 (7F), 137 (89), 138, 139 (8B), 156 (9C), 157, 158, 159 (9F), 189 (BD)<br />
<br />
== Video Resources ==<br />
<br />
There are many YouTube videos about computer number systems. Here are a handful that cover the topic nicely, without too many ads:<br />
<br />
{|<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/aW3qCcH6Dao</youtube><br />
| [https://youtu.be/aW3qCcH6Dao ''Number Systems - Converting Decimal, Binary and Hexadecimal'' ('''Joe James''')]<br />
<br />
An introduction to number systems, and how to convert between decimal, binary and hexadecimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/m1JtWKuTLR0</youtube><br />
| [https://youtu.be/m1JtWKuTLR0 ''Lesson 2.3 : Hexadecimal Tutorial'' ('''Carl Herold''')]<br />
<br />
The video focuses on hexadecimal numbers: their relationship to binary numbers and how to convert to decimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/WR67syBDzew</youtube><br />
| [https://youtu.be/WR67syBDzew ''Hexes and the Magic of Base 16 - Vaidehi Joshi - May 2017' ('''DonutJS''')]<br />
<br />
A fun introduction to hexadecimal numbers, focusing a bit <br />
on using hex numbers for specifying RGB colors. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jvx-NrILgpE</youtube><br />
| [https://youtu.be/jvx-NrILgpE ''Collins Lab: Binary & Hex'' ('''Adafruit Industries''')]<br />
<br />
A professionally produced video that explains the number systems, how and why binary numbers are fundamental to computer science, and why hexadecimal is important to computer programmers. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Computer_Number_Systems&diff=827Computer Number Systems2020-09-02T16:14:28Z<p>Marc: more wordsmithing</p>
<hr />
<div>All digital computers, from supercomputers to your smartphone, are electronic devices and ultimately can do one thing: detect whether an electrical signal is on or off. That basic information, called a ''bit'' ('''bi'''nary digi'''t'''), has two values: a 1 (or ''true'') when the signal is on, and a 0 (of ''false'') when the signal is off. <br />
<br />
Larger values can be stored by a group of bits. For example, there are 4 different values stored by 2 bits (00, 01, 10, and 11), 8 values for 3 bits (000, 001, 010, 011, 100, 101, 110, and 111), 16 values for 4 bits (0000, 0001, ..., 1111), and so on. However, large numbers, using 0s and 1s only, are quite unwieldy for humans. For example, a computer would need 19 bits to store the numbers up to 500,000! We use of different number systems to work with large binary strings that represent numbers within computers.<br />
<br />
== Different Number Systems ==<br />
<br />
A ''number system'' is the way we name and represent numbers. The number system we use in our daily life is known as '''decimal number system''' or '''base 10''' because it is based on 10 different digits: 0, 1, 2, ..., 8, and 9. The base of number is written as subscript to a number. For example, the decimal number "twelve thousand three hundred and forty-five" is written as $12345_{10}$. Without a base, it's assumed that a number is in base 10. <br />
<br />
In computer science, apart from the decimal system, three additional number systems are commonly used: '''binary''' (base-2), '''octal''' (base-8), and '''hexadecimal''' or just '''hex''' (base-16). Binary numbers are important because that is how number are stored in the computer. Octal and hexadecimal are used to represent binary numbers is a user-friendly way. Each octal symbol represents 3 binary bits and each hex digit represents 4 binary bits. For example, the decimal number 53201, stored as in the computer as the binary number 10000001111000101100, is represented by the octal number 2017054, and the hex number 81E2C. The table below compares the number systems: <br />
:{| class="wikitable"<br />
!Number System<br />
!Base<br />
!Digits Used<br />
!Examples<br />
|-<br />
|Binary<br />
|2<br />
|0,1<br />
|$10110_{2}$, $10110011_{2}$<br />
|-<br />
|Octal<br />
|8<br />
|0,1,2,3,4,5,6,7<br />
|$75021_{8}$, $231_{8}$, $60012_{8}$<br />
|-<br />
|Decimal<br />
|10<br />
|0,1,2,3,4,5,6,7,8,9<br />
|$97425_{10}$ or simply 97425<br />
|-<br />
|Hexadecimal<br />
|16<br />
|0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F<br />
|$54A2DD0F_{16}$<br />
|}<br />
<br />
<br />
In general, $N$ bits have $2^N$ different values. The computers aboard the Apollo spacecraft had 8-bit words of memory. Each word of memory could store 256 different values, and the contents were displayed using 2 hex characters.<br />
<br />
The following table shows the first 20 numbers in decimal, binary, octal, and hexadecimal:<br />
:{| class="wikitable" style="text-align:center"<br />
!Decimal<br />
!Binary<br />
!Octal<br />
!Hexadecimal<br />
|-<br />
|0<br />
|0<br />
|0<br />
|0 <br />
|-<br />
|1<br />
|1<br />
|1<br />
|1 <br />
|-<br />
|2<br />
|10<br />
|2<br />
|2 <br />
|-<br />
|3<br />
|11<br />
|3<br />
|3 <br />
|-<br />
|4<br />
|100<br />
|4<br />
|4 <br />
|-<br />
|5<br />
|101<br />
|5<br />
|5 <br />
|-<br />
|6<br />
|110<br />
|6<br />
|6 <br />
|-<br />
|7<br />
|111<br />
|7<br />
|7 <br />
|-<br />
|8<br />
|1000<br />
|10<br />
|8<br />
|-<br />
|9<br />
|1001<br />
|11<br />
|9<br />
|-<br />
|10<br />
|1010<br />
|12<br />
|A<br />
|-<br />
|11<br />
|1011<br />
|13<br />
|B<br />
|-<br />
|12<br />
|1100<br />
|14<br />
|C <br />
|-<br />
|13<br />
|1101<br />
|15<br />
|D <br />
|-<br />
|14<br />
|1110<br />
|16<br />
|E <br />
|-<br />
|15<br />
|1111<br />
|17<br />
|F <br />
|-<br />
|16<br />
|10000<br />
|20<br />
|10 <br />
|-<br />
|17<br />
|10001<br />
|21<br />
|11<br />
|-<br />
|18<br />
|10010<br />
|22<br />
|12<br />
|-<br />
|19<br />
|10011<br />
|23<br />
|13<br />
|-<br />
|20<br />
|10100<br />
|24<br />
|14<br />
|}<br />
<br />
== Decimal Values ==<br />
<br />
The value of a any number is the sum of each digit multiplied by its place value. Here are some examples:<br />
<br />
:$12345 = 1×10000 + 2×1000 + 3×100 + 4×10 + 5×1 = 1×10^{4}+ 2×10^{3}+ 3×10^{2}+ 4×10^{1}+ 5×10{0}.$<br />
<br />
:$1101_{2} = 1 × 2^3 + 1 × 2^2 + 0 × 2^1 + 1 × 2^0 = 8 + 4 + 0 + 1 = 13_{10}$ <br />
<br />
:$175_{8} = 1 × 8^2 + 7 × 8^1 + 5 × 8^0 = 1 × 64 + 7 × 8 + 5 × 1 = 64 + 56 + 5 = 125_{10}$. <br />
<br />
:$A5E_{16} = 10 × 16^2 + 5 × 16^1 + 14 × 16^0 = 10 × 256 + 5 × 16 + 14 × 1 = 2560 + 80 + 14 = 2654_{10}$. <br />
<br />
<br />
== Converting between Binary, Octal, and Hexadecimal == <br />
<br />
Converting from octal to base 2 is simple: replace each octal digit by its corresponding 3 binary bits. For example:<br />
:$375_{8} = \ \ 011\ \ \ 111\ \ \ 101_{2}$<br />
Converting from hex to base is also simple: replace each hex digit by its corresponding 4 binary bits. For example:<br />
:$FD_{16} = \ \ 1111\ \ \ 1101_{2}$<br />
Converting from binary to either octal or hex is pretty simple as well: group the bit by 3s or 4s (starting at the right), and convert each group:<br />
:$10000001111000101100 = 10\ 000\ 001\ 111\ 000\ 101 \ 100 = 2017054_8$<br />
<br />
:$10000001111000101100 = 1000\ 0001\ 1110\ 0010\ 1100 = 81E2C_{16}$<br />
You can convert between base 8 and 16 by expressing the number in base 2 (easy to do!) and then converting that number from base 2 (another easy operation)! This is shown below in Sample Problem #1.<br />
<br />
== Using Hexadecimal Numbers to Represent Colors ==<br />
<br />
Computers use hexadecimal numbers to represent various colors in computer graphics because all computer screens use combinations of red, green, and blue light or RGB to represent thousands of different colors. Two digits are used for each so the hexadecimal number “#FF0000” represents the color red, “#00FF00” represents green, and “#0000FF” represents blue. The color black is “#000000” and white is “#FFFFFF”. <br />
<br />
The hash tag or number sign is used to denote a hexadecimal number. $FF_{16} = F (15) × 16 + F (15) × 1 = 240 + 15 = 255_{10}$ so there are 0 to 255 or 256 different shades of each color or $256^{3} = 16,777,216$ different colors. <br />
<br />
The following web site has nearly every color name, along with its hex code and decimal values: <br />
<br />
:https://www.rapidtables.com/web/color/RGB_Color.html <br />
<br />
For example “salmon” is “#FA8072” which represents the decimal numbers 250 (hex FA), 128 (hex 80), and 114 (hex 72).<br />
<br />
== Resources ==<br />
<br />
[https://ryanstutorials.net/ Ryan's Tutorials] covers this topic beautifully. Rather than trying to duplicate that work, we'll point you to the different sections:<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/ 1. Number Systems] - An introduction to what numbers systems are all about, with emphasis on decimal, binary, octal, and hexadecimal. ACSL will typically identify the base of a number using a subscript. For example, $123_8$ is an octal number, whereas $123_{16}$ is a hexadecimal number. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-conversions.php 2. Binary Conversions] - This section shows how to convert between binary, decimal, hexadecimal and octal numbers. In the [https://ryanstutorials.net/binary-tutorial/binary-conversions.php#activities ''Activities''] section, you can practice converting numbers. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-arithmetic.php 3. Binary Arithmetic] - Describes how to perform various arithmetic operations (addition, subtraction, multiplication, and division) with binary numbers. ACSL problems will also cover basic arithmetic in other bases, such as adding and subtracting together 2 hexadecimal numbers. ACSL problems will not cover division in other bases.<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php 4. Negative Numbers] - ACSL problems will not cover how negative numbers are represented in binary. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-floating-point.php 5. Binary Fractions and Floating Point] - The first part of this section is relevant to ACSL: fractions in other bases. ACSL will not cover floating point numbers in other basis. So, focus on the section [https://ryanstutorials.net/binary-tutorial/binary-floating-point.php#convertfraction Converting to a Binary Fraction], but keep in mind that ACSL problems may also cover octal and hexadecimal fractions.<br />
<br />
The [https://coolconversion.com/math/binary-octal-hexa-decimal/ CoolConversion.com online calculator] is another online app for practicing conversion from/to decimal, hexadecimal, octal and binary; this tool shows the steps that one goes through in the conversion. <br />
<br />
The AskNumbers.com site has a nice description of the conversions between [https://www.asknumbers.com/hex-to-octal.aspx binary, octal, decimal and hexadecimal] numbers. <br />
<br />
== Format of ACSL Problems ==<br />
<br />
The problems in this category will focus on converting between binary, octal, decimal, and hexadecimal, basic arithmetic of numbers in those bases, and, occasionally, fractions in those bases.<br />
<br />
To be successful in this category, you must know the following facts cold:<br />
<br />
# The binary value of each octal digit 0, 1, ..., 7<br />
# The binary value of each hex digit 0, 1, ..., 9, A, B, C, D, E, F<br />
# The decimal value of each hex digit 0, 1, ..., F <br />
# Powers of 2, up to 4096<br />
# Powers of 8, up to 4096<br />
# Powers of 16, up to 65,536<br />
<br />
== Sample Problems ==<br />
<br />
=== Sample Problem 1 ===<br />
<br />
Solve for $x$ where $x_{16}=3676_8$.<br />
<br />
'''Solution:''' One method of solution is to convert $3676_8$ into base 10, and then convert that number into base 16 to yield the value of $x$.<br />
<br />
An easier solution, less prone to arithmetic mistakes, is to convert from octal (base 8) to hexadecimal (base 16) through the binary (base 2) representation of the number:<br />
<br />
$$\begin{align}<br />
3676_8 &= 011 ~ 110 ~ 111 ~ 110_2 & \text{convert each octal digit into base 2}\hfill\cr <br />
&= 0111 ~ 1011 ~ 1110_2 & \text{group by 4 bits, from right-to-left}\hfill\cr<br />
&= 7 ~ \text{B} ~ \text{E}_{16} & \text{convert each group of 4 bits into a hex digit}\cr<br />
\end{align}$$<br />
<br />
=== Sample Problem 2 ===<br />
<br />
Solve for $x$ in the following hexadecimal equation: $ x= \text{F5AD}_{16} - \text{69EB}_{16}$<br />
<br />
'''Solution:''' One could convert the hex numbers into base 10, perform the subtraction, and then convert the answer back to base 16. However, <br />
working directly in base 16 isn't too hard. <br />
As in conventional decimal arithmetic, one works from right-to-left, from the least significant digits to the most. <br />
<br />
:The rightmost digit becomes 2, because D-B=2. <br />
:The next column is A-E. We need to ''borrow'' a one from the 5 column, and 1A-E=C <br />
:In the next column, 4-9=B, again, borrowing a 1 from the next column.<br />
:Finally, the leftmost column, E-6=8<br />
<br />
Combining these results of each column, we get a final answer of $8BC2_{16}$.<br />
<br />
=== Sample Problem 3 ===<br />
<br />
How many numbers from 100 to 200 in base 10 consist of distinct<br />
ascending digits and also have distinct ascending hex digits when<br />
converted to base 16? <br />
<br />
'''Solution:''' There are 13 numbers that have ascending digits in both bases from 100<br />
to 200. They are (in base 10):<br />
123 (7B), 124, 125, 126, 127 (7F), 137 (89), 138, 139 (8B), 156 (9C), 157, 158, 159 (9F), 189 (BD)<br />
<br />
== Video Resources ==<br />
<br />
There are many YouTube videos about computer number systems. Here are a handful that cover the topic nicely, without too many ads:<br />
<br />
{|<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/aW3qCcH6Dao</youtube><br />
| [https://youtu.be/aW3qCcH6Dao ''Number Systems - Converting Decimal, Binary and Hexadecimal'' ('''Joe James''')]<br />
<br />
An introduction to number systems, and how to convert between decimal, binary and hexadecimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/m1JtWKuTLR0</youtube><br />
| [https://youtu.be/m1JtWKuTLR0 ''Lesson 2.3 : Hexadecimal Tutorial'' ('''Carl Herold''')]<br />
<br />
The video focuses on hexadecimal numbers: their relationship to binary numbers and how to convert to decimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/WR67syBDzew</youtube><br />
| [https://youtu.be/WR67syBDzew ''Hexes and the Magic of Base 16 - Vaidehi Joshi - May 2017' ('''DonutJS''')]<br />
<br />
A fun introduction to hexadecimal numbers, focusing a bit <br />
on using hex numbers for specifying RGB colors. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jvx-NrILgpE</youtube><br />
| [https://youtu.be/jvx-NrILgpE ''Collins Lab: Binary & Hex'' ('''Adafruit Industries''')]<br />
<br />
A professionally produced video that explains the number systems, how and why binary numbers are fundamental to computer science, and why hexadecimal is important to computer programmers. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Computer_Number_Systems&diff=826Computer Number Systems2020-09-01T23:32:48Z<p>Marc: </p>
<hr />
<div>All digital computers, from supercomputers to your smartphone, are electronic devices and ultimately can do one thing: detect whether an electrical signal is on or off. That basic information, called a ''bit'' ('''bi'''nary digi'''t'''), has two values: a 1 (or ''true'') when the signal is on, and a 0 (of ''false'') when the signal is off. <br />
<br />
Larger values can be stored by a group of bits. For example, there are 4 different values stored by 2 bits (00, 01, 10, and 11), 8 values for 3 bits (000, 001, 010, 011, 100, 101, 110, and 111), 16 values for 4 bits (0000, 0001, ..., 1111), and so on. In general, $n$ bits can store $2^n$ different values. Large numbers, using 0s and 1s only, is quite unwieldy for human. For example, a computer would need 19 bits to store the numbers up to 500,000! <br />
<br />
Fortunately, we can make use of different number systems to work with large binary strings that are represent numbers within computers.<br />
<br />
== Different Number Systems ==<br />
<br />
A ''number system'' is the way we name and represent numbers. The number system we use in our daily life is known as '''decimal number system''' or '''base 10''' because it is based on 10 different digits: 0, 1, 2, ..., 8, and 9. The base of number is written as subscript to a number. For example, the decimal number "twelve thousand three hundred and forty-five" is written as $12345_{10}$. Without a base, it's assumed that a number is in base 10. <br />
<br />
In computer science, apart from the decimal system, three additional number systems are used: '''binary''' (base-2), '''octal''' (base-8), and '''hexadecimal''' or just '''hex''' (base-16). The table below compares the number systems: <br />
:{| class="wikitable"<br />
!Number System<br />
!Base<br />
!Digits Used<br />
!Sample Numbers<br />
|-<br />
|Binary<br />
|2<br />
|0,1<br />
|$10110_{2}$, $10110011_{2}$<br />
|-<br />
|Octal<br />
|8<br />
|0,1,2,3,4,5,6,7<br />
|$75021_{8}$, $231_{8}$, and $60012_{8}$<br />
|-<br />
|Decimal<br />
|10<br />
|0,1,2,3,4,5,6,7,8,9<br />
|$97425_{10}$ or simply 97425<br />
|-<br />
|Hexadecimal<br />
|16<br />
|0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F<br />
|$54A2DD0F_{16}$<br />
|}<br />
<br />
Octal and hexadecimal are used to represent binary numbers is a user-friendly way. Each octal symbol represents 3 binary bits and each hex digit represents 4 binary bits. Thus, the number 53201, stored as in the computer as the binary number 10000001111000101100, is represented by the octal number 2017054, and the hex number 81E2C.<br />
<br />
In general, $N$ bits have $2^N$ different values. The computers aboard the Apollo spacecraft had 8-bit words of memory. Each word of memory could store 256 different values, and the contents were displayed using 2 hex characters.<br />
<br />
The following table shows the first 20 numbers in decimal, binary, octal, and hexadecimal:<br />
:{| class="wikitable" style="text-align:center"<br />
!Decimal<br />
!Binary<br />
!Octal<br />
!Hexadecimal<br />
|-<br />
|0<br />
|0<br />
|0<br />
|0 <br />
|-<br />
|1<br />
|1<br />
|1<br />
|1 <br />
|-<br />
|2<br />
|10<br />
|2<br />
|2 <br />
|-<br />
|3<br />
|11<br />
|3<br />
|3 <br />
|-<br />
|4<br />
|100<br />
|4<br />
|4 <br />
|-<br />
|5<br />
|101<br />
|5<br />
|5 <br />
|-<br />
|6<br />
|110<br />
|6<br />
|6 <br />
|-<br />
|7<br />
|111<br />
|7<br />
|7 <br />
|-<br />
|8<br />
|1000<br />
|10<br />
|8<br />
|-<br />
|9<br />
|1001<br />
|11<br />
|9<br />
|-<br />
|10<br />
|1010<br />
|12<br />
|A<br />
|-<br />
|11<br />
|1011<br />
|13<br />
|B<br />
|-<br />
|12<br />
|1100<br />
|14<br />
|C <br />
|-<br />
|13<br />
|1101<br />
|15<br />
|D <br />
|-<br />
|14<br />
|1110<br />
|16<br />
|E <br />
|-<br />
|15<br />
|1111<br />
|17<br />
|F <br />
|-<br />
|16<br />
|10000<br />
|20<br />
|10 <br />
|-<br />
|17<br />
|10001<br />
|21<br />
|11<br />
|-<br />
|18<br />
|10010<br />
|22<br />
|12<br />
|-<br />
|19<br />
|10011<br />
|23<br />
|13<br />
|-<br />
|20<br />
|10100<br />
|24<br />
|14<br />
|}<br />
<br />
<br />
<br />
== Formation of Numbers ==<br />
<br />
The formation of any number goes by the digit multiplied by its place value and adding them together. For example, we read and understand the number 12345 as twelve thousand three hundred and forty-five which has five digits: 1, 2, 3, 4, and 5. The number 12345 is completely different from the number 54321, which is also having the exact same 5 digits. This tells us that the digit with its place value give us the number. So, how to create/form a number in decimal system? Let's see using these 2 example numbers:<br />
:$12345 = 1×10000 + 2×1000 + 3×100 + 4×10 + 5×1 = 1×10^{4}+ 2×10^{3}+ 3×10^{2}+ 4×10^{1}+ 5×10{0}.$<br />
:$54321 = 5×10000 + 4×1000 + 3×100 + 2×10 + 1×1 = 5×10^{4}+ 4×10^{3}+ 3×10^{2}+ 2×10^{1}+ 1×10{0}.$<br />
<br />
The expanded notation in the decimal number system above can represent different numbers by using powers of 10.<br />
<br />
All other bases work similarly:<br />
<br />
:$1101_{2} = 1 × 2^3 + 1 × 2^2 + 0 × 2^1 + 1 × 2^0 = 8 + 4 + 0 + 1 = 13_{10}$ <br />
:$175_{8} = 1 × 8^2 + 7 × 8^1 + 5 × 8^0 = 1 × 64 + 7 × 8 + 5 × 1 = 64 + 56 + 5 = 125_{10}$. <br />
:$A5E_{16} = 10 × 16^2 + 5 × 16^1 + 14 × 16^0 = 10 × 256 + 5 × 16 + 14 × 1 = 2560 + 80 + 14 = 2654_{10}$. <br />
<br />
<br />
'''Grouping Binary Digits for Octal and Hexadecimal Numbers''' <br />
<br />
To convert from octal to base 2 is simple: replace each octal digit by its corresponding 3 binary bits. For example:<br />
:$375_{8} = \ \ 011\ \ \ 111\ \ \ 101_{2}$<br />
To convert from hex to base is also simple: replace each hex digit by its corresponding 4 binary bits. For example:<br />
:$FD_{16} = \ \ 1111\ \ \ 1101_{2}$<br />
To convert from binary to either octal or hex is pretty simple as well: group the bit by 3s or 4s (starting at the right), and convert each group:<br />
:$10000001111000101100 = 10\ 000\ 001\ 111\ 000\ 101 \ 100 = 2017054_8$<br />
:$10000001111000101100 = 1000\ 0001\ 1110\ 0010\ 1100 = 81E2C_{16}$<br />
It's easy to see that you can convert between base 8 and 16 by expressing the number in base 2 (easy to do!) and then converting that number from base 2 (another easy operation)! This is shown below in Sample Problem #1.<br />
<br />
'''Using Hexadecimal Numbers to Represent Colors'''<br />
<br />
Computers use hexadecimal numbers to represent various colors in computer graphics because all computer screens use combinations of red, green, and blue light or RGB to represent thousands of different colors. Two digits are used for each so the hexadecimal number “#FF0000” represents the color red, “#00FF00” represents green, and “#0000FF” represents blue. The color black is “#000000” and white is “#FFFFFF”. <br />
<br />
The hash tag or number sign is used to denote a hexadecimal number. $FF_{16} = F (15) × 16 + F (15) × 1 = 240 + 15 = 255_{10}$ so there are 0 to 255 or 256 different shades of each color or $256^{3} = 16,777,216$ different colors. <br />
<br />
The following web site has nearly every color name, along with its hex code and decimal values: <br />
<br />
:https://www.rapidtables.com/web/color/RGB_Color.html <br />
<br />
For example “salmon” is “#FA8072” which represents the decimal numbers 250 (hex FA), 128 (hex 80), and 114 (hex 72).<br />
<br />
== Resources ==<br />
<br />
[https://ryanstutorials.net/ Ryan's Tutorials] covers this topic beautifully. Rather than trying to duplicate that work, we'll point you to the different sections:<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/ 1. Number Systems] - An introduction to what numbers systems are all about, with emphasis on decimal, binary, octal, and hexadecimal. ACSL will typically identify the base of a number using a subscript. For example, $123_8$ is an octal number, whereas $123_{16}$ is a hexadecimal number. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-conversions.php 2. Binary Conversions] - This section shows how to convert between binary, decimal, hexadecimal and octal numbers. In the [https://ryanstutorials.net/binary-tutorial/binary-conversions.php#activities ''Activities''] section, you can practice converting numbers. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-arithmetic.php 3. Binary Arithmetic] - Describes how to perform various arithmetic operations (addition, subtraction, multiplication, and division) with binary numbers. ACSL problems will also cover basic arithmetic in other bases, such as adding and subtracting together 2 hexadecimal numbers. ACSL problems will not cover division in other bases.<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php 4. Negative Numbers] - ACSL problems will not cover how negative numbers are represented in binary. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-floating-point.php 5. Binary Fractions and Floating Point] - The first part of this section is relevant to ACSL: fractions in other bases. ACSL will not cover floating point numbers in other basis. So, focus on the section [https://ryanstutorials.net/binary-tutorial/binary-floating-point.php#convertfraction Converting to a Binary Fraction], but keep in mind that ACSL problems may also cover octal and hexadecimal fractions.<br />
<br />
The [https://coolconversion.com/math/binary-octal-hexa-decimal/ CoolConversion.com online calculator] is another online app for practicing conversion from/to decimal, hexadecimal, octal and binary; this tool shows the steps that one goes through in the conversion. <br />
<br />
The AskNumbers.com site has a nice description of the conversions between [https://www.asknumbers.com/hex-to-octal.aspx binary, octal, decimal and hexadecimal] numbers. <br />
<br />
== Format of ACSL Problems ==<br />
<br />
The problems in this category will focus on converting between binary, octal, decimal, and hexadecimal, basic arithmetic of numbers in those bases, and, occasionally, fractions in those bases.<br />
<br />
To be successful in this category, you must know the following facts cold:<br />
<br />
# The binary value of each octal digit 0, 1, ..., 7<br />
# The binary value of each hex digit 0, 1, ..., 9, A, B, C, D, E, F<br />
# The decimal value of each hex digit 0, 1, ..., F <br />
# Powers of 2, up to 4096<br />
# Powers of 8, up to 4096<br />
# Powers of 16, up to 65,536<br />
<br />
== Sample Problems ==<br />
<br />
=== Sample Problem 1 ===<br />
<br />
Solve for $x$ where $x_{16}=3676_8$.<br />
<br />
'''Solution:''' One method of solution is to convert $3676_8$ into base 10, and then convert that number into base 16 to yield the value of $x$.<br />
<br />
An easier solution, less prone to arithmetic mistakes, is to convert from octal (base 8) to hexadecimal (base 16) through the binary (base 2) representation of the number:<br />
<br />
$$\begin{align}<br />
3676_8 &= 011 ~ 110 ~ 111 ~ 110_2 & \text{convert each octal digit into base 2}\hfill\cr <br />
&= 0111 ~ 1011 ~ 1110_2 & \text{group by 4 bits, from right-to-left}\hfill\cr<br />
&= 7 ~ \text{B} ~ \text{E}_{16} & \text{convert each group of 4 bits into a hex digit}\cr<br />
\end{align}$$<br />
<br />
=== Sample Problem 2 ===<br />
<br />
Solve for $x$ in the following hexadecimal equation: $ x= \text{F5AD}_{16} - \text{69EB}_{16}$<br />
<br />
'''Solution:''' One could convert the hex numbers into base 10, perform the subtraction, and then convert the answer back to base 16. However, <br />
working directly in base 16 isn't too hard. <br />
As in conventional decimal arithmetic, one works from right-to-left, from the least significant digits to the most. <br />
<br />
:The rightmost digit becomes 2, because D-B=2. <br />
:The next column is A-E. We need to ''borrow'' a one from the 5 column, and 1A-E=C <br />
:In the next column, 4-9=B, again, borrowing a 1 from the next column.<br />
:Finally, the leftmost column, E-6=8<br />
<br />
Combining these results of each column, we get a final answer of $8BC2_{16}$.<br />
<br />
=== Sample Problem 3 ===<br />
<br />
How many numbers from 100 to 200 in base 10 consist of distinct<br />
ascending digits and also have distinct ascending hex digits when<br />
converted to base 16? <br />
<br />
'''Solution:''' There are 13 numbers that have ascending digits in both bases from 100<br />
to 200. They are (in base 10):<br />
123 (7B), 124, 125, 126, 127 (7F), 137 (89), 138, 139 (8B), 156 (9C), 157, 158, 159 (9F), 189 (BD)<br />
<br />
== Video Resources ==<br />
<br />
There are many YouTube videos about computer number systems. Here are a handful that cover the topic nicely, without too many ads:<br />
<br />
{|<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/aW3qCcH6Dao</youtube><br />
| [https://youtu.be/aW3qCcH6Dao ''Number Systems - Converting Decimal, Binary and Hexadecimal'' ('''Joe James''')]<br />
<br />
An introduction to number systems, and how to convert between decimal, binary and hexadecimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/m1JtWKuTLR0</youtube><br />
| [https://youtu.be/m1JtWKuTLR0 ''Lesson 2.3 : Hexadecimal Tutorial'' ('''Carl Herold''')]<br />
<br />
The video focuses on hexadecimal numbers: their relationship to binary numbers and how to convert to decimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/WR67syBDzew</youtube><br />
| [https://youtu.be/WR67syBDzew ''Hexes and the Magic of Base 16 - Vaidehi Joshi - May 2017' ('''DonutJS''')]<br />
<br />
A fun introduction to hexadecimal numbers, focusing a bit <br />
on using hex numbers for specifying RGB colors. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jvx-NrILgpE</youtube><br />
| [https://youtu.be/jvx-NrILgpE ''Collins Lab: Binary & Hex'' ('''Adafruit Industries''')]<br />
<br />
A professionally produced video that explains the number systems, how and why binary numbers are fundamental to computer science, and why hexadecimal is important to computer programmers. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Computer_Number_Systems&diff=825Computer Number Systems2020-09-01T23:30:45Z<p>Marc: </p>
<hr />
<div>All digital computers, from supercomputers to your smartphone, are electronic devices and ultimately can do one thing: detect whether an electrical signal is on or off. That basic information, called a ''bit'' ('''bi'''nary digi'''t'''), has two values: a 1 (or ''true'') when the signal is on, and a 0 (of ''false'') when the signal is off. <br />
<br />
Larger values can be stored by a group of bits. For example, there are 4 different values stored by 2 bits (00, 01, 10, and 11), 8 values for 3 bits (000, 001, 010, 011, 100, 101, 110, and 111), 16 values for 4 bits (0000, 0001, ..., 1111), and so on. In general, $n$ bits can store $2^n$ different values. Large numbers, using 0s and 1s only, is quite unwieldy for human. For example, a computer would need 19 bits to store the numbers up to 500,000! <br />
<br />
Fortunately, we can make use of different number systems to work with large binary strings that are represent numbers within computers.<br />
<br />
== Different Number Systems ==<br />
<br />
A ''number system'' is the way we name and represent numbers. The number system we use in our daily life is known as '''decimal number system''' or '''base 10''' because it is based on 10 different digits: 0, 1, 2, ..., 8, and 9. The base of number is written as subscript to a number. For example, the decimal number "twelve thousand three hundred and forty-five" is written as $12345_{10}$. Without a base, it's assumed that a number is in base 10. <br />
<br />
In computer science, apart from the decimal system, three additional number systems are used: '''binary''' (base-2), '''octal''' (base-8), and '''hexadecimal''' or just '''hex''' (base-16). The table below compares the number systems: <br />
:{| class="wikitable"<br />
!Number System<br />
!Base<br />
!Digits Used<br />
!Sample Numbers<br />
|-<br />
|Binary<br />
|2<br />
|0,1<br />
|$10110_{2}$, $10110011_{2}$<br />
|-<br />
|Octal<br />
|8<br />
|0,1,2,3,4,5,6,7<br />
|$75021_{8}$, $231_{8}$, and $60012_{8}$<br />
|-<br />
|Decimal<br />
|10<br />
|0,1,2,3,4,5,6,7,8,9<br />
|$97425_{10}$ or simply 97425<br />
|-<br />
|Hexadecimal<br />
|16<br />
|0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F<br />
|$54A2DD0F_{16}$<br />
|}<br />
<br />
Octal and hexadecimal are used to represent binary numbers is a user-friendly way. Each octal symbol represents 3 binary bits and each hex digit represents 4 binary bits. Thus, the number 53201, stored as in the computer as the binary number 10000001111000101100, is represented by the octal number 2017054, and the hex number 81E2C.<br />
<br />
In general, $N$ bits have $2^N$ different values. The computers aboard the Apollo spacecraft had 8-bit words of memory. Each word of memory could store 256 different values, and the contents were displayed using 2 hex characters.<br />
<br />
The following table shows the first 20 numbers in decimal, binary, octal, and hexadecimal:<br />
:{| class="wikitable" style="text-align:center"<br />
!Decimal<br />
!Binary<br />
!Octal<br />
!Hexadecimal<br />
|-<br />
|0<br />
|0<br />
|0<br />
|0 <br />
|-<br />
|1<br />
|1<br />
|1<br />
|1 <br />
|-<br />
|2<br />
|10<br />
|2<br />
|2 <br />
|-<br />
|3<br />
|11<br />
|3<br />
|3 <br />
|-<br />
|4<br />
|100<br />
|4<br />
|4 <br />
|-<br />
|5<br />
|101<br />
|5<br />
|5 <br />
|-<br />
|6<br />
|110<br />
|6<br />
|6 <br />
|-<br />
|7<br />
|111<br />
|7<br />
|7 <br />
|-<br />
|8<br />
|1000<br />
|10<br />
|8<br />
|-<br />
|9<br />
|1001<br />
|11<br />
|9<br />
|-<br />
|10<br />
|1010<br />
|12<br />
|A<br />
|-<br />
|11<br />
|1011<br />
|13<br />
|B<br />
|-<br />
|12<br />
|1100<br />
|14<br />
|C <br />
|-<br />
|13<br />
|1101<br />
|15<br />
|D <br />
|-<br />
|14<br />
|1110<br />
|16<br />
|E <br />
|-<br />
|15<br />
|1111<br />
|17<br />
|F <br />
|-<br />
|16<br />
|10000<br />
|20<br />
|10 <br />
|-<br />
|17<br />
|10001<br />
|21<br />
|11<br />
|-<br />
|18<br />
|10010<br />
|22<br />
|12<br />
|-<br />
|19<br />
|10011<br />
|23<br />
|13<br />
|-<br />
|20<br />
|10100<br />
|24<br />
|14<br />
|}<br />
<br />
<br />
<br />
== Formation of Numbers ==<br />
<br />
The formation of any number goes by the digit multiplied by its place value and adding them together. For example, we read and understand the number 12345 as twelve thousand three hundred and forty-five which has five digits: 1, 2, 3, 4, and 5. The number 12345 is completely different from the number 54321, which is also having the exact same 5 digits. This tells us that the digit with its place value give us the number. So, how to create/form a number in decimal system? Let's see using these 2 example numbers:<br />
:$12345 = 1×10000 + 2×1000 + 3×100 + 4×10 + 5×1 = 1×10^{4}+ 2×10^{3}+ 3×10^{2}+ 4×10^{1}+ 5×10{0}.$<br />
:$54321 = 5×10000 + 4×1000 + 3×100 + 2×10 + 1×1 = 5×10^{4}+ 4×10^{3}+ 3×10^{2}+ 2×10^{1}+ 1×10{0}.$<br />
<br />
The expanded notation in the decimal number system above can represent different numbers by using powers of 10.<br />
<br />
All other bases work similarly:<br />
<br />
:$1101_{2} = 1 × 2^3 + 1 × 2^2 + 0 × 2^1 + 1 × 2^0 = 8 + 4 + 0 + 1 = 13_{10}$ <br />
:$175_{8} = 1 × 8^2 + 7 × 8^1 + 5 × 8^0 = 1 × 64 + 7 × 8 + 5 × 1 = 64 + 56 + 5 = 125_{10}$. <br />
:$A5E_{16} = 10 × 16^2 + 5 × 16^1 + 14 × 16^0 = 10 × 256 + 5 × 16 + 14 × 1 = 2560 + 80 + 14 = 2654_{10}$. <br />
<br />
<br />
'''Grouping Binary Digits for Octal and Hexadecimal Numbers''' <br />
<br />
To convert from octal to base 2 is simple: replace each octal digit by its corresponding 3 binary bits. For example:<br />
:$375_{8} = \ \ 011\ \ \ 111\ \ \ 101_{2}$<br />
To convert from hex to base is also simple: replace each hex digit by its corresponding 4 binary bits. For example:<br />
:$FD_{16} = \ \ 1111\ \ \ 1101_{2}$<br />
To convert from binary to either octal or hex is pretty simple as well: group the bit by 3s or 4s (starting at the right), and convert each group:<br />
$10000001111000101100 = 10\ 000\ 001\ 111\ 000\ 101 \ 100 = 2017054_8$<br />
$10000001111000101100 = 1000\ 0001\ 1110\ 0010\ 1100 = 81E2C_{16}$<br />
It's easy to see that you can convert between base 8 and 16 by expressing the number in base 2 (easy to do!) and then converting that number from base 2 (another easy operation)! This is shown below in Sample Problem #1.<br />
<br />
'''Using Hexadecimal Numbers to Represent Colors'''<br />
<br />
Computers use hexadecimal numbers to represent various colors in computer graphics because all computer screens use combinations of red, green, and blue light or RGB to represent thousands of different colors. Two digits are used for each so the hexadecimal number “#FF0000” represents the color red, “#00FF00” represents green, and “#0000FF” represents blue. The color black is “#000000” and white is “#FFFFFF”. <br />
<br />
The hash tag or number sign is used to denote a hexadecimal number. $FF_{16} = F (15) × 16 + F (15) × 1 = 240 + 15 = 255_{10}$ so there are 0 to 255 or 256 different shades of each color or $256^{3} = 16,777,216$ different colors. <br />
<br />
The following web site has nearly every color name, along with its hex code and decimal values: <br />
<br />
:https://www.rapidtables.com/web/color/RGB_Color.html <br />
<br />
For example “salmon” is “#FA8072” which represents the decimal numbers 250 (hex FA), 128 (hex 80), and 114 (hex 72).<br />
<br />
== Resources ==<br />
<br />
[https://ryanstutorials.net/ Ryan's Tutorials] covers this topic beautifully. Rather than trying to duplicate that work, we'll point you to the different sections:<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/ 1. Number Systems] - An introduction to what numbers systems are all about, with emphasis on decimal, binary, octal, and hexadecimal. ACSL will typically identify the base of a number using a subscript. For example, $123_8$ is an octal number, whereas $123_{16}$ is a hexadecimal number. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-conversions.php 2. Binary Conversions] - This section shows how to convert between binary, decimal, hexadecimal and octal numbers. In the [https://ryanstutorials.net/binary-tutorial/binary-conversions.php#activities ''Activities''] section, you can practice converting numbers. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-arithmetic.php 3. Binary Arithmetic] - Describes how to perform various arithmetic operations (addition, subtraction, multiplication, and division) with binary numbers. ACSL problems will also cover basic arithmetic in other bases, such as adding and subtracting together 2 hexadecimal numbers. ACSL problems will not cover division in other bases.<br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php 4. Negative Numbers] - ACSL problems will not cover how negative numbers are represented in binary. <br />
<br />
:[https://ryanstutorials.net/binary-tutorial/binary-floating-point.php 5. Binary Fractions and Floating Point] - The first part of this section is relevant to ACSL: fractions in other bases. ACSL will not cover floating point numbers in other basis. So, focus on the section [https://ryanstutorials.net/binary-tutorial/binary-floating-point.php#convertfraction Converting to a Binary Fraction], but keep in mind that ACSL problems may also cover octal and hexadecimal fractions.<br />
<br />
The [https://coolconversion.com/math/binary-octal-hexa-decimal/ CoolConversion.com online calculator] is another online app for practicing conversion from/to decimal, hexadecimal, octal and binary; this tool shows the steps that one goes through in the conversion. <br />
<br />
The AskNumbers.com site has a nice description of the conversions between [https://www.asknumbers.com/hex-to-octal.aspx binary, octal, decimal and hexadecimal] numbers. <br />
<br />
== Format of ACSL Problems ==<br />
<br />
The problems in this category will focus on converting between binary, octal, decimal, and hexadecimal, basic arithmetic of numbers in those bases, and, occasionally, fractions in those bases.<br />
<br />
To be successful in this category, you must know the following facts cold:<br />
<br />
# The binary value of each octal digit 0, 1, ..., 7<br />
# The binary value of each hex digit 0, 1, ..., 9, A, B, C, D, E, F<br />
# The decimal value of each hex digit 0, 1, ..., F <br />
# Powers of 2, up to 4096<br />
# Powers of 8, up to 4096<br />
# Powers of 16, up to 65,536<br />
<br />
== Sample Problems ==<br />
<br />
=== Sample Problem 1 ===<br />
<br />
Solve for $x$ where $x_{16}=3676_8$.<br />
<br />
'''Solution:''' One method of solution is to convert $3676_8$ into base 10, and then convert that number into base 16 to yield the value of $x$.<br />
<br />
An easier solution, less prone to arithmetic mistakes, is to convert from octal (base 8) to hexadecimal (base 16) through the binary (base 2) representation of the number:<br />
<br />
$$\begin{align}<br />
3676_8 &= 011 ~ 110 ~ 111 ~ 110_2 & \text{convert each octal digit into base 2}\hfill\cr <br />
&= 0111 ~ 1011 ~ 1110_2 & \text{group by 4 bits, from right-to-left}\hfill\cr<br />
&= 7 ~ \text{B} ~ \text{E}_{16} & \text{convert each group of 4 bits into a hex digit}\cr<br />
\end{align}$$<br />
<br />
=== Sample Problem 2 ===<br />
<br />
Solve for $x$ in the following hexadecimal equation: $ x= \text{F5AD}_{16} - \text{69EB}_{16}$<br />
<br />
'''Solution:''' One could convert the hex numbers into base 10, perform the subtraction, and then convert the answer back to base 16. However, <br />
working directly in base 16 isn't too hard. <br />
As in conventional decimal arithmetic, one works from right-to-left, from the least significant digits to the most. <br />
<br />
:The rightmost digit becomes 2, because D-B=2. <br />
:The next column is A-E. We need to ''borrow'' a one from the 5 column, and 1A-E=C <br />
:In the next column, 4-9=B, again, borrowing a 1 from the next column.<br />
:Finally, the leftmost column, E-6=8<br />
<br />
Combining these results of each column, we get a final answer of $8BC2_{16}$.<br />
<br />
=== Sample Problem 3 ===<br />
<br />
How many numbers from 100 to 200 in base 10 consist of distinct<br />
ascending digits and also have distinct ascending hex digits when<br />
converted to base 16? <br />
<br />
'''Solution:''' There are 13 numbers that have ascending digits in both bases from 100<br />
to 200. They are (in base 10):<br />
123 (7B), 124, 125, 126, 127 (7F), 137 (89), 138, 139 (8B), 156 (9C), 157, 158, 159 (9F), 189 (BD)<br />
<br />
== Video Resources ==<br />
<br />
There are many YouTube videos about computer number systems. Here are a handful that cover the topic nicely, without too many ads:<br />
<br />
{|<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/aW3qCcH6Dao</youtube><br />
| [https://youtu.be/aW3qCcH6Dao ''Number Systems - Converting Decimal, Binary and Hexadecimal'' ('''Joe James''')]<br />
<br />
An introduction to number systems, and how to convert between decimal, binary and hexadecimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/m1JtWKuTLR0</youtube><br />
| [https://youtu.be/m1JtWKuTLR0 ''Lesson 2.3 : Hexadecimal Tutorial'' ('''Carl Herold''')]<br />
<br />
The video focuses on hexadecimal numbers: their relationship to binary numbers and how to convert to decimal numbers. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/WR67syBDzew</youtube><br />
| [https://youtu.be/WR67syBDzew ''Hexes and the Magic of Base 16 - Vaidehi Joshi - May 2017' ('''DonutJS''')]<br />
<br />
A fun introduction to hexadecimal numbers, focusing a bit <br />
on using hex numbers for specifying RGB colors. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jvx-NrILgpE</youtube><br />
| [https://youtu.be/jvx-NrILgpE ''Collins Lab: Binary & Hex'' ('''Adafruit Industries''')]<br />
<br />
A professionally produced video that explains the number systems, how and why binary numbers are fundamental to computer science, and why hexadecimal is important to computer programmers. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Boolean_Algebra&diff=824Boolean Algebra2020-09-01T21:25:29Z<p>Marc: /* Why is Boolean Algebra Important for ACSL Students? */</p>
<hr />
<div>''Boolean algebra'' is the branch of algebra in which the values of the variables and constants have exactly two values: ''true'' and ''false'', usually denoted 1 and 0 respectively. <br />
<br />
== Operators ==<br />
<br />
The basic operators in Boolean algebra are '''AND''', '''OR''', and '''NOT'''. The secondary operators are ''eXclusive OR'' (often called '''XOR''') and ''eXclusive NOR'' ('''XNOR''', sometimes called '''equivalence'''). They are secondary in the sense that they <br />
can be composed from the basic operators.<br />
<br />
*The '''AND''' of two values is true only whenever both values are true. It is written as $xy$ or $x \cdot y$. The values of ''and'' for all possible inputs is shown in the following truth table:<br />
::{| class="wikitable" style="text-align: center"<br />
|-<br />
!<math>\ \ \ \ x\ \ \ \ </math><br />
!<math>\ \ \ \ y\ \ \ \ </math><br />
!<math>\ x y\ </math><br />
|-<br />
!0<br />
!0<br />
| 0 <br />
|-<br />
!0<br />
!1<br />
| 0 <br />
|-<br />
!1<br />
!0<br />
| 0 <br />
|-<br />
!1<br />
!1<br />
| 1 <br />
|} <br />
<br />
* The '''OR''' of two values is true whenever either or both values are true. It is written as $x+y$. The values of ''or'' for all possible inputs is shown in the following truth table:<br />
::{| class="wikitable" style="text-align: center"<br />
|-<br />
!<math>\ \ \ \ x\ \ \ \ </math><br />
!<math>\ \ \ \ y\ \ \ \ </math><br />
!<math>\ x + y\ </math><br />
|-<br />
!0<br />
!0<br />
| 0 <br />
|-<br />
!0<br />
!1<br />
| 1 <br />
|-<br />
!1<br />
!0<br />
| 1 <br />
|-<br />
!1<br />
!1<br />
| 1 <br />
|} <br />
<br />
* The '''NOT''' of a value is its opposite; that is, the ''not'' of a true value is false whereas the ''not'' of a false value is true. It is written as $\overline{x}$ or $\neg{x}$. The values of ''not'' for all possible inputs is shown in the following truth table:<br />
::{| class="wikitable" style="text-align: center"<br />
|-<br />
!<math>\ \ \ \ x\ \ \ \ </math><br />
!<math>\ \ \ \ \overline{x}\ \ \ \ </math><br />
|-<br />
!0<br />
| 1 <br />
|-<br />
!1<br />
| 0 <br />
|} <br />
<br />
* The '''XOR''' of two values is true whenever the values are different. It uses the $\oplus$ operator, and can be built from the basic operators: $x \oplus y = x \overline{y} + \overline{x} y$ The values of ''xor'' for all possible inputs is shown in the following truth table:<br />
::{| class="wikitable" style="text-align: center"<br />
|-<br />
!<math>\ \ \ \ x\ \ \ \ </math><br />
!<math>\ \ \ \ y\ \ \ \ </math><br />
!<math>\ x \oplus y\ </math><br />
|-<br />
!0<br />
!0<br />
| 0 <br />
|-<br />
!0<br />
!1<br />
| 1 <br />
|-<br />
!1<br />
!0<br />
| 1 <br />
|-<br />
!1<br />
!1<br />
| 0<br />
|} <br />
<br />
*The '''XNOR''' of two values is true whenever the values are the same. It is the '''NOT''' of the '''XOR''' function. It uses the $\odot$ operator: $x \odot y = \overline{x \oplus y}$. The ''xnor'' can be built from basic operators: $x \odot y = x y + \overline{x} \overline{y}$ The values of ''xnor'' for all possible inputs is shown in the following truth table:<br />
::{| class="wikitable" style="text-align: center"<br />
|-<br />
!<math>\ \ \ \ x\ \ \ \ </math><br />
!<math>\ \ \ \ y\ \ \ \ </math><br />
!<math>\ x \odot y\ </math><br />
|-<br />
!0<br />
!0<br />
| 1 <br />
|-<br />
!0<br />
!1<br />
| 0<br />
|-<br />
!1<br />
!0<br />
| 0<br />
|-<br />
!1<br />
!1<br />
| 1<br />
|} <br />
<br />
Just as algebra has basic rules for simplifying and evaluating expressions, so does Boolean algebra.<br />
<br />
== Why is Boolean Algebra Important for ACSL Students? ==<br />
<br />
Boolean algebra is important to programmers, computer scientists, and the general population. <br />
<br />
*For programmers, Boolean expressions are used for conditionals and loops. For example, the following snippet of code sums the even numbers that are not also multiples of 3, stopping when the sum hits 100:<br />
<br />
<dl><br />
<dd><br />
<syntaxhighlight lang="python"><br />
s = 0<br />
x = 1<br />
while (s < 100):<br />
if (x % 2 == 0) and (x % 3 != 0)<br />
then s = s + x<br />
x = x + 1<br />
</syntaxhighlight><br />
Both the conditional statement <br />
<syntaxhighlight lang="python" inline>s < 100</syntaxhighlight><br />
and the Boolean expression with 2 conditional statements <br />
<syntaxhighlight lang="python" inline>(x % 2 == 0) and (x % 3 != 0)</syntaxhighlight><br />
evaluate to ''true'' or ''false''. <br />
</dd><br />
</dl><br />
<br />
*For computer scientists, Boolean algebra is the basis for digital circuits that make up a computer's hardware. The [[Digital Electronics]] category concerns a graphical representation of a circuit. That circuit is typically easiest to understand and evaluate by converting it to its Boolean algebra representation. <br />
<br />
* The general population uses Boolean algebra, probably without knowing that they are doing so, when they enter search terms in Internet search engines. For example, the search expression ''jaguar speed -car'' is parsed by the search engine as the Boolean expression <syntaxhighlight lang="python" inline>"jaguar" and "car" and not "speed"</syntaxhighlight>; it returns pages about the speed of the jaguar animal, not the Jaguar car.<br />
<br />
==Laws==<br />
A '''law''' of Boolean algebra is an identity such as <math>x + (y + z) = (x + y) + z</math><br />
between two Boolean terms, where a '''Boolean term''' is defined as an expression built up from variables, the constants 0 and 1, and operations ''and'', ''or'', ''not'', ''xor'', and ''xnor''. <br />
<br />
Like ordinary algebra, parentheses are used to group terms. When a ''not'' is represented with an overhead horizontal line, there is an implicit grouping of the terms under the line. That is, $x \cdot \overline{y + z}$ is evaluated as if it were written $x \cdot \overline{(y + z)}.$<br />
<br />
=== Order of Precedence ===<br />
<br />
The order of operator precedence is ''not''; then ''and''; then ''xor'' and ''xnor''; and finally ''or''. Operators with the same level of precedence are evaluated from left-to-right. <br />
<br />
=== Fundamental Identities ===<br />
<br />
{| class="wikitable" style="text-align: center"<br />
|- <br />
| style="text-align: left" | Commutative Law – The order of application of two separate terms is not important. || $x+y = y+x$ || $x \cdot y = y \cdot x$ <br />
|- <br />
| style="text-align: left" | Associative Law – Regrouping of the terms in an expression doesn't change the value of the expression. ||$(x + y) + z$ = $x + (y + z)$ || $x \cdot (y \cdot z) = (x \cdot y) \cdot z$ <br />
|-<br />
| style="text-align: left" | Idempotent Law – A term that is ''or'''´ed or ''and''´ed with itself is equal to that term. || $ x +x = x $ || $x \cdot x = x$<br />
|-<br />
| style="text-align: left" | Annihilator Law – A term that is ''or'''´ed with 1 is 1; a term ''and''´ed with 0 is 0. || $x + 1 = 1$ || $ x \cdot 0 = 0 $ <br />
|-<br />
| style="text-align: left" | Identity Law – A term ''or''´ed 0 or ''and''´ed with a 1 will always equal that term. || $x + 0 = x$ || $x \cdot 1 = x$<br />
|-<br />
| style="text-align: left" | Complement Law – A term ''or''´ed with its complement equals 1 and a term ''and''´ed with its complement equals 0. || $x + \overline{x} = 1 $ || $x \cdot \overline{x} = 0$<br />
|-<br />
| style="text-align: left" | Absorptive Law – Complex expressions can be reduced to a simpler ones by absorbing like terms. ||colspan=2|<br />
$x+x y = x$<br />
<br />
$ x +\overline{x}y = x + y $<br />
<br />
$x (x+y) = x$<br />
<br />
|-<br />
| style="text-align: left" | Distributive Law – It's OK to multiply or factor-out an expression.||colspan=2|<br />
<br />
$x \cdot (y + z) = xy + xz$<br />
<br />
$(x+y) \cdot (p + q) = xp + xq +yp + yq$<br />
<br />
$(x+y)(x+z)=x + yz $<br />
<br />
|-<br />
| style="text-align: left" | DeMorgan's Law – An ''or'' (''and'') expression that is negated is equal to the ''and'' (''or'') of the negation of each term.|| $\overline{x+y} = \overline{x} \cdot \overline{y}$||$\overline{x \cdot y} = \overline{x} + \overline{y}$<br />
|- <br />
| style="text-align: left" | Double Negation – A term that is inverted twice is equal to the original term.||colspan=2| $\overline{\overline{x}} = x $ <br />
|- <br />
| style="text-align: left" | Relationship between XOR and XNOR|| colspan=2 | $ x\odot y = \overline{x\oplus y} = x \oplus \overline{y} =\overline{x} \oplus {y}$ <br />
|}<br />
<br />
== Sample Problems ==<br />
<br />
Problems in this category are typically of the form "Given a Boolean expression, simplify it as much as possible" or "Given a Boolean expression,<br />
find the values of all possible inputs that make the expression ''true''." Simplify means writing an equivalent expression using the fewest number of operators.<br />
<br />
=== Problem 1: Simplify the Expression ===<br />
<br />
'''Problem:''' Simplify the following expression as much as possible:<br />
$ \overline{ \overline{A(A+B)} + B\overline{A}}$<br />
<br />
'''Solution:'''<br />
<br />
The simplification proceeds as follows:<br />
<br />
:$\overline{ \overline{A(A+B)} + B\overline{A}}$<br />
::{| <br />
<br />
|-<br />
| <math>= \left(\overline{ \overline{A(A+B)}}\right) \cdot \left(\overline{ B\overline{A}}\right)</math> ||<br />
| (DeMorgan's Law)<br />
<br />
|-<br />
| <math>= \left(A(A+B)\right) \cdot \left( \overline{B}+\overline{\overline{A}}\right)</math> ||<br />
| (Double Negation; DeMorgan's Law)<br />
<br />
|-<br />
| <math>= A \cdot \left( \overline{B}+A\right)</math> ||<br />
| (Absorption; Double Negation)<br />
<br />
|-<br />
| <math>=A</math> || <br />
| (Absorption)<br />
| <br />
<br />
|}<br />
<br />
=== Problem 2: Find Solutions ===<br />
<br />
'''Problem:''' Find all orderd pairs $(A,B)$ that make the following expression ''true'':<br />
$ \overline{ \overline{(A+B)} + \overline{A}B }$<br />
<br />
'''Solution:'''<br />
<br />
There are typically two approaches to solving this type of problem. One approach is to simplify the expression as much as possible, until<br />
it's obvious what the solutions are. The other approach is to create a truth table of all possible inputs, with columns for each subexpression.<br />
<br />
The simplification approach is as following:<br />
:$ \overline{\overline{(A+B)} + \overline{A}B}$<br />
::$= \overline{\overline{A+B}} \cdot \overline{\overline{A}B}$ <br />
::$= (A+B) \cdot (\overline{\overline{A}}+\overline{B} ) $ <br />
::$= (A+B) \cdot (A+\overline{B}) $ <br />
::$= AA + A\overline{B} + BA + B\overline{B} $ <br />
::$= A + A(\overline{B} + B) + 0 $ <br />
::$= A + A(1)$ <br />
::$= A + A$ <br />
::$=A$<br />
This means that all inputs are valid whenever $A$ is ''true'': $(1,0)$ and $(1,1)$<br />
<br />
The truth table approach is as following. Each column is the result of a basic operation on two other columns. <br />
:{| class="wikitable" style="text-align: center"<br />
|-<br />
!style="background-color: #cceeff; font-size: x-small" |#1<br />
!style="background-color: #cceeff; font-size: x-small" |#2<br />
!style="background-color: #cceeff; font-size: x-small" |#3 <br />
!style="background-color: #cceeff; font-size: x-small" |#4<br />
!style="background-color: #cceeff; font-size: x-small" |#5<br />
!style="background-color: #cceeff; font-size: x-small" |#6<br />
!style="background-color: #cceeff; font-size: x-small" |#7<br />
!style="background-color: #cceeff; font-size: x-small" |#8<br />
<br />
|-<br />
|style="background-color: #cceeff"|<br />
|style="background-color: #cceeff"|<br />
!style="background-color: #cceeff; font-size: x-small" |OR of Col#1, Col#2<br />
!style="background-color: #cceeff; font-size: x-small" |NOT of Col#3<br />
!style="background-color: #cceeff; font-size: x-small" |NOT of Col#1<br />
!style="background-color: #cceeff; font-size: x-small" |ADD of Col#1, Col#2<br />
!style="background-color: #cceeff; font-size: x-small" |OR of Col#4, Col#6<br />
!style="background-color: #cceeff; font-size: x-small" |NOT of Col#7 <br />
<br />
|-<br />
!<math>A</math><br />
!<math>B</math><br />
!<math>A+B</math><br />
!<math>\overline{A+B}</math><br />
!<math>\overline{A}</math><br />
!<math>\overline{A}B</math><br />
!<math>\overline{A+B} + \overline{A}B</math><br />
!<math>\overline{\overline{A+B} + \overline{A}B}</math><br />
<br />
|-<br />
!0<br />
!0<br />
|0<br />
|1<br />
|1<br />
|0<br />
|1<br />
!0<br />
<br />
|-<br />
!0<br />
!1<br />
<br />
|1<br />
|0<br />
|1<br />
|1<br />
|1<br />
!0<br />
<br />
|-<br />
!1<br />
!0<br />
<br />
|1<br />
|0<br />
|0<br />
|0<br />
|0<br />
!1<br />
<br />
|-<br />
!1<br />
!1<br />
<br />
|1<br />
|0<br />
|0<br />
|0<br />
|0<br />
!1<br />
|}<br />
<br />
The rightmost column is the expression we are solving; it is ''true'' for the 3rd and 4th rows, where the inputs are $(1,0)$ and $(1,1)$.<br />
<br />
== Online Resources ==<br />
<br />
===Websites===<br />
A great online tutorial on Boolean Algebra is part of [https://ryanstutorials.net/boolean-algebra-tutorial/ Ryan's Tutorials].<br />
<br />
===Videos===<br />
The following YouTube videos show ACSL students and advisors working out some previous problems. To access the YouTube page with the video, click on the title of the video in the icon. (You can also play the video directly by clicking on the arrow in the center of the image; however, you'll <br />
probably want to have a larger viewing of the video since it contains writing on a whiteboard.) Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/1cwO-FtybNw</youtube><br />
| [https://youtu.be/1cwO-FtybNw ''ACSL Prep - Mrs. Gupta - Boolean Algebra'' ('''MegaChristian5555''')]<br />
<br />
Christian is a student participating in ACSL. This video shows how to solve a half-dozen or so Boolean Algebra problems that have appeared in ACSL contests in recent years in the Intermediate and Senior divisions.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/HJdhEjpVYsY</youtube><br />
| [https://youtu.be/HJdhEjpVYsY ''ACSL Boolean Algebra Contest 2 Worksheet 1'' ('''misterminich''')]<br />
<br />
Mr. Minich is an ACSL advisor. This video was one of two he created to help prepare his students for the ACSL Boolean algebra category. It shows solutions to 5 different problems that have<br />
appeared in recent years. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/4io6xgz8Zwk</youtube><br />
| [https://youtu.be/4io6xgz8Zwk ''ACSL Boolean Algebra Contest 2 Worksheet 2'' ('''misterminich''') ]<br />
<br />
Mr. Minich is an ACSL advisor. This video was one of two he created to help prepare his students for the ACSL Boolean algebra category. It shows solutions to 5 different problems that have<br />
appeared in recent years. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/6vI1mO1XOjQ</youtube><br />
| [https://youtu.be/6vI1mO1XOjQ ''ACSL 3 13-14 #1 - AM'' ('''Gordon Campbell''')]<br />
<br />
This video walks through the solution to finding all ordered triples that make the following<br />
Boolean expression ''true'':<br />
:<math><br />
(AB+\overline{C})(\overline{A}+BC)(A+\overline{B}+C)<br />
</math><br />
This problem appeared in 2013-2014 in the Senior Division, Contest #3.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/KRKTbAZYlLM</youtube><br />
| [https://youtu.be/KRKTbAZYlLM ''ACSL 3 #1 14-15 - AM'' ('''Gordon Campbell''')]<br />
<br />
This video walks through the simplification of the expression:<br />
:<math><br />
\overline{(\overline{A}+B)}(B+C)\overline{(A+\overline{C})}(A\overline{B}+BC)<br />
</math><br />
This problem appeared in 2014-2015 in the Senior Division, Contest #3.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jDnni-zm2g8</youtube><br />
| [https://youtu.be/jDnni-zm2g8 ''A general tutorial on boolean algebra that can be used for American Computer Science League.'' ('''Tangerine Code''')]<br />
<br />
Walks through the simplification of the following Boolean expression:<br />
:<math><br />
\overline{ (\overline{A + \overline{B}})(AB)} +<br />
\overline{ (A+B)(\overline{\overline{A}B})}<br />
</math><br />
<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/aGJALO57X7o</youtube><br />
| [https://youtu.be/aGJALO57X7o ''Boolean Algebra by Ravi Yeluru'' ]<br />
<br />
Walks through solving a handful of ACSL problems.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/-Iagy51n5bQ</youtube><br />
| [https://youtu.be/-Iagy51n5bQ ''Boolean Algebra by Ravi Yeluru'' ]<br />
<br />
Walks through solving 4 ACSL problems.<br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Main_Page&diff=823Main Page2020-09-01T21:20:13Z<p>Marc: </p>
<hr />
<div>Welcome to the wiki describing the topics covered in the short programs section of the [//www.acsl.org ACSL] contests.<br />
<br />
If you'd like to contribute to this wiki - and we'd love to improve it - please shoot us an email requesting an account. Conversely, if we've linked to your material (especially YouTube videos) that you'd prefer that we do not reference, let us know and we will promptly remove those links.<br />
<br />
The categories covered during the regular season in the Classroom, Junior, Intermediate, and Senior Divisions are as follows:<br />
<br />
* [[Assembly Language Programming]] <br />
* [[Bit-String Flicking]] <br />
* [[Boolean Algebra]]<br />
* [[Computer Number Systems]]<br />
* [[Data Structures]]<br />
* [[Digital Electronics]] <br />
* [[FSAs and Regular Expressions]] <br />
* [[Graph Theory]]<br />
* [[LISP]] <br />
* [[Prefix/Infix/Postfix Notation]]<br />
* [[Recursive Functions]]<br />
* [[What Does This Program Do?]]<br />
<br />
The categories covered during the regular season in the Elementary Division are as follows:<br />
<br />
* [//www.acsl.org/categories/C1Elem-ComputerNumberSystems.pdf Elementary Division: Computer Number Systems (Contest 1)]<br />
* [//www.acsl.org/categories/C2Elem-Prefix-Postfix-InfixNotation.pdf Elementary Division: Prefix-Postfix-Infix Notation (Contest 2)]]<br />
* [//www.acsl.org/categories/C3Elem-BooleanAlgebra.pdf Elementary Division: Boolean Algebra (Contest 3)]<br />
* [//www.acsl.org/categories/C4Elem-GraphTheory.pdf Elementary Division: Graph Theory (Contest 4)]<br />
<br />
The categories covered during the end-of-season invitational competition are as follows:<br />
<br />
* [[Advanced Regular Expressions]] <br />
* [[Karnaugh Maps]] <br />
<br />
== Getting started ==<br />
* [//meta.wikimedia.org/wiki/Help:Contents User's Guide] for information on using the wiki software<br />
* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings Configuration settings list]<br />
* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ MediaWiki FAQ]</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Main_Page&diff=822Main Page2020-09-01T21:04:28Z<p>Marc: reorganized the list of categories into: regular season CL,JR,INT, SR ; regular eason EL; and all-star</p>
<hr />
<div>Welcome to the wiki describing the topics covered in the short programs section of the [//www.acsl.org ACSL] contests.<br />
<br />
If you'd like to contribute to this wiki - and we'd love to improve it - please shoot us an email requesting an account. Conversely, if we've linked to your material (especially YouTube videos) that you'd prefer that we do not reference, let us know and we will promptly remove those links.<br />
<br />
Categories covered during the regular season in the Classroom, Junior, Intermediate, and Senior Divisions:<br />
<br />
* [[Assembly Language Programming]] <br />
* [[Bit-String Flicking]] <br />
* [[Boolean Algebra]]<br />
* [[Computer Number Systems]]<br />
* [[Data Structures]]<br />
* [[Digital Electronics]] <br />
* [[FSAs and Regular Expressions]] <br />
* [[Graph Theory]]<br />
* [[LISP]] <br />
* [[Prefix/Infix/Postfix Notation]]<br />
* [[Recursive Functions]]<br />
* [[What Does This Program Do?]]<br />
<br />
Categories covered during the regular season in the Elementary Division:<br />
<br />
* [//www.acsl.org/categories/C1Elem-ComputerNumberSystems.pdf Elementary Division: Computer Number Systems (Contest 1)]<br />
* [//www.acsl.org/categories/C2Elem-Prefix-Postfix-InfixNotation.pdf Elementary Division: Prefix-Postfix-Infix Notation (Contest 2)]]<br />
* [//www.acsl.org/categories/C3Elem-BooleanAlgebra.pdf Elementary Division: Boolean Algebra (Contest 3)]<br />
* [//www.acsl.org/categories/C4Elem-GraphTheory.pdf Elementary Division: Graph Theory (Contest 4)]<br />
<br />
Categories covered during the end-of-season invitational competition:<br />
<br />
* [[Advanced Regular Expressions]] <br />
* [[Karnaugh Maps]] <br />
<br />
== Getting started ==<br />
* [//meta.wikimedia.org/wiki/Help:Contents User's Guide] for information on using the wiki software<br />
* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings Configuration settings list]<br />
* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ MediaWiki FAQ]</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Graph_Theory&diff=821Graph Theory2020-09-01T20:49:48Z<p>Marc: wordsmithing</p>
<hr />
<div>Many problems are naturally formulated in terms of points and connections between them. For example, a computer network has PCs connected by cables, an airline map has cities connected by routes, and a school has rooms connected by hallways. A graph is a mathematical object which models such situations.<br />
<br />
== Overview ==<br />
<br />
A ''graph'' is a collection of vertices and edges. An ''edge'' is a connection between two ''vertices'' (sometimes referred to as ''nodes''). One can draw a graph by marking points for the vertices and drawing lines connecting them for the edges, but the graph is defined independently of the visual representation. For example, the following two drawings represent the same graph:<br />
<br />
::[[File:Graph fig1a.svg|150px]] <span style="display:inline-block; width: 5em;"></span>[[File:Graph fig1b.svg|150px]]<br />
<br />
The precise way to represent this graph is to identify its set of vertices {A, B, C, D, E, F, G}, and its set of edges between these vertices {AB, AD, BD, CF, FG, GH, GE, HE}.<br />
<br />
== Terminology ==<br />
<br />
The edges of the above graph have no directions meaning that the edge from one vertex A to another vertex B is the same as from vertex B to vertex A. Such a graph is called an ''undirected graph''. Similarly, a graph having a direction associated with each edge is known as a ''directed graph''.<br />
<br />
A ''path'' from vertex x to y in a graph is a list of vertices, in which successive vertices are connected by edges in the graph. For example, FGHE is path from F to E in the graph above. A ''simple path'' is a path with no vertex repeated. For example, FGHEG is not a simple path. <br />
<br />
A graph is ''connected'' if there is a path from every vertex to every other vertex in the graph. Intuitively, if the vertices were physical objects and the edges were strings connecting them, a connected graph would stay in one piece if picked up by any vertex. A graph which is not connected is made up of ''connected components''. For example, the graph above has two connected components: {A, B, D} and {C, E, F, G, H}.<br />
<br />
A ''cycle'' is a path, which is simple except that the first and last vertex are the same (a path from a point back to itself). For example, the path HEGH is a cycle in our example. Vertices must be listed in the order that they are traveled to make the path; any of the vertices may be listed first. Thus, HEGH and EHGE are different ways to identify the same cycle. For clarity, we list the start / end vertex twice: once at the start of the cycle and once at the end.<br />
<br />
We’ll denote the number of vertices in a given graph by V and the number of edges by E. Note that E can range anywhere from $V$ to $V^2$ (or $V(V-1)/2$ in an undirected graph). Graphs with all edges present are called ''complete graphs''; graphs with relatively few edges present (say less than $V log(V)$) are called ''sparse''; graphs with relatively few edges missing are called ''dense.''<br />
<br />
== Directed Graphs ==<br />
<br />
''Directed graphs'' are graphs which have a direction associated with each edge. An edge xy in a directed graph can be used in a path that goes from x to y but not necessarily from y to x. For example, a directed graph similar to our example graph is drawn below:<br />
<br />
[[File:Graph fig1a directed.svg|150px]]<br />
<br />
This graph is defined as the set of vertices V = {A,B,C,D,E,F,G,H} and the set of edges {AB,AD,DA,DB,EG,GE,HG,HE,GF,CF,FC}. There is one directed path from G to C (namely, GFC); however, there are no directed paths from C to G. Note that a few of the edges have arrows on both ends, such as the edge between A and D. These dual arrows indicate that there is an edge in each direction, which essentially makes an undirected edge. An ''undirected graph'' can be thought of as a directed graph with all edges occurring in pairs in this way. A directed graph with no cycles is called a ''dag'' (directed acyclic graph).<br />
<br />
== Trees & Forests ==<br />
<br />
A graph with no cycles is called a ''tree''. There is only one path between any two nodes in a tree. A tree with $N$ vertices contains exactly $N-1$ edges.<br />
<br />
[[File:forest.png|500px]]<br />
<br />
The two graphs shown above are trees because neither has any cycles and all vertices are connected. The graph <br />
on the left has 4 vertices and 3 edges; the graph on the right has 8 vertices and 7 edges. Note that in both cases, because they <br />
are trees, <br />
the number of edges is one less than the number of vertices. <br />
<br />
A group of disconnected trees is called a ''forest''.<br />
<br />
A ''weighted graph'' is a graph that has a weight (also referred to as a cost) associated with each edge. <br />
For example, in a graph <br />
used by airlines where cities are vertices and edges are cities with direct flights connecting them,<br />
the weight for each edge might be the distance between the cities. <br />
<br />
A ''spanning tree'' of a graph is a subgraph that contains all the vertices and forms a tree. <br />
A ''minimal spanning tree''<br />
can be found for weighted graphs<br />
in order to minimize the cost across an entire network.<br />
<br />
== Adjacency Matrices ==<br />
<br />
It is frequently convenient to represent a graph by a matrix known as an ''adjacency matrix.'' Consider<br />
the following directed graph:<br />
<br />
[[File:DirectedGraph.png|200px]]<br />
<br />
To draw the adjacency matrix, we create an $N$ by $N$ grid and label the rows and columns for each vertex (diagram at the left). Then, place a 1 for each edge in the cell whose row and column correspond to the<br />
starting and ending vertices of the edge (diagram in the middle). Finally, place <br />
a 0 in all other cells (diagram at the right). <br />
<br />
[[File:AdjMatrix.png|600px]]<br />
<br />
By construction, cell $(i,j)$ in the matrix with a value of 1 indicates a direct path from vertex $i$ to vertex $j$. <br />
If we square the <br />
matrix, the value in cell $(i,j)$ indicates the number of paths of length 2 from vertex $i$ to vertex $j$.<br />
<br />
[[File:AdjMatrix2.png|250px]]<br />
<br />
For example, the $M^2$ says that there 2 paths of length 2 from A to C (A → B → C and A → D → C). This also says that there is exactly 1 path of length 2 from A to D (A → B → D), exactly 1 path of length 2 from B to B (B → C → B), and so on.<br />
<br />
In general, if we raise $M$ to the ''pth'' power, the resulting matrix indicates which paths of length $p$ exist in the graph. <br />
The value in $M^p(i,j)$ is the number of paths from vertex $i$ to vertex $j$.<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
Find the number of different cycles contained in the directed graph with vertices {A, B, C, D, E} and edges {AB, BA, BC, CD, DC, DB, DE}.<br />
<br />
'''Solution:'''<br />
The graph is as follows:<br />
<br />
::[[File:graph sample1.svg|196px]]<br />
<br />
By inspection, the cycles are: ABA, BCDB, and CDC. Thus, there are 3 cycles in the graph.<br />
<!--<br />
=== Sample Problem 2 ===<br />
<br />
Given the directed graph below, how many more paths of length 3 would exist if edges AE and EA were added?<br />
<br />
[[File:graph_s1.png]]<br />
<br />
'''Solution:'''<br />
<br />
By using adjacency matrices, the current one is<br />
<br />
[[File:matrix1_s2.png]]<br />
<br />
After adding edges AE and EA, the matrices are:<br />
<br />
[[File:matrix2_s2.png]]<br />
<br />
There are 6 more paths of length 2 since 16 – 10 = 6.<br />
--><br />
<br />
=== Problem 2===<br />
<br />
In the following directed graph, find the total number of different paths from vertex A to vertex C of length 2 or 4.<br />
<br />
::[[File:graph sample3.svg|128px]]<br />
<br />
'''Solution:'''<br />
<br />
Let matrix $M$ represent the graph. Recall that the number of paths from vertex $i$ to vertex $j$ of length $p$ equals $M^p[i,j]$. The values of $M$, $M^2$ and $M^4$ are:<br />
<br />
[[File:matrix_s3.png]]<br />
<br />
There is 1 path of length 2 from A to C (cell [1,3] in $M^2$). By inspection, the only path of length 2 is A → A → C.<br />
<br />
There are 3 paths of length 4 (cell [1,3] in $M^4$) and they are A → A → A → A → C, A → A → C → A → C, A → C → A → A → C.<br />
<br />
=== Problem 3 ===<br />
<br />
Given the adjacency matrix, draw the directed graph.<br />
<br />
[[File:matrix_s4.png]]<br />
<br />
'''Solution:'''<br />
<br />
There must be exactly 4 vertices: V = {A, B, C, D}. There must be be exactly 7 edges: E = {AB, AD, BA, BD, CA, DB, DC}. Here are two valid drawings of the graph:<br />
<br />
: [[File:Graph sample4soln-a.svg|128px]] <span style="display:inline-block; width: 5em;"></span>[[File:Graph sample4soln-d.svg|128px]]<br />
<br />
== Video Resources ==<br />
<br />
===ACSL Videos===<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/pWCuRRqBGvw</youtube><br />
| [https://youtu.be/pWCuRRqBGvw ''ACSL Math: Graph Theory'' ('''Raj Joshi''')]<br />
<br />
This video introduces the topic, then using an example problem, explains the methodology to solve problems that appear on ACSL contests.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/H96Lo2q_FSk</youtube><br />
| [https://youtu.be/H96Lo2q_FSk ''ACSL - Graph Theory Worksheet 1'' ('''misterminich''')]<br />
<br />
Shows the solution to the ACSL problem: '''Draw the directed graph with vertices A, B, C, D, E and the directed edges AB, BC, AD, BC, DC, ED, and EA.''<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/y1fdIb_oNG0</youtube><br />
| [https://youtu.be/y1fdIb_oNG0 ''ACSL Graph Theory Worksheet 3'' ('''misterminich''')]<br />
<br />
Shows the solution to an ACSL problem asking to find how many paths of a specific length there are in a specific directed graph.<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/vtTCRMGB0K8</youtube><br />
| [https://youtu.be/vtTCRMGB0K8 ''Graph Theory ACSL Example Problem'' ('''Tangerine Code''')]<br />
<br />
Shows the solution to an ACSL problem asking to find how many different cycles there are from a specific vertex in a specific directed graph.<br />
|}<br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/ivhOH6crQ1w</youtube><br />
| [https://youtu.be/ivhOH6crQ1w ''ACSL Test Prep - Graph Theory'' ('''Mrs. Goopta''')]<br />
<br />
A talked-over presentation discussing graph theory as needed for the American Computer Science League and its tests.<br />
|}<br />
<br />
===Other Videos===<br />
<br />
There are dozens of YouTube videos that provide an introduction to graph theory. The following are a couple that we found particularly well done. Be aware that the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/44sQJK4BycY</youtube><br />
| [https://youtu.be/44sQJK4BycY ''CS 106B Lecture: Graphs: basic concepts'']<br />
<br />
This lecture from Stanford University is s wonderful introduction to graph theory. The lecture starts out with many examples of real world problems that are modeled by graphs, and then moves on to review basic terminology relating to graph theory. <br />
|-<br />
| <youtube width="300" height="180"> https://youtu.be/gXgEDyodOJU</youtube><br />
| [https://youtu.be/gXgEDyodOJU ''Data structures: Introduction to Graphs'' '''(mycodeschool)''']<br />
<br />
A nice introduction to the fundamental concepts of graphs. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Graph_Theory&diff=820Graph Theory2020-09-01T20:37:29Z<p>Marc: wordsmithing</p>
<hr />
<div>Many problems are naturally formulated in terms of points and connections between them. For example, a computer network has PCs connected by cables, an airline map has cities connected by routes, and a school has rooms connected by hallways. A graph is a mathematical object which models such situations.<br />
<br />
== Overview ==<br />
<br />
A ''graph'' is a collection of vertices and edges. An ''edge'' is a connection between two ''vertices'' (sometimes referred to as ''nodes''). One can draw a graph by marking points for the vertices and drawing lines connecting them for the edges, but the graph is defined independently of the visual representation. For example, the following two drawings represent the same graph:<br />
<br />
::[[File:Graph fig1a.svg|150px]] <span style="display:inline-block; width: 5em;"></span>[[File:Graph fig1b.svg|150px]]<br />
<br />
The precise way to represent this graph is to identify its set of vertices {A, B, C, D, E, F, G}, and its set of edges between these vertices {AB, AD, BD, CF, FG, GH, GE, HE}.<br />
<br />
== Terminology ==<br />
<br />
The edges of the above graph have no directions meaning that the edge from one vertex A to another vertex B is the same as from vertex B to vertex A. Such a graph is called an ''undirected graph''. Similarly, a graph having a direction associated with each edge is known as a ''directed graph''.<br />
<br />
A ''path'' from vertex x to y in a graph is a list of vertices, in which successive vertices are connected by edges in the graph. For example, FGHE is path from F to E in the graph above. A ''simple path'' is a path with no vertex repeated. For example, FGHEG is not a simple path. <br />
<br />
A graph is ''connected'' if there is a path from every vertex to every other vertex in the graph. Intuitively, if the vertices were physical objects and the edges were strings connecting them, a connected graph would stay in one piece if picked up by any vertex. A graph which is not connected is made up of ''connected components''. For example, the graph above has two connected components: {A, B, D} and {C, E, F, G, H}.<br />
<br />
A ''cycle'' is a path, which is simple except that the first and last vertex are the same (a path from a point back to itself). For example, the path HEGH is a cycle in our example. Vertices must be listed in the order that they are traveled to make the path; any of the vertices may be listed first. Thus, HEGH and EHGE are different ways to identify the same cycle. For clarity, we list the start / end vertex twice: once at the start of the cycle and once at the end.<br />
<br />
We’ll denote the number of vertices in a given graph by V and the number of edges by E. Note that E can range anywhere from $V$ to $V^2$ (or $V(V-1)/2$ in an undirected graph). Graphs with all edges present are called ''complete graphs''; graphs with relatively few edges present (say less than $V log(V)$) are called ''sparse''; graphs with relatively few edges missing are called ''dense.''<br />
<br />
== Directed Graphs ==<br />
<br />
''Directed graphs'' are graphs which have a direction associated with each edge. An edge xy in a directed graph can be used in a path that goes from x to y but not necessarily from y to x. For example, a directed graph similar to our example graph is drawn below:<br />
<br />
[[File:Graph fig1a directed.svg|150px]]<br />
<br />
This graph is defined as the set of vertices V = {A,B,C,D,E,F,G,H} and the set of edges {AB,AD,DA,DB,EG,GE,HG,HE,GF,CF,FC}. There is one directed path from G to C (namely, GFC); however, there are no directed paths from C to G. Note that a few of the edges have arrows on both ends, such as the edge between A and D. These dual arrows indicate that there is an edge in each direction, which essentially makes an undirected edge. An ''undirected graph'' can be thought of as a directed graph with all edges occurring in pairs in this way. A directed graph with no cycles is called a ''dag'' (directed acyclic graph).<br />
<br />
== Trees & Forests ==<br />
<br />
A graph with no cycles is called a ''tree''. There is only one path between any two nodes in a tree. A tree with $N$ vertices contains exactly $N-1$ edges.<br />
<br />
[[File:forest.png|500px]]<br />
<br />
The two graphs shown above are trees because neither has any cycles and all vertices are connected. The graph <br />
on the left has 4 vertices and 3 edges; the graph on the right has 8 vertices and 7 edges. Note that in both cases, because they <br />
are trees, <br />
the number of edges is one less than the number of vertices. <br />
<br />
A group of disconnected trees is called a ''forest''.<br />
<br />
A ''weighted graph'' is a graph that has a weight (also referred to as a cost) associated with each edge. <br />
For example, in a graph <br />
used by airlines where cities are vertices and edges are cities with direct flights connecting them,<br />
the weight for each edge might be the distance between the cities. <br />
<br />
A ''spanning tree'' of a graph is a subgraph that contains all the vertices and forms a tree. <br />
A ''minimal spanning tree''<br />
can be found for weighted graphs<br />
in order to minimize the cost across an entire network.<br />
<br />
== Adjacency Matrices ==<br />
<br />
It is frequently convenient to represent a graph by a matrix known as Adjacency Matrix. Let us understand using the 4 node directed graph below:<br />
<br />
[[File:DirectedGraph.png|200px]]<br />
<br />
To draw adjacency matrix, we put all nodes in rows and also arrange them in columns which gives a $4×4$ matrix (the left empty matrix). We then put 1 for all the edges considering their direction (see the middle one) and finally put 0 for all unfilled cells which gives us the adjacency matrix (the rightmost one, highlighted).<br />
<br />
[[File:AdjMatrix.png|600px]]<br />
<br />
By construction, each path of this matrix is of length 1. Now if we raise the power of matrix M to 2, we get:<br />
<br />
[[File:AdjMatrix2.png|250px]]<br />
<br />
And this says that there are 2 paths of length 2 from A to C. By examination we can see they are A → B → C and A → D → C. This also says that there is exactly 1 path of length 2 from A to D which is A → B → D, exactly 1 path of length 2 from B to B which is B → C → B and so on.<br />
<br />
If we raise M to the ''pth'' power, the resulting matrix indicates which paths of length ''p'' exist in the graph. The value in $M^p(i,j)$ is the number of paths from vertex ''i'' to vertex ''j''.<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
Find the number of different cycles contained in the directed graph with vertices {A, B, C, D, E} and edges {AB, BA, BC, CD, DC, DB, DE}.<br />
<br />
'''Solution:'''<br />
The graph is as follows:<br />
<br />
::[[File:graph sample1.svg|196px]]<br />
<br />
By inspection, the cycles are: ABA, BCDB, and CDC. Thus, there are 3 cycles in the graph.<br />
<!--<br />
=== Sample Problem 2 ===<br />
<br />
Given the directed graph below, how many more paths of length 3 would exist if edges AE and EA were added?<br />
<br />
[[File:graph_s1.png]]<br />
<br />
'''Solution:'''<br />
<br />
By using adjacency matrices, the current one is<br />
<br />
[[File:matrix1_s2.png]]<br />
<br />
After adding edges AE and EA, the matrices are:<br />
<br />
[[File:matrix2_s2.png]]<br />
<br />
There are 6 more paths of length 2 since 16 – 10 = 6.<br />
--><br />
<br />
=== Problem 2===<br />
<br />
In the following directed graph, find the total number of different paths from vertex A to vertex C of length 2 or 4.<br />
<br />
::[[File:graph sample3.svg|128px]]<br />
<br />
'''Solution:'''<br />
<br />
Let matrix $M$ represent the graph. Recall that the number of paths from vertex $i$ to vertex $j$ of length $p$ equals $M^p[i,j]$. The values of $M$, $M^2$ and $M^4$ are:<br />
<br />
[[File:matrix_s3.png]]<br />
<br />
There is 1 path of length 2 from A to C (cell [1,3] in $M^2$). By inspection, the only path of length 2 is A → A → C.<br />
<br />
There are 3 paths of length 4 (cell [1,3] in $M^4$) and they are A → A → A → A → C, A → A → C → A → C, A → C → A → A → C.<br />
<br />
=== Problem 3 ===<br />
<br />
Given the adjacency matrix, draw the directed graph.<br />
<br />
[[File:matrix_s4.png]]<br />
<br />
'''Solution:'''<br />
<br />
There must be exactly 4 vertices: V = {A, B, C, D}. There must be be exactly 7 edges: E = {AB, AD, BA, BD, CA, DB, DC}. Here are two valid drawings of the graph:<br />
<br />
: [[File:Graph sample4soln-a.svg|128px]] <span style="display:inline-block; width: 5em;"></span>[[File:Graph sample4soln-d.svg|128px]]<br />
<br />
== Video Resources ==<br />
<br />
===ACSL Videos===<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/pWCuRRqBGvw</youtube><br />
| [https://youtu.be/pWCuRRqBGvw ''ACSL Math: Graph Theory'' ('''Raj Joshi''')]<br />
<br />
This video introduces the topic, then using an example problem, explains the methodology to solve problems that appear on ACSL contests.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/H96Lo2q_FSk</youtube><br />
| [https://youtu.be/H96Lo2q_FSk ''ACSL - Graph Theory Worksheet 1'' ('''misterminich''')]<br />
<br />
Shows the solution to the ACSL problem: '''Draw the directed graph with vertices A, B, C, D, E and the directed edges AB, BC, AD, BC, DC, ED, and EA.''<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/y1fdIb_oNG0</youtube><br />
| [https://youtu.be/y1fdIb_oNG0 ''ACSL Graph Theory Worksheet 3'' ('''misterminich''')]<br />
<br />
Shows the solution to an ACSL problem asking to find how many paths of a specific length there are in a specific directed graph.<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/vtTCRMGB0K8</youtube><br />
| [https://youtu.be/vtTCRMGB0K8 ''Graph Theory ACSL Example Problem'' ('''Tangerine Code''')]<br />
<br />
Shows the solution to an ACSL problem asking to find how many different cycles there are from a specific vertex in a specific directed graph.<br />
|}<br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/ivhOH6crQ1w</youtube><br />
| [https://youtu.be/ivhOH6crQ1w ''ACSL Test Prep - Graph Theory'' ('''Mrs. Goopta''')]<br />
<br />
A talked-over presentation discussing graph theory as needed for the American Computer Science League and its tests.<br />
|}<br />
<br />
===Other Videos===<br />
<br />
There are dozens of YouTube videos that provide an introduction to graph theory. The following are a couple that we found particularly well done. Be aware that the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/44sQJK4BycY</youtube><br />
| [https://youtu.be/44sQJK4BycY ''CS 106B Lecture: Graphs: basic concepts'']<br />
<br />
This lecture from Stanford University is s wonderful introduction to graph theory. The lecture starts out with many examples of real world problems that are modeled by graphs, and then moves on to review basic terminology relating to graph theory. <br />
|-<br />
| <youtube width="300" height="180"> https://youtu.be/gXgEDyodOJU</youtube><br />
| [https://youtu.be/gXgEDyodOJU ''Data structures: Introduction to Graphs'' '''(mycodeschool)''']<br />
<br />
A nice introduction to the fundamental concepts of graphs. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Graph_Theory&diff=819Graph Theory2020-09-01T18:48:23Z<p>Marc: wordsmith 1st paragraph; used mathtype in last paragraph</p>
<hr />
<div>Many problems are naturally formulated in terms of points and connections between them. For example, a computer network has PCs connected by cables, an airline map has cities connected by routes, and a school has rooms connected by hallways. A graph is a mathematical object which models such situations.<br />
<br />
== Overview ==<br />
<br />
A ''graph'' is a collection of vertices and edges. An ''edge'' is a connection between two ''vertices'' (sometimes referred to as ''nodes''). One can draw a graph by marking points for the vertices and drawing lines connecting them for the edges, but the graph is defined independently of the visual representation. For example, the following two drawings represent the same graph:<br />
<br />
::[[File:Graph fig1a.svg|150px]] <span style="display:inline-block; width: 5em;"></span>[[File:Graph fig1b.svg|150px]]<br />
<br />
The precise way to represent this graph is to identify its set of vertices {A, B, C, D, E, F, G}, and its set of edges between these vertices {AB, AD, BD, CF, FG, GH, GE, HE}.<br />
<br />
== Terminology ==<br />
<br />
The edges of the above graph have no directions meaning that the edge from one vertex A to another vertex B is the same as from vertex B to vertex A. Such a graph is called an ''undirected graph''. Similarly, a graph having a direction associated with each edge is known as a ''directed graph''.<br />
<br />
A ''path'' from vertex x to y in a graph is a list of vertices, in which successive vertices are connected by edges in the graph. For example, FGHE is path from F to E in the graph above. A ''simple path'' is a path with no vertex repeated. For example, FGHEG is not a simple path. <br />
<br />
A graph is ''connected'' if there is a path from every vertex to every other vertex in the graph. Intuitively, if the vertices were physical objects and the edges were strings connecting them, a connected graph would stay in one piece if picked up by any vertex. A graph which is not connected is made up of ''connected components''. For example, the graph above has two connected components: {A, B, D} and {C, E, F, G, H}.<br />
<br />
A ''cycle'' is a path, which is simple except that the first and last vertex are the same (a path from a point back to itself). For example, the path HEGH is a cycle in our example. Vertices must be listed in the order that they are traveled to make the path; any of the vertices may be listed first. Thus, HEGH and EHGE are different ways to identify the same cycle. For clarity, we list the start / end vertex twice: once at the start of the cycle and once at the end.<br />
<br />
We’ll denote the number of vertices in a given graph by V and the number of edges by E. Note that E can range anywhere from $V$ to $V^2$ (or $V(V-1)/2$ in an undirected graph). Graphs with all edges present are called ''complete graphs''; graphs with relatively few edges present (say less than $V log(V)$) are called ''sparse''; graphs with relatively few edges missing are called ''dense.''<br />
<br />
== Directed Graphs ==<br />
<br />
''Directed graphs'' are graphs which have a direction associated with each edge. An edge xy in a directed graph can be used in a path that goes from x to y but not necessarily from y to x. For example, a directed graph similar to our example graph is drawn below:<br />
<br />
[[File:Graph fig1a directed.svg|150px]]<br />
<br />
This graph is defined as the set of vertices V = {A,B,C,D,E,F,G,H} and the set of edges {AB,AD,DA,DB,EG,GE,HG,HE,GF,CF,FC}. There is one directed path from G to C (namely, GFC); however, there are no directed paths from C to G. Note that a few of the edges have arrows on both ends, such as the edge between A and D. These dual arrows indicate that there is an edge in each direction, which essentially makes an undirected edge. An ''undirected graph'' can be thought of as a directed graph with all edges occurring in pairs in this way. A directed graph with no cycles is called a ''dag'' (directed acyclic graph).<br />
<br />
== Trees & Forest ==<br />
<br />
A graph with no cycles is called a ''tree''. There is only one path between any two nodes in a tree. A tree on N vertices contains exactly N-1 edges.<br />
<br />
[[File:forest.png|500px]]<br />
<br />
The graph on the left shown above is a tree because it has no cycles and vertices are connected. It has four vertices and three edges, i.e., for ''N'' vertices ''N-1'' edges as mentioned in the definition.<br />
<br />
A ''spanning tree'' of a graph is a subgraph that contains all the vertices and forms a tree. Minimal spanning trees can be found for weighted graphs (i.e. each edge has a cost or distance associated with it) in order to minimize the cost or distance across an entire network.<br />
<br />
A group of disconnected trees is called a ''forest''.<br />
<br />
== Adjacency Matrices ==<br />
<br />
It is frequently convenient to represent a graph by a matrix known as Adjacency Matrix. Let us understand using the 4 node directed graph below:<br />
<br />
[[File:DirectedGraph.png|200px]]<br />
<br />
To draw adjacency matrix, we put all nodes in rows and also arrange them in columns which gives a $4×4$ matrix (the left empty matrix). We then put 1 for all the edges considering their direction (see the middle one) and finally put 0 for all unfilled cells which gives us the adjacency matrix (the rightmost one, highlighted).<br />
<br />
[[File:AdjMatrix.png|600px]]<br />
<br />
By construction, each path of this matrix is of length 1. Now if we raise the power of matrix M to 2, we get:<br />
<br />
[[File:AdjMatrix2.png|250px]]<br />
<br />
And this says that there are 2 paths of length 2 from A to C. By examination we can see they are A → B → C and A → D → C. This also says that there is exactly 1 path of length 2 from A to D which is A → B → D, exactly 1 path of length 2 from B to B which is B → C → B and so on.<br />
<br />
If we raise M to the ''pth'' power, the resulting matrix indicates which paths of length ''p'' exist in the graph. The value in $M^p(i,j)$ is the number of paths from vertex ''i'' to vertex ''j''.<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
Find the number of different cycles contained in the directed graph with vertices {A, B, C, D, E} and edges {AB, BA, BC, CD, DC, DB, DE}.<br />
<br />
'''Solution:'''<br />
The graph is as follows:<br />
<br />
::[[File:graph sample1.svg|196px]]<br />
<br />
By inspection, the cycles are: ABA, BCDB, and CDC. Thus, there are 3 cycles in the graph.<br />
<!--<br />
=== Sample Problem 2 ===<br />
<br />
Given the directed graph below, how many more paths of length 3 would exist if edges AE and EA were added?<br />
<br />
[[File:graph_s1.png]]<br />
<br />
'''Solution:'''<br />
<br />
By using adjacency matrices, the current one is<br />
<br />
[[File:matrix1_s2.png]]<br />
<br />
After adding edges AE and EA, the matrices are:<br />
<br />
[[File:matrix2_s2.png]]<br />
<br />
There are 6 more paths of length 2 since 16 – 10 = 6.<br />
--><br />
<br />
=== Problem 2===<br />
<br />
In the following directed graph, find the total number of different paths from vertex A to vertex C of length 2 or 4.<br />
<br />
::[[File:graph sample3.svg|128px]]<br />
<br />
'''Solution:'''<br />
<br />
Let matrix $M$ represent the graph. Recall that the number of paths from vertex $i$ to vertex $j$ of length $p$ equals $M^p[i,j]$. The values of $M$, $M^2$ and $M^4$ are:<br />
<br />
[[File:matrix_s3.png]]<br />
<br />
There is 1 path of length 2 from A to C (cell [1,3] in $M^2$). By inspection, the only path of length 2 is A → A → C.<br />
<br />
There are 3 paths of length 4 (cell [1,3] in $M^4$) and they are A → A → A → A → C, A → A → C → A → C, A → C → A → A → C.<br />
<br />
=== Problem 3 ===<br />
<br />
Given the adjacency matrix, draw the directed graph.<br />
<br />
[[File:matrix_s4.png]]<br />
<br />
'''Solution:'''<br />
<br />
There must be exactly 4 vertices: V = {A, B, C, D}. There must be be exactly 7 edges: E = {AB, AD, BA, BD, CA, DB, DC}. Here are two valid drawings of the graph:<br />
<br />
: [[File:Graph sample4soln-a.svg|128px]] <span style="display:inline-block; width: 5em;"></span>[[File:Graph sample4soln-d.svg|128px]]<br />
<br />
== Video Resources ==<br />
<br />
===ACSL Videos===<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/pWCuRRqBGvw</youtube><br />
| [https://youtu.be/pWCuRRqBGvw ''ACSL Math: Graph Theory'' ('''Raj Joshi''')]<br />
<br />
This video introduces the topic, then using an example problem, explains the methodology to solve problems that appear on ACSL contests.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/H96Lo2q_FSk</youtube><br />
| [https://youtu.be/H96Lo2q_FSk ''ACSL - Graph Theory Worksheet 1'' ('''misterminich''')]<br />
<br />
Shows the solution to the ACSL problem: '''Draw the directed graph with vertices A, B, C, D, E and the directed edges AB, BC, AD, BC, DC, ED, and EA.''<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/y1fdIb_oNG0</youtube><br />
| [https://youtu.be/y1fdIb_oNG0 ''ACSL Graph Theory Worksheet 3'' ('''misterminich''')]<br />
<br />
Shows the solution to an ACSL problem asking to find how many paths of a specific length there are in a specific directed graph.<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/vtTCRMGB0K8</youtube><br />
| [https://youtu.be/vtTCRMGB0K8 ''Graph Theory ACSL Example Problem'' ('''Tangerine Code''')]<br />
<br />
Shows the solution to an ACSL problem asking to find how many different cycles there are from a specific vertex in a specific directed graph.<br />
|}<br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/ivhOH6crQ1w</youtube><br />
| [https://youtu.be/ivhOH6crQ1w ''ACSL Test Prep - Graph Theory'' ('''Mrs. Goopta''')]<br />
<br />
A talked-over presentation discussing graph theory as needed for the American Computer Science League and its tests.<br />
|}<br />
<br />
===Other Videos===<br />
<br />
There are dozens of YouTube videos that provide an introduction to graph theory. The following are a couple that we found particularly well done. Be aware that the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/44sQJK4BycY</youtube><br />
| [https://youtu.be/44sQJK4BycY ''CS 106B Lecture: Graphs: basic concepts'']<br />
<br />
This lecture from Stanford University is s wonderful introduction to graph theory. The lecture starts out with many examples of real world problems that are modeled by graphs, and then moves on to review basic terminology relating to graph theory. <br />
|-<br />
| <youtube width="300" height="180"> https://youtu.be/gXgEDyodOJU</youtube><br />
| [https://youtu.be/gXgEDyodOJU ''Data structures: Introduction to Graphs'' '''(mycodeschool)''']<br />
<br />
A nice introduction to the fundamental concepts of graphs. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Digital_Electronics&diff=657Digital Electronics2020-08-11T18:08:07Z<p>Marc: /* Sample Problem 3 */</p>
<hr />
<div>A digital circuit is constructed from logic gates. Each logic gate performs a function of boolean logic based on its inputs, such as AND or OR. <br />
Each circuit can be represented as a Boolean Algebra expression; <br />
this topic is an extension of the topic of [[Boolean Algebra]], which includes <br />
a thorough description of truth tables and simplifying expressions. <br />
<br />
= Definitions =<br />
<br />
The following table illustrates all logic gates. For each logic gate, the<br />
table shows the equivalent Boolean algebra expression and truth table.<br />
<br />
{| class="wikitable" style="text-align: left"<br />
|-<br />
!'''NAME'''<br />
!'''GRAPHICAL SYMBOL'''<br />
!'''ALGEBRAIC EXPRESSION'''<br />
!'''TRUTH TABLE'''<br />
|-<br />
!'''BUFFER'''<br />
| [[File:Buffer-gate-en.svg|128px]]<br />
| X = A<br />
|<br />
{| class="wikitable" style="text-align: center"<br />
|-<br />
|style="background-color: #cceeff; font-size: x-small" "|INPUT<br />
|style="background-color: #cceeff; font-size: x-small" "|OUTPUT<br />
|-<br />
| 0 || 0<br />
|-<br />
| 1 || 1<br />
|}<br />
|-<br />
!'''NOT'''<br />
| [[File:Not-gate-en.svg|128px]]<br />
| X = <math>\overline{A}</math> or <math>\neg A</math><br />
|<br />
{| class="wikitable" style="text-align: center"<br />
|-<br />
|style="background-color: #cceeff; font-size: x-small" "|INPUT<br />
|style="background-color: #cceeff; font-size: x-small" "|OUTPUT<br />
|-<br />
! A || X<br />
|-<br />
| 0 || 1<br />
|-<br />
| 1 || 0<br />
|}<br />
|-<br />
!'''AND'''<br />
| |[[File:And-gate.png|128px]]<br />
| X = <math>AB</math> or <math>A \cdot B</math> <br />
|<br />
{| class="wikitable" style="text-align: center"<br />
|-<br />
|colspan="2" style="background-color: #cceeff; font-size: x-small" "|INPUT<br />
|colspan="1" style="background-color: #cceeff; font-size: x-small" "|OUTPUT<br />
|-<br />
! A || B || X<br />
|-<br />
| 0 || 0 || 0<br />
|-<br />
| 0 || 1 || 0<br />
|-<br />
| 1 || 0 || 0<br />
|-<br />
| 1 || 1 || 1<br />
|}<br />
|-<br />
!'''NAND'''<br />
| [[File:Nand-gate-en.svg|128px]]<br />
| X = <math>\overline{AB}</math> or <math>\overline{A\cdot B}</math><br />
|<br />
{| class="wikitable" style="text-align: center"<br />
|-<br />
|colspan="2" style="background-color: #cceeff; font-size: x-small" "|INPUT<br />
|style="background-color: #cceeff; font-size: x-small" "|OUTPUT<br />
|-<br />
! A || B || X<br />
|-<br />
| 0 || 0 || 1<br />
|-<br />
| 0 || 1 || 1<br />
|-<br />
| 1 || 0 || 1<br />
|-<br />
| 1 || 1 || 0<br />
|}<br />
|-<br />
!'''OR'''<br />
| [[File:Or-gate-en.svg|128px]]<br />
| X = <math>A+B</math> <br />
|<br />
{| class="wikitable" style="text-align: center"<br />
|-<br />
|colspan="2" style="background-color: #cceeff; font-size: x-small" "|INPUT<br />
|style="background-color: #cceeff; font-size: x-small" "|OUTPUT<br />
|-<br />
! A || B || X<br />
|-<br />
| 0 || 0 || 0<br />
|-<br />
| 0 || 1 || 1<br />
|-<br />
| 1 || 0 || 1<br />
|-<br />
| 1 || 1 || 1<br />
|}<br />
|-<br />
!'''NOR'''<br />
| [[File:Nor-gate-en.svg|128px]]<br />
| X = <math>\overline{A+B}</math><br />
|<br />
{| class="wikitable" style="text-align: center"<br />
|-<br />
|colspan="2" style="background-color: #cceeff; font-size: x-small" "|INPUT<br />
|style="background-color: #cceeff; font-size: x-small" "|OUTPUT<br />
|-<br />
! A || B || X<br />
|-<br />
| 0 || 0 || 1<br />
|-<br />
| 0 || 1 || 0<br />
|-<br />
| 1 || 0 || 0<br />
|-<br />
| 1 || 1 || 0<br />
|}<br />
|-<br />
!'''XOR'''<br />
|[[File:Xor-gate-en.svg|128px]]<br />
| X = <math>A \oplus B</math><br />
|<br />
{| class="wikitable" style="text-align: center"<br />
|-<br />
|colspan="2" style="background-color: #cceeff; font-size: x-small" "|INPUT<br />
|style="background-color: #cceeff; font-size: x-small" "|OUTPUT<br />
|-<br />
! A || B || X<br />
|-<br />
| 0 || 0 || 0<br />
|-<br />
| 0 || 1 || 1<br />
|-<br />
| 1 || 0 || 1<br />
|-<br />
| 1 || 1 || 0<br />
|}<br />
|-<br />
!'''XNOR'''<br />
| [[File:Xnor-gate-en.svg|128px]]<br />
| X = <math>\overline{A \oplus B} \text{ or } A \odot B</math><br />
|<br />
{| class="wikitable" style="text-align: center"<br />
|-<br />
|colspan="2" style="background-color: #cceeff; font-size: x-small" "|INPUT<br />
|style="background-color: #cceeff; font-size: x-small" "|OUTPUT<br />
|-<br />
! A || B || X<br />
|-<br />
| 0 || 0 || 1<br />
|-<br />
| 0 || 1 || 0<br />
|-<br />
| 1 || 0 || 0<br />
|-<br />
| 1 || 1 || 1<br />
|}<br />
|}<br />
<br />
Note that there is some ambiguity in the conversion from a diagram to a circuit. For example, is <math>\overline{A+B}</math> an OR gate followed by a NOT gate, or smply<br />
a NOT gate.<br />
<br />
=Online Tools=<br />
<br />
The [http://www.cburch.com/logisim/index.html Logisim] application is a wonderful tool<br />
for exploring this topic.<br />
Logisim is free to download and use; among its many features is support to automatically draw<br />
a circuit from a Boolean Algebra expression; to simulate the circuit with arbitrary inputs;<br />
and to complete a truth table for the circuit. From the application, you can import the circuit corresponding to the [[#Sample_Problem_1|Sample Problem 1]] <br />
from the file located at [http://www.acsl.org/misc/wiki-digital-electronics-sample1.circ http://www.acsl.org/misc/wiki-digital-electronics-sample1.circ].<br />
There are many YouTube videos that show how to use the Logisim application; including a very nice [https://www.youtube.com/watch?v=cMz7wyY_PxE 4 minute tutorial].<br />
<br />
Logisim contains many <br />
additional advanced features that are beyond<br />
the scope of ACSL problems.<br />
<br />
=Sample Problems =<br />
<br />
== Sample Problem 1 ==<br />
<br />
Find all ordered triplets (A, B, C) which make the following circuit FALSE:<br />
<br />
::[[File:NotABorC.svg|200px]]<br />
<br />
'''Solution:'''<br />
<br />
One approach to solving this problem is to reason about that inputs and outputs are necessary at each gate. For the circuit to be FALSE, both inputs to the file OR gate must be false. Thus, input C must be FALSE, and the output of the NAND gate must also be false. The NAND gate is false only when both of its inputs are TRUE; thus, inputs A and B must both be TRUE. The final answer is (TRUE, TRUE, FALSE), or (1, 1, 0).<br />
<br />
Another approach to solving this problem is to translate the circuit into a Boolean Algebra expression and simplify <br />
the expression using the laws of Boolean Algebra. This circuit translates to the Boolean expression <math>\overline{AB}+C</math>. <br />
To find when this is FALSE we can equivalently find when the <math>\overline{\overline{AB}+C}</math> is TRUE. <br />
The expression becomes <math>\overline{\overline{AB}}\cdot \overline{C}</math> after applying DeMorgan’s Law. The double NOT over the AB expression<br />
cancels out, to become <math>AB\overline{C}</math>. The AND of 3 terms is TRUE when each term is TRUE, or A=1, B=1 and C=0.<br />
<br />
== Sample Problem 2 ==<br />
<br />
How many ordered 4-tuples (A, B, C, D) make the following circuit TRUE?<br />
<br />
::[[File:circuit-sample2.svg |400px]]<br />
<br />
'''Solution:'''<br />
<br />
We'll use a truth table to solve this problem. The rows in the truth table will correspond to all possible inputs - 16 in this case, since there are 4 inputs. The output columns will be the output of each gate, other than the NOT gates. The diagram below labels each of the gates; it's useful to keep the column straight when working the truth table.<br />
<br />
::[[File:circuit-sample2-labels.svg |300px]]<br />
<br />
{| class="wikitable" style="text-align: center"<br />
|-<br />
|colspan="4" style="background-color: #cceeff; font-size: x-small" "|INPUT<br />
|colspan="5" style="background-color: #cceeff; font-size: x-small" "|OUTPUT<br />
|-<br />
!rowspan="2" | A<br />
!rowspan="2" | B<br />
!rowspan="2" | C<br />
!rowspan="2" | D<br />
! p<br />
! q<br />
! r<br />
! s<br />
! t<br />
|-<br />
!<math>\overline{C+D}</math><br />
!<math>p+\overline{B}</math><br />
!<math>\overline{A}B</math><br />
!<math>r \oplus q</math><br />
!<math>s \oplus p</math><br />
|-<br />
|0 || 0 || 0 || 0 || 1 || 1 || 0 || 1 || 0<br />
|-<br />
|0 || 0 || 0 || 1 || 0 || 1 || 0 || 1 || 1<br />
|-<br />
|0 || 0 || 1 || 0 || 0 || 1 || 0 || 1 || 1<br />
|-<br />
|0 || 0 || 1 || 1 || 0 || 1 || 0 || 1 || 1<br />
|-<br />
|0 || 1 || 0 || 0 || 1 || 1 || 1 || 0 || 1<br />
|-<br />
|0 || 1 || 0 || 1 || 0 || 0 || 1 || 1 || 1<br />
|-<br />
|0 || 1 || 1 || 0 || 0 || 1 || 1 || 1 || 1<br />
|-<br />
|0 || 1 || 1 || 1 || 0 || 0 || 1 || 1 || 1<br />
|-<br />
|1 || 0 || 0 || 0 || 1 || 1 || 0 || 1 || 0<br />
|-<br />
|1 || 0 || 0 || 1 || 0 || 1 || 0 || 1 || 1<br />
|-<br />
|1 || 0 || 1 || 0 || 0 || 1 || 0 || 1 || 1<br />
|-<br />
|1 || 0 || 1 || 1 || 0 || 1 || 0 || 1 || 1<br />
|-<br />
|1 || 1 || 0 || 0 || 1 || 1 || 0 || 1 || 0<br />
|-<br />
|1 || 1 || 0 || 1 || 0 || 0 || 0 || 0 || 0<br />
|-<br />
|1 || 1 || 1 || 0 || 0 || 0 || 0 || 0 || 0<br />
|-<br />
|1 || 1 || 1 || 1 || 0 || 0 || 0 || 0 || 0<br />
|}<br />
<br />
From the truth table, there are 10 rows where the final output is TRUE.<br />
<br />
== Sample Problem 3 ==<br />
<br />
Simplify the Boolean expression that this circuit represents.<br />
<br />
::[[File:Circuit-PB.png|300px]]<br />
<br />
'''Solution:'''<br />
<br />
The circuit translates as follows:<br />
<br />
<math>(\overline{A}(A+B))\overline{B+C}</math><br />
<math>=(\overline{A}A+\overline{A}B)\overline{B}\overline{C}</math><br />
<math>=(0+\overline{A}B)\overline{B}\overline{C}</math><br />
<math>=\overline{A}B\overline{B}\overline{C}</math><br />
<math>=0</math><br />
<br />
= Video Resources =<br />
<br />
==ACSL Advisors==<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--><br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/gxil9VyGTtE</youtube><br />
| [https://youtu.be/gxil9VyGTtE ''Digital Electronics -1'' ('''Ravi Yeluru (hemsra)''')]<br />
<br />
Introduces the AND, OR, and NOT gates, and works through a couple of simple circuits.<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/m7-wa5ca_G8</youtube><br />
| [https://youtu.be/m7-wa5ca_G8 Digital Electronics -2'' ('''Ravi Yeluru (hemsra)''')]<br />
<br />
Solves a 3-gate circuit (the AND of an OR and a NOR) given by the Boolean expression <math>(\overline{A}+B)(\overline{B+C})</math>.<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/Eckd5UlJmB4</youtube><br />
| [https://youtu.be/Eckd5UlJmB4 ''Digital Electronics Boolean Algebra'' ('''Tangerine Code''')]<br />
<br />
Solves a 3-gate circuit (the AND of an OR and a NOR) given by the Boolean expression<br />
<math>\overline{(A+B)}(B+C)</math>. <br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/JJL6DsVCpHo</youtube><br />
| [https://youtu.be/JJL6DsVCpHo ''ACSL Digital Electronics Worksheet Sample'' ('''misterminich''')]<br />
<br />
Solves a handful of ACSL problems.<br />
|}<br />
<br />
<br />
==Other Videos==<br />
<br />
The topic of Digital Electronics is fundamental in Computer Science and there are many videos on YouTube that teach this subject. We found the following videos to be<br />
nice introductions to digital logic gates. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/s2MI_NgKD98</youtube><br />
| [https://youtu.be/s2MI_NgKD98 ''Digital Electronics Basics'' ('''Beginning Electronics''')]<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/z9s8A8oBe7g</youtube><br />
| [https://youtu.be/z9s8A8oBe7g ''Logic Gate Expressions'' ('''Kevin Drumm''')]<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/TLl4E3IV6Z0</youtube><br />
| [https://youtu.be/TLl4E3IV6Z0 ''Logic Gate Combinations'' ('''Kevin Drumm''')]<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/eroqZpbKrhc</youtube><br />
| [https://youtu.be/eroqZpbKrhc ''Determining the truth table and logic statement'' ('''Anna does some physics''')]<br />
Uses a truth table to find the inputs that make the circuit <math>\not(A+B)+(AB)</math> true using a truth table.<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=My_test_page&diff=651My test page2020-07-28T18:27:13Z<p>Marc: Created page with "This is a '''bold''' statement and here is another '''very bold''' two words. Each contest in the Elementary Division consists of 5 short answer questions covering one topic..."</p>
<hr />
<div>This is a '''bold''' statement and here is another '''very bold''' two words.<br />
<br />
Each contest in the Elementary Division consists of 5 short answer questions covering one topic each contest; the topic for each contest is listed below. The time limit for the 5-question test is 30 minutes. More information about this division is available in this presentation.<br />
<br />
== This is section header==<br />
<br />
<br />
<br />
Each contest in the Junior, Intermediate, and Senior Divisions has two parts: a take home 72-hour time limit programming problem and a set of short answer questions. Each short answer test consists of 2 questions on each of the first two topics listed below and 1 question on the third topic for a total of 5 questions. The time limit for the 5 question test is 30 minutes.<br />
<br />
=== THis is a subsectino ===<br />
<br />
Mastering this topic is essential for systems programming, programming in assembly language, optimizing code, and hardware design.<br />
<br />
== Operators ==<br />
<br />
=== Bitwise Operators ===<br />
<br />
The logical operators are '''not''' (~ or $\neg$), '''and''' (&), '''or''' (|), and '''xor''' ($\oplus$). These operators should be familiar to ACSL students from the [[Boolean Algebra]] and [[Digital Electronics]] categories.<br />
<br />
* '''not''' is a unary operator that performs logical negation on each bit. Bits that are 0 become 1, and those that are 1 become 0. For example: ~101110 has a value of 010001.<br />
<br />
* '''and''' is a binary operator that performs the logical '''and''' of each bit in each of its operands. The '''and''' of two values is 1 only if both values are 1. For example, '''1011011 and 011001''' has a value of '''001001'''. The '''and''' function is often used to isolate the value of a bit in a bit-string or to clear the value of a bit in a bit-string.<br />
<br />
* '''or''' is a binary operator that performs the logical '''or''' of each bit in each of its operands. The '''or''' of two values is 1 only if one or both values are 1. For example, '''1011011 or 011001''' has a value of '''111011'''. The '''or''' function is often use to force the value of a bit in a bit-string to be 1, if it isn't already.</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Assembly_Language_Programming&diff=650Assembly Language Programming2020-07-28T17:47:05Z<p>Marc: </p>
<hr />
<div>Programs written in high-level languages are traditionally converted by compilers into assembly language, which is turned into machine language programs – sequences of 1’s and 0’s – by an assembler. Even today, with very good quality compilers available, there is the need for programmers to understand assembly language. First, it provides programmers with a better understanding of the compiler and its constraints. Second, on occasion, programmers find themselves needing to program directly in assembly language in order to meet constraints in execution speed or space. <br />
<br />
ACSL chose to define its own assembly language rather than use a “real” one in order to eliminate the many sticky details associated with real languages. The basic concepts of our ACSL topic description are common to all assembly languages. <br />
<br />
== Reference Manual ==<br />
<br />
Execution starts at the first line of the program and continues sequentially, except for branch instructions (BG, BE, BL, BU), until the end instruction (END) is encountered. The result of each operation is stored in a special word of memory, called the “accumulator” (ACC). The initial value of the ACC is 0. Each line of an assembly language program has the following fields:<br />
<br />
LABEL OPCODE LOC<br />
<br />
The LABEL field, if present, is an alphanumeric character string beginning in the first column. A label must begin with an alphabetic character(A through Z, or a through z), and labels are case-sensitive. Valid OPCODEs are listed in the chart below; they are also case-sensitive and uppercase. Opcodes are reserved words of the language and (the uppercase version) many not be used a label. The LOC field is either a reference to a label or ''immediate data''. For example, “LOAD A” would put the contents referenced by the label “A” into the ACC; “LOAD =123” would store the value 123 in the ACC. Only those instructions that do not modify the LOC field can use the “immediate data” format. In the following chart, they are indicated by an asterisk in the first column.<br />
<br />
{| class="wikitable" style="text-align: left"|<br />
|-<br />
!OP CODE<br />
!DESCRIPTION<br />
<br />
|-<br />
!*LOAD<br />
|The contents of LOC are placed in the ACC. LOC is unchanged.<br />
<br />
|-<br />
!STORE<br />
|The contents of the ACC are placed in the LOC. ACC is unchanged.<br />
<br />
|-<br />
!*ADD<br />
|The contents of LOC are added to the contents of the ACC. The sum is stored in the ACC. LOC is unchanged. Addition is modulo 1,000,000.<br />
<br />
|-<br />
!*SUB<br />
|The contents of LOC are subtracted from the contents of the ACC. The difference is stored in the ACC. LOC is unchanged. Subtraction is modulo 1,000,000.<br />
<br />
|-<br />
!*MULT<br />
|The contents of LOC are multiplied by the contents of the ACC. The product is stored in the ACC. LOC is unchanged. Multiplication is modulo 1,000,000.<br />
<br />
|-<br />
!*DIV<br />
|The contents of LOC are divided into the contents of the ACC. The signed integer part of the quotient is stored in the ACC. LOC is unchanged.<br />
.<br />
|-<br />
!BG<br />
|<br />
Branch to the instruction labeled with LOC if ACC>0.<br />
<br />
|-<br />
!BE<br />
|<br />
Branch to the instruction labeled with LOC if ACC=0.<br />
<br />
|-<br />
!BL<br />
|<br />
Branch to the instruction labeled with LOC if ACC<0.<br />
<br />
|-<br />
!BU<br />
|Branch to the instruction labeled with LOC.<br />
<br />
|-<br />
!READ<br />
|<br />
Read a signed integer (modulo 1,000,000) into LOC.<br />
<br />
|-<br />
!PRINT<br />
|<br />
Print the contents of LOC.<br />
<br />
|-<br />
!DC<br />
|<br />
The value of the memory word defined by the LABEL field is defined to contain the specified constant. The LABEL field is mandatory for this opcode. The ACC is not modified.<br />
<br />
|-<br />
!END<br />
| <br />
Program terminates. LOC field is ignored and must be empty.<br />
|}<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
After the following program is executed, <br />
what value is in location TEMP?<br />
<br />
{|class="wikitable" style="text-align: left"<br />
|TEMP || DC || 0<br />
|-<br />
|A||DC||8<br />
|-<br />
|B||DC||-2<br />
|-<br />
|C||DC||3<br />
|-<br />
| ||LOAD||B<br />
|-<br />
| ||MULT||C<br />
|-<br />
| ||ADD||A<br />
|-<br />
| ||DIV||B<br />
|-<br />
| ||SUB||A<br />
|-<br />
| ||STORE||TEMP<br />
|-<br />
| ||END||<br />
|}<br />
<br />
'''Solution:''' The ACC takes on values -2, -6, 2, -1, and -9 in that order. The last value, -9, is stored in location TEMP.<br />
<br />
=== Problem 2 ===<br />
<br />
If the following program has an input value<br />
of N, what is the final value of X which is<br />
computed? Express X as an algebraic<br />
expression in terms of N. <br />
<br />
{|class="wikitable" style="text-align: left"<br />
| || READ || X<br />
|-<br />
| || LOAD || X<br />
|-<br />
|TOP || SUB || =1<br />
|-<br />
| || BE || DONE<br />
|-<br />
| || STORE || A<br />
|-<br />
| || MULT || X<br />
|-<br />
| || STORE || X<br />
|-<br />
| || LOAD || A<br />
|-<br />
| || BU || TOP<br />
|-<br />
|DONE || END || <br />
|}<br />
<br />
'''Solution:''' This program loops between labels TOP and<br />
DONE for A times. A has an initial value of X and subsequent terms of N,<br />
then values of A-1, A-2, …, 1. Each time through the loop,<br />
X is multiplied by the the current value of A.<br />
Thus, X = A * (A-1) * (A-2) * … * 1 or X=A! or A factorial.<br />
For example, 5! = 5 * 4 * 3 * 2 * 1 = 120. Since the<br />
initial value of A is the number input (i.e. N),<br />
the algebraic expression is X = N!.<br />
<br />
== Video Resources ==<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/RUm3iHsbO3I</youtube><br />
| [https://youtu.be/RUm3iHsbO3I ''Intro to Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A general introduction into assembly language. In particular, it covers how it fits into the source code to an executable image pipeline.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/i_JYT398O64</youtube><br />
| [https://youtu.be/i_JYT398O64 ''Syntax of ACSL Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A very nice introduction to this ACSL category.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/NEQASUsZ0g4</youtube><br />
| [https://youtu.be/NEQASUsZ0g4 ''Examples'' ('''CalculusNguyenify''')]<br />
<br />
Walks through a couple of ACSL Assembly language programs that have been used in previous contests.<br />
<br />
|}<br />
<br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--><br />
<br />
x</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Prefix/Infix/Postfix_Notation&diff=649Prefix/Infix/Postfix Notation2020-07-23T17:39:34Z<p>Marc: Reverted edits by Raj Joshi (talk) to last revision by Marc Brown</p>
<hr />
<div>The expression $5+\large{8\over{3-1}}$ clearly has a value of 9. It is written in ''infix'' notation as $5+8/(3-1)$. The value of an ''infix'' version is well-defined because there is a well-established order of precedence in mathematics: We first evaluate the parentheses (3-1=2); then, because division has higher precedence that subtraction, we next do 8/2=4. And finally, 5+4=9. The order of precedence is often given the mnemonic of '''Please excuse my dear Aunt Sue''', or '''PEMDAS''': '''p'''arentheses, '''e'''xponentiation, '''m'''ultiplication/'''d'''ivision, and '''a'''ddition/''''s'''ubtraction. Multiplication and division have the same level of precedence; addition and subtraction also have the same level of precedence. Terms with equals precedence are evaluated from left-to-right [https://en.wikipedia.org/wiki/Order_of_operations wikipedia].<br />
<br />
The algorithm to evaluate an ''infix'' expression is complex, as it must address the order of precedence. Two alternative notations have been developed which lend themselves to simple computer algorithms for evaluating expressions. In ''prefix'' notation, each operator is placed before its operands . The expression above would be <syntaxhighlight lang="python" inline>+ 5 / 8 - 3 1</syntaxhighlight>. In ''postfix'' notation, each operator is placed after its operand. The expression above is <syntaxhighlight lang="python" inline>5 8 3 1 - / +</syntaxhighlight>. In ''prefix'' and ''postfix'' notations, there is no notion of order of precedence, nor are there any parentheses. The evaluation is the same regardless of the operators. <br />
<br />
Problems in this category ask to convert between prefix, infix, and postfix, or to evaluate an expression in prefix or postfix.<br />
<br />
== Converting Expressions ==<br />
<br />
An algorithm for converting from infix to prefix (postfix) is as follows:<br />
* Fully parenthesize the infix expression. It should now consist solely of “terms”: a binary operator sandwiched between two operands.<br />
* Write down the operands in the same order that they appear in the infix expression.<br />
* Look at each term in the infix expression in the order that one would evaluate them, i.e., inner-most parenthesis to outer-most and left to right among terms of the same depth.<br />
* For each term, write down the operand before (after) the operators. <br />
<br />
One way to convert from prefix (postfix) to infix is to make repeated scans through the expression. <br />
Each scan, find an operator with two adjacent operators and replace it with a parenthesized infix expression. <br />
This is not the most efficient algorithm, but works well for a human.<br />
<br />
A quick check for determining whether a conversion is correct is to convert the result back into the original format. <br />
<br />
== Context ==<br />
<br />
''Prefix'' and ''postfix'' notation is also known as [https://en.wikipedia.org/wiki/Polish_notation ''Polish'' and ''Reverse Polish'' notation], respectively.<br />
<br />
== Examples ==<br />
<br />
=== Infix to Prefix ===<br />
<br />
<br />
The following sequence of steps illustrates converting $X=\left(AB-{C\over{D}}\right)^E$ from infix to prefix:<br />
::{| class="wikitable" style="text-align: center"<br />
|-<br />
!(X = (((A * B) - (C / D)) ↑ E)) <br />
|-<br />
| style="background-color: #ffffff; text-align: left; padding-left: 2em" |X A B C D E <br />
|-<br />
| style="background-color: #ffffff; text-align: left; padding-left: 2em" | X <syntaxhighlight lang="python" inline>* A B</syntaxhighlight> C D E<br />
|-<br />
| style="background-color: #ffffff; text-align: left; padding-left: 2em" | X <syntaxhighlight lang="python" inline>* A B</syntaxhighlight> <syntaxhighlight lang="python" inline>/ C D</syntaxhighlight> E<br />
|-<br />
| style="background-color: #ffffff; text-align: left; padding-left: 2em" | X <syntaxhighlight lang="python" inline>- * A B / C D</syntaxhighlight> E<br />
|-<br />
| style="background-color: #ffffff; text-align: left; padding-left: 2em" | X <syntaxhighlight lang="python" inline>↑- * A B / C D E</syntaxhighlight><br />
|-<br />
| style="background-color: #ffffff; text-align: left; padding-left: 2em" | <syntaxhighlight lang="python" inline>= X ↑ - * A B / C D E</syntaxhighlight><br />
|}<br />
<br />
=== Infix to Postfix ===<br />
<br />
<br />
The following sequence of steps illustrates converting $X=\left(AB-{C\over{D}}\right)^E$ from infix to postfix:<br />
::{| class="wikitable" style="text-align: center"<br />
|-<br />
!(X = (((A * B) - (C / D)) ↑ E))<br />
|-<br />
| style="background-color: #ffffff; text-align: left; padding-left: 2em" |X A B C D E <br />
|-<br />
| style="background-color: #ffffff; text-align: left; padding-left: 2em" | X <syntaxhighlight lang="python" inline>A B *</syntaxhighlight> C D E<br />
|-<br />
| style="background-color: #ffffff; text-align: left; padding-left: 2em" | X <syntaxhighlight lang="python" inline>A B *</syntaxhighlight> <syntaxhighlight lang="python" inline>C D /</syntaxhighlight> E<br />
|-<br />
| style="background-color: #ffffff; text-align: left; padding-left: 2em" | X <syntaxhighlight lang="python" inline>A B * C D / -</syntaxhighlight> E<br />
|-<br />
| style="background-color: #ffffff; text-align: left; padding-left: 2em" | X <syntaxhighlight lang="python" inline>A B * C D / - E ↑</syntaxhighlight><br />
|-<br />
| style="background-color: #ffffff; text-align: left; padding-left: 2em" | <syntaxhighlight lang="python" inline> X A B * C D / - E ↑ =</syntaxhighlight><br />
|}<br />
<br />
=== Prefix to Infix ===<br />
<br />
The following sequence of steps illustrates converting $(3*4+{8\over{2}})^{(7-5)}$ from its prefix representation to infix:<br />
::{| class="wikitable" style="text-align: left"<br />
|-<br />
! ↑ + * 3 4 / 8 2 – 7 5 <br />
|-<br />
| style="background-color: #ffffff" | ↑ + <syntaxhighlight lang="python" inline>(3*4)</syntaxhighlight> / 8 2 – 7 5<br />
|-<br />
| style="background-color: #ffffff" |↑ + <syntaxhighlight lang="python" inline>(3*4)</syntaxhighlight> <syntaxhighlight lang="python" inline>(8/2)</syntaxhighlight> – 7 5<br />
|-<br />
| style="background-color: #ffffff" |↑ <syntaxhighlight lang="python" inline>(3*4)+(8/2)</syntaxhighlight> - 7 5<br />
|-<br />
| style="background-color: #ffffff" |↑ <syntaxhighlight lang="python" inline>((3*4)+(8/2))</syntaxhighlight> <syntaxhighlight lang="python" inline>(7-5)</syntaxhighlight><br />
|-<br />
| style="background-color: #ffffff" |<syntaxhighlight lang="python" inline>(((3*4)+(8/2))↑(7-5))</syntaxhighlight><br />
|}<br />
<br />
=== Postfix to Infix ===<br />
<br />
The following sequence of steps illustrates converting $(3*4+{8\over{2}})^{(7-5)}$ from its postfix representation to infix:<br />
::{| class="wikitable" style="text-align: left"<br />
|-<br />
!3 4 * 8 2 / + 7 5 -↑<br />
|-<br />
| style="background-color: #ffffff" | <syntaxhighlight lang="python" inline>(3*4)</syntaxhighlight> 8 2 / + 7 5 - ↑<br />
|-<br />
| style="background-color: #ffffff" | <syntaxhighlight lang="python" inline>(3*4)</syntaxhighlight> <syntaxhighlight lang="python" inline>(8/2)</syntaxhighlight> + 7 5 - ↑<br />
|-<br />
| style="background-color: #ffffff" | <syntaxhighlight lang="python" inline>((3*4)+(8/2))</syntaxhighlight> 7 5 -↑ <br />
|-<br />
| style="background-color: #ffffff" | <syntaxhighlight lang="python" inline>((3*4)+(8/2))</syntaxhighlight> <syntaxhighlight lang="python" inline>(7-5)</syntaxhighlight> ↑ <br />
|-<br />
| style="background-color: #ffffff" |<syntaxhighlight lang="python" inline>(((3*4)+(8/2))↑(7-5))</syntaxhighlight> <br />
|}<br />
<br />
== Video Resources ==<br />
<br />
The following YouTube videos show ACSL students and advisors working out some previous problems.<br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/Lj91oRv3EIY</youtube><br />
| [https://youtu.be/Lj91oRv3EIY ''ACSL Prep - Mrs. Gupta - Prefix Infix Postfix'' ('''MegaChristian5555''')]<br />
<br />
This video starts with a nice introduction to the category, and then solves a handful of practice problems.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/lDm08rP_lms</youtube><br />
| [https://youtu.be/lDm08rP_lms ''ACSL Prefix Postfix Infix Contest 2 Worksheet 1'' ('''misterminich''')]<br />
<br />
Mr. Minich is an ACSL advisor. This video was created to help prepare his students for the ACSL Prefix/Infix/Postfix category. It shows solutions to 5 different problems that have<br />
appeared in recent years. <br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/4YZdsw2UOpo</youtube><br />
| [https://youtu.be/4YZdsw2UOpo ''Prefix Notation'' ('''Tangerine Code''')]<br />
<br />
To quote the video description: ''A general tutorial on prefix notation that can be used for American Computer Science League.''<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/R-t9-rbJDw8</youtube><br />
| [https://youtu.be/R-t9-rbJDw8 ''Postfix Notation'' ('''Tangerine Code''')]<br />
<br />
To quote the video description: ''A general tutorial on postfix notation that can be used for American Computer Science League.''<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/dcc-nXY2L6c</youtube><br />
| [https://youtu.be/dcc-nXY2L6c ''2015 16 Contest 2 Prefix Infix Postfix Problem#1'' ('''Ravi Yeluru''')]<br />
<br />
Ravi Yeluru walks through the solution to an ACSL Prefix/Infix/Postfix program that appeared in the 2015-16 year, Contest #2.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/lEIPvUqstEY</youtube><br />
| [https://youtu.be/lEIPvUqstEY ''2015 16 Contest 2 Prefix Infix Postfix Problem#2'' ('''Ravi Yeluru''')]<br />
<br />
Ravi Yeluru walks through the solution to an ACSL Prefix/Infix/Postfix program that appeared in the 2015-16 year, Contest #2.<br />
<br />
|}<br />
<br />
=== Other Videos ===<br />
<br />
The following YouTube videos cover various aspects of this topic; they were created by authors who are not involved (or aware) of ACSL, to the best of our knowledge. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/t7YWDiz8LMY</youtube><br />
| [https://youtu.be/t7YWDiz8LMY ''Infix Postfix and Prefix Notation'' ('''Carleton Moore''')]<br />
<br />
A very gentle overview of infix, prefix, and postfix.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/z3VsmufB_QI</youtube><br />
| [https://youtu.be/z3VsmufB_Q I''Infix Prefix and Postfix'' ('''University Academy''')]<br />
<br />
Another very gentle overview of infix, prefix, and postfix.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jos1Flt21is</youtube><br />
| [https://youtu.be/jos1Flt21is ''Infix Postfix and Prefix Notation'' ('''mycodeschool''')]<br />
<br />
Another overview of infix, prefix, and postfix notations.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/MeRb_1bddWg</youtube><br />
| [https://youtu.be/MeRb_1bddWg ''Evaluation of Prefix and Postfix expressions using stack'' ('''mycodeschool''')]<br />
<br />
Describes the standard algorithm to evaluate prefix and postfix expressions.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/vq-nUF0G4fI</youtube><br />
| [https://youtu.be/vq-nUF0G4fI ''Infix to Postfix using stack'' ('''mycodeschool''')]<br />
<br />
Describes the standard algorithm to convert from infix to postfix. <br />
|}<br />
<br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--></div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Graph_Theory&diff=645Graph Theory2020-06-10T06:46:26Z<p>Marc: /* ACSL Videos */</p>
<hr />
<div>Many problems are naturally formulated in terms of points and connections between them. For example, a computer network has PCs connected by cables, an airline map has cities connected by routes, and a school has rooms connected by hallways. A graph is a mathematical object which models such situations.<br />
<br />
== Overview ==<br />
<br />
A ''graph'' is a collection of vertices and edges. An ''edge'' is a connection between two ''vertices'' (sometimes referred to as ''nodes''). One can draw a graph by marking points for the vertices and drawing lines connecting them for the edges, but the graph is defined independently of the visual representation. For example, the following two drawings represent the same graph:<br />
<br />
::[[File:Graph fig1a.svg|150px]] <span style="display:inline-block; width: 5em;"></span>[[File:Graph fig1b.svg|150px]]<br />
<br />
The precise way to represent this graph is to identify its set of vertices {A, B, C, D, E, F, G}, and its set of edges between these vertices {AB, AD, BD, CF, FG, GH, GE, HE}.<br />
<br />
To be precise, the graph above is an ''undirected graph''; the edge from x to y is the same as from y to x.<br />
<br />
== Terminology ==<br />
<br />
A ''path'' from vertex x to y in a graph is a list of vertices, in which successive vertices are connected by edges in the graph. For example, FGHE is path from F to E in the graph above. A ''simple path'' is a path with no vertex repeated. For example, FGHEG is not a simple path. <br />
<br />
A graph is ''connected'' if there is a path from every vertex to every other vertex in the graph. Intuitively, if the vertices were physical objects and the edges were strings connecting them, a connected graph would stay in one piece if picked up by any vertex. A graph which is not connected is made up of ''connected components''. For example, the graph above has two connected components: {A, B, D} and {C, E, F, G, H}.<br />
<br />
A ''cycle'' is a path, which is simple except that the first and last vertex are the same (a path from a point back to itself). For example, the path HEGH is a cycle in our example. Vertices must be listed in the order that they are traveled to make the path; any of the vertices may be listed first. Thus, HEGH and EHGE are different ways to identify the same cycle. For clarity, we list the start / end vertex twice: once at the start of the cycle and once at the end.<br />
<br />
We’ll denote the number of vertices in a given graph by V and the number of edges by E. Note that E can range anywhere from V to V<sup>2</sup> (or V(V-1)/2 in an undirected graph). Graphs with all edges present are called ''complete graphs''; graphs with relatively few edges present (say less than V log(V)) are called ''sparse''; graphs with relatively few edges missing are called ''dense.''<br />
<br />
== Trees ==<br />
<br />
A graph with no cycles is called a ''tree''. There is only one path between any two nodes in a tree. A tree on N vertices contains exactly N-1 edges. A ''spanning tree'' of a graph is a subgraph that contains all the vertices and forms a tree. Minimal spanning trees can be found for weighted graphs (i.e. each edge has a cost or distance associated with it) in order to minimize the cost or distance across an entire network. A group of disconnected trees is called a ''forest.''<br />
<br />
== Directed Graphs ==<br />
<br />
''Directed graphs'' are graphs which have a direction associated with each edge. An edge xy in a directed graph can be used in a path that goes from x to y but not necessarily from y to x. For example, a directed graph similar to our example graph is drawn below:<br />
<br />
[[File:Graph fig1a directed.svg|150px]]<br />
<br />
This graph is defined as the set of vertices V = {A,B,C,D,E,F,G,H} and the set of edges {AB,AD,DA,DB,EG,GE,HG,HE,GF,CF,FC}. There is one directed path from G to C (namely, GFC); however, there are no directed paths from C to G. Note that a few of the edges have arrows on both ends, such as the edge between A and D. These dual arrows indicate that there is an edge in each direction, which essentially makes an undirected edge. An ''undirected graph'' can be thought of as a directed graph with all edges occurring in pairs in this way. A directed graph with no cycles is called a ''dag'' (directed acyclic graph).<br />
<br />
== Adjacency Matrices ==<br />
<br />
It is frequently convenient to represent a graph by a matrix, as shown in the second sample problem below. If we consider vertex A as 1, B as 2, etc., then a “one” in M at row ''i'' and column ''j'' indicates that there is an edge from vertex ''i'' to ''j''., whereas a "zero" indicates there is not an edge. If we raise M to the ''pth'' power, the resulting matrix indicates which paths of length ''p'' exist in the graph. The value in $M^p(i,j)$ is the number of paths from vertex ''i'' to vertex ''j''.<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
Find the number of different cycles contained in the directed graph with vertices {A, B, C, D, E} and edges {AB, BA, BC, CD, DC, DB, DE}.<br />
<br />
'''Solution:'''<br />
The graph is as follows:<br />
<br />
::[[File:graph sample1.svg|196px]]<br />
<br />
By inspection, the cycles are: ABA, BCDB, and CDC. Thus, there are 3 cycles in the graph.<br />
<!--<br />
=== Sample Problem 2 ===<br />
<br />
Given the directed graph below, how many more paths of length 3 would exist if edges AE and EA were added?<br />
<br />
[[File:graph_s1.png]]<br />
<br />
'''Solution:'''<br />
<br />
By using adjacency matrices, the current one is<br />
<br />
[[File:matrix1_s2.png]]<br />
<br />
After adding edges AE and EA, the matrices are:<br />
<br />
[[File:matrix2_s2.png]]<br />
<br />
There are 6 more paths of length 2 since 16 – 10 = 6.<br />
--><br />
<br />
=== Problem 2===<br />
<br />
In the following directed graph, find the total number of different paths from vertex A to vertex C of length 2 or 4.<br />
<br />
::[[File:graph sample3.svg|128px]]<br />
<br />
'''Solution:'''<br />
<br />
Let matrix $M$ represent the graph. Recall that the number of paths from vertex $i$ to vertex $j$ of length $p$ equals $M^p[i,j]$. The values of $M$, $M^2$ and $M^4$ are:<br />
<br />
[[File:matrix_s3.png]]<br />
<br />
There is 1 path of length 2 from A to C (cell [1,3] in $M^2$) <br />
and 3 paths of length 4 (cell [1,3] in $M^4$).<br />
<br />
=== Problem 3 ===<br />
<br />
Given the adjacency matrix, draw the directed graph.<br />
<br />
[[File:matrix_s4.png]]<br />
<br />
'''Solution:'''<br />
<br />
There must be exactly 4 vertices: V = {A, B, C, D}. There must be be exactly 7 edges: E = {AB, AD, BA, BD, CA, DB, DC}. Here are two valid drawings of the graph:<br />
<br />
: [[File:Graph sample4soln-a.svg|128px]] <span style="display:inline-block; width: 5em;"></span>[[File:Graph sample4soln-d.svg|128px]]<br />
<br />
== Video Resources ==<br />
<br />
===ACSL Videos===<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/H96Lo2q_FSk</youtube><br />
| [https://youtu.be/H96Lo2q_FSk ''ACSL - Graph Theory Worksheet 1'' ('''misterminich''')]<br />
<br />
Shows the solution to the ACSL problem: '''Draw the directed graph with vertices A, B, C, D, E and the directed edges AB, BC, AD, BC, DC, ED, and EA.''<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/y1fdIb_oNG0</youtube><br />
| [https://youtu.be/y1fdIb_oNG0 ''ACSL Graph Theory Worksheet 3'' ('''misterminich''')]<br />
<br />
Shows the solution to an ACSL problem asking to find how many paths of a specific length there are in a specific directed graph.<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/vtTCRMGB0K8</youtube><br />
| [https://youtu.be/vtTCRMGB0K8 ''Graph Theory ACSL Example Problem'' ('''Tangerine Code''')]<br />
<br />
Shows the solution to an ACSL problem asking to find how many different cycles there are from a specific vertex in a specific directed graph.<br />
|}<br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/ivhOH6crQ1w</youtube><br />
| [https://youtu.be/ivhOH6crQ1w ''ACSL Test Prep - Graph Theory'' ('''Mrs. Goopta''')]<br />
<br />
A talked-over presentation discussing graph theory as needed for the American Computer Science League and its tests.<br />
|}<br />
<br />
===Other Videos===<br />
<br />
There are dozens of YouTube videos that provide an introduction to graph theory. The following are a couple that we found particularly well done. Be aware that the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/44sQJK4BycY</youtube><br />
| [https://youtu.be/44sQJK4BycY ''CS 106B Lecture: Graphs: basic concepts'']<br />
<br />
This lecture from Stanford University is s wonderful introduction to graph theory. The lecture starts out with many examples of real world problems that are modeled by graphs, and then moves on to review basic terminology relating to graph theory. <br />
|-<br />
| <youtube width="300" height="180"> https://youtu.be/gXgEDyodOJU</youtube><br />
| [https://youtu.be/gXgEDyodOJU ''Data structures: Introduction to Graphs'' '''(mycodeschool)''']<br />
<br />
A nice introduction to the fundamental concepts of graphs. <br />
<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Data_Structures&diff=644Data Structures2020-06-10T06:43:46Z<p>Marc: /* ACSL Videos */</p>
<hr />
<div>At the heart of virtually every computer program are its algorithms and its data structures. It is hard to separate these two items, for data structures are meaningless without algorithms to create and manipulate them, and algorithms are usually trivial unless there are data structures on which to operate. The bigger the data sets, the more important data structures are in various algorithms.<br />
<br />
This category concentrates on four of the most basic structures: '''stacks''', '''queues''', '''binary search trees''', and '''priority queues'''. Questions will cover these data structures and implicit algorithms, not specific to implementation language details.<br />
<br />
A ''stack'' is usually used to save information that will need to be processed later. Items are processed in a “last-in, first-out” (LIFO) order. A ''queue'' is usually used to process items in the order in which requests are generated; a new item is not processed until all items currently on the queue are processed. This is also known as “first-in, first-out” (FIFO) order. A ''binary search tree'' is used when one is storing items and needs to be able to efficiently process the operations of insertion, deletion, and query (i.e. find out if a particular item is found in the list of items and if not, which item is close to the item in question). A ''priority queue'' is used like a binary search tree, except one cannot delete an arbitrary item, nor can one make an arbitrary query. One can only find out or delete the smallest element of the list.<br />
<br />
There are many online resources covering these basic data structures; indeed there are many books and entire courses devoted to fundamental data structures. Implementation varies by computer language, but for our purposes, they can all be represented as a list of items that might contain duplicates. The rest of this page is an overview of these structures.<br />
<br />
== Stacks and Queues ==<br />
<br />
A ''stack'' supports two operations: PUSH and POP. A command of the form PUSH("A") puts the key "A" at the top of the stack; the command “X = POP()” removes the top item from the stack and stores its value into variable X. If the stack was empty (because nothing had ever been pushed on it, or if all elements have been popped off of it), then X is given the special value of NIL. An analogy to this is a stack of books on a desk: a new book is placed on the top of the stack (pushed) and a book is removed from the top also (popped). Some textbooks call this data structure a “push-down stack” or a “LIFO stack”.<br />
<br />
''Queues'' operate just like stacks, except items are removed from the bottom instead of the top. A command of the form PUSH("A") puts the key "A" at the top of the queue; the command “X = POP()” removes the item from the bottom of the queue and stores its value into variable X. If the queue was empty (because nothing had ever been pushed on it, or if all elements have been popped off of it), then X is given the special value of NIL. A good physical analogy of this is the way a train conductor or newspaper boy uses a coin machine to give change: new coins are added to the tops of the piles, and change is given from the bottom of each. Sometimes the top and bottom of a queue are referred to as the rear and the front respectively. Therefore, items are pushed/enqueued at the rear of the queue and popped/dequeued at the front of the queue. There is a similarity to the Britsh "queueing up". Some textbooks refer to this data structure as a “FIFO stack”.<br />
<br />
Consider the following sequence of 14 operations:<br />
<br />
<pre><br />
PUSH("A")<br />
PUSH("M")<br />
PUSH("E")<br />
X = POP()<br />
PUSH("R")<br />
X = POP()<br />
PUSH("I")<br />
X = POP()<br />
X = POP()<br />
X = POP()<br />
X = POP()<br />
PUSH("C")<br />
PUSH("A")<br />
PUSH("N")<br />
</pre><br />
<br />
If these operations are applied to a stack, then the values of the pops are: E, R, I, M, A and NIL. After all of the operations, there are three items still on the stack: the N is at the top (it will be the next to be popped, if nothing else is pushed before the pop command), and C is at the bottom. If, instead of using a stack we used a queue, then the values popped would be: A, M, E, R, I and NIL. There would be three items still on the queue: N at the top and C on the bottom. Since items are removed from the bottom of a queue, C would be the next item to be popped regardless of any additional pushes.<br />
<br />
=== Pre-2018 Syntax ===<br />
<br />
ACSL Contests pre-2018 used a slightly different, and more liberal, syntax. Consider the following sequence on a stack:<br />
<br />
<pre><br />
PUSH(A)<br />
PUSH(B)<br />
PUSH(C)<br />
POP(X)<br />
POP(Z)<br />
</pre><br />
<br />
After the 5 operations, the stack would be left with A as the only element on the stack, the value of C in variable X, and the value of B in variable Z. <br />
<br />
Were the above operations applied to a queue, the queue would be left with C; variable X would contain A; and variable Z would contain B.<br />
<br />
== Trees ==<br />
<br />
''Trees'', in general, use the following terminology: the ''root'' is the top node in the tree; ''children'' are the nodes that are immediately below a ''parent'' node; ''leaves'' are the bottom-most nodes on every branch of the tree; and ''siblings'' are nodes that have the same immediate parent. <br />
<br />
A ''binary search tree'' is composed of nodes having three parts: information (or a key), a pointer to a left child, and a pointer to a right child. It has the property that the key at every node is always greater than or equal to the key of its left child, and less than the key of its right child.<br />
<br />
The following tree is built from the keys A, M, E, R, I, C, A, N in that order:<br />
<br />
<center><br />
[[File:bst-american.svg|200px]]<br />
</center><br />
<br />
The ''root'' of the resulting tree is the node containing the key A. Our ACSL convention places duplicate keys into the tree as if they were less than their equal key. (In some textbooks and software libraries, duplicate keys may be considered larger than their equal key.) The tree has a ''depth'' (sometimes called height) of 3 because the deepest node is 3 nodes below the root. The root node has a depth of 0. Nodes with no children are called leaf nodes; there are four of them in the tree: A, C, I and N. Our ACSL convention is that an ''external node'' is the name given to a place where a new node could be attached to the tree. (In some textbooks, an external node is synonymous with a leaf node.) In the final tree above, there are 9 external nodes; these are not drawn. The tree has an ''internal path length'' of 15 which is the sum of the depths of all nodes. It has an ''external path length'' of 31 which is the sum of the depths of all external nodes. To insert the N (the last key inserted), 3 ''comparisons'' were needed against the root A (>), the M (>), and the R (≤).<br />
<br />
To perform an ''inorder'' traversal of the tree, recursively traverse the tree by first visiting the left child, then the root, then the right child. In the tree above, the nodes are visited in the following order: A, A, C, E, I, M, N, and R. A ''preorder'' travel (root, left, right) visits in the following order: A, A, M, E, C, I, R, and N. A ''postorder'' traversal (left, right, root) is: A, C, I, E, N, R, M, A. Inorder traversals are typically used to list the contents of the tree in sorted order. <br />
<br />
A binary search tree can support the operations insert, delete, and search. Moreover, it handles the operations efficiently for ''balanced'' trees. In a tree with 1 million items, one can search for a particular value in about log<sub>2</sub> 1,000,000 ≈ 20 steps. Items can be inserted or deleted in about as many steps, too. However, consider the binary search tree resulting from inserting the keys A, E, I, O, U, Y which places all of the other letters on the right side of the root "A". This is very unbalanced; therefore, sophisticated algorithms can be used to maintain balanced trees. Binary search trees are “dynamic” data structures that can support many operations in any order and introduces or removes nodes as needed. <br />
<br />
To search for a node in a binary tree, the following algorithm (in pseudo-code) is used:<br />
<br />
<syntaxhighlight lang="text"><br />
p = root<br />
found = FALSE<br />
while (p ≠ NIL) and (not found)<br />
if (x < p’s key)<br />
p = p’s left child<br />
else if (x > p’s key)<br />
p = p’s right child<br />
else<br />
found = TRUE<br />
end if<br />
end while<br />
</syntaxhighlight><br />
<br />
Deleting from a binary search tree is a bit more complicated. The algorithm we’ll use is as follows:<br />
<br />
<syntaxhighlight lang="text"><br />
p = node to delete<br />
f = father of p<br />
if (p has no children)<br />
delete p<br />
else if (p has one child)<br />
make p’s child become f’s child<br />
delete p<br />
else if (p has two children)<br />
l = p’s left child (it might also have children)<br />
r = p’s right child (it might also have children)<br />
make l become f’s child instead of p<br />
stick r onto the l tree<br />
delete p<br />
end if<br />
</syntaxhighlight><br />
<br />
These diagrams illustrate the algorithm using the tree above. At the left, we delete I (0 children); in the middle, we delete the R (1 child); and at the right, we delete the M (2 children).<br />
<br />
<center><br />
{| class ="wikitable"<br />
|[[File:bst-american-del-i.svg|200px]]<br />
|[[File:bst-american-del-r.svg|200px]]<br />
|[[File:bst-american-del-m.svg|200px]] <br />
|}<br />
</center><br />
<br />
There are also general trees that use the same terminology, but they have 0 or more ''subnodes'' which can be accessed with an array or linked list of pointers. ''Pre-order'' and ''post-order'' traversals are possible with these trees, but the other algorithms do not work in the same way. Applications of general trees include game theory, organizational charts, family trees, etc.<br />
<br />
''Balanced'' trees minimize searching time when every leaf node has a depth of within 1 of every other leaf node. ''Complete'' trees are filled in at every level and are always balanced. ''Strictly binary'' trees ensure that every node has either 0 or 2 subnodes. You may want to consider how there are exactly 5 strictly binary trees with 7 nodes.<br />
<br />
== Priority Queues ==<br />
<br />
A ''priority queue'' is quite similar to a binary search tree, but one can only delete the smallest item and retrieve the smallest item only. These insert and delete operations can be done in a guaranteed time proportional to the log (base 2) of the number of items; the retrieve-the-smallest can be done in constant time. <br />
<br />
The standard way to implement a priority queue is using a ''heap'' data structure. A heap uses a binary tree (that is, a tree with two children) and maintains the following two properties: every node is less than or equal to both of its two children (nothing is said about the relative magnitude of the two children), and the resulting tree contains no “holes”. That is, all levels of the tree are completely filled, except the bottom level, which is filled in from the left to the right.<br />
<br />
The algorithm for insertion is not too difficult: put the new node at the bottom of the tree and then go up the tree, making exchanges with its parent, until the tree is valid. <br />
<!--here would be the place to show building the heap with AMERICAN--><br />
The heap at the left<br />
was building from the letters A, M, E, R, I, C, A, N (in that order); the heap at the right is after a C has been added.<br />
<br />
<center><br />
{| class="wikitable"<br />
|[[File:heap-american.svg|220px]]||<br />
[[File:heap-american-insert-c.svg|220px]]<br />
|}<br />
</center><br />
<br />
The smallest value is always the root. To delete it (and one can only delete the smallest value), one replaces it with the bottom-most and right-most element, and then walks down the tree making exchanges with the smaller child in order to ensure that the tree is valid. The following pseudo-code formalizes this notion:<br />
<br />
<syntaxhighlight lang="text"><br />
b = bottom-most and right-most element<br />
p = root of tree<br />
p’s key = b’s key<br />
delete b<br />
while (p is larger than either child)<br />
exchange p with smaller child<br />
p = smaller child<br />
end while<br />
</syntaxhighlight><br />
<br />
When the smallest item is at the root of the heap, the heap is called a ''min-heap''. Of course, A ''max-heap'' is also possible and is common in practice. An efficient implementation of a heap uses an array that can be understood conceptually by using a tree data structure.<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
Consider an initially empty stack. After the following operations are performed, what is the value of Z?<br />
<br />
<syntaxhighlight lang="text"><br />
PUSH(3)<br />
PUSH(6)<br />
PUSH(8)<br />
Y = POP()<br />
X = POP()<br />
PUSH(X-Y)<br />
Z = POP() <br />
</syntaxhighlight><br />
<br />
'''Solution:''' The first POP stores 8 in Y. The second POP stores 6 in X. Then, 6-8=-2 is pushed onto the stack. Finally, the last POP removes the -2 and stores it in Z.<br />
<br />
=== Problem 2 ===<br />
<br />
Create a min-heap with the letters in the word PROGRAMMING. What are the letters in the bottom-most row, from left to right?<br />
<br />
'''Solution:''' The bottom row contains the letters RORN, from left-to-right. Here is the entire heap:<br />
<br />
<center><br />
[[File:heap-programming.svg|200px]]<br />
</center><br />
<br />
=== Problem 3 ===<br />
<br />
Create a binary search tree from the letters in the word PROGRAM. What is the internal path length? <br />
<br />
'''Solution:''' When drawing the tree, P has a depth of 0, O and R have a depth of 1, G and R have a depth of 2, and A and M have a depth of 3. Therefore, the internal path length is 2*1 + 2*2 + 2*3 = 12. Here is the tree:<br />
<br />
<center><br />
[[File:bst-program.svg|200px]]<br />
</center><br />
<br />
= Video Resources =<br />
<br />
== ACSL Videos ==<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--><br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/gXj7K_petqo</youtube><br />
| [https://youtu.be/gXj7K_petqo ''Data Structures (Stacks and Queues)'' ('''Tangerine Code''')]<br />
<br />
A general tutorial on stacks and queues.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/_BnbbOhyroQ</youtube><br />
| [https://youtu.be/_BnbbOhyroQ ''Construct a Binary Search Tree'' ('''Tangerine Code''')]<br />
<br />
Shows how to build a binary search tree from the letters '''S U S H I'''.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/l9aMO7lgHj0</youtube><br />
| [https://youtu.be/l9aMO7lgHj0 ''Binary Search Tree ACSL Problem (Internal Path Length)'' ('''Tangerine Code''')]<br />
<br />
A general tutorial on internal path length of a binary search tree<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/Olu_RbWnHb8</youtube><br />
| [https://youtu.be/Olu_RbWnHb8 ''ACSL Test Prep - Data Structures - Queues, Stacks, Binary Search Trees, and Heaps'' ('''Mrs. Gupta''')]<br />
<br />
A talked-over presentation discussing queues, stacks, binary search trees, and heaps as needed for the American Computer Science League and its tests.<br />
|}<br />
<br />
== Other Videos ==<br />
<br />
There is no shortage of instructional video material covering basic data structures. Here is a series that combines teaching concepts with showing Java code that implements the concepts. Most of the ACSL questions will not involve coding the basic operations on the data structures; rather, the problems will involve high-level understanding of them. However, seeing the code is an excellent way to thoroughly understand these data structures, and what it takes to implement them.<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/wjI1WNcIntg</youtube><br />
| [https://youtu.be/wjI1WNcIntg ''Data Structures: Stacks and Queues'' ('''HackerRank''')]<br />
<br />
The first half of the video is a nice description of stacks and queues; the second half walks through very clean Java code that implements the fundamental methods on these data structures.<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/njTh_OwMljA</youtube><br />
| [https://youtu.be/njTh_OwMljA ''Data Structures: Linked Lists'' ('''HackerRank''')]<br />
<br />
Although ACSL does not cover linked lists per se, they are a great preliminary study for binary search trees. <br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/oSWTXtMglKE</youtube><br />
| [https://youtu.be/oSWTXtMglKE ''Data Structures: Trees'' ('''HackerRank''')]<br />
<br />
Learn about binary search trees. This video is a part of HackerRank's ''Cracking The Coding Interview Tutorial'' with Gayle Laakmann McDowell. The first half of the video is a clear and eloquent description of binary search trees; the second half walks through very clean Java code that implements the fundamental methods. <br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/t0Cq6tVNRBA</youtube><br />
| [https://youtu.be/t0Cq6tVNRBA ''Data Structures: Heaps'' ('''HackerRank''')]<br />
<br />
Learn about heaps. This video is a part of HackerRank's ''Cracking The Coding Interview Tutorial'' with Gayle Laakmann McDowell. The first half of the video is a clear and eloquent description of heaps; the second half walks through very clean Java code that implements the fundamental methods. <br />
|}<br />
<br />
Here are a few more videos covering the basics of binary search trees.<br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/FvdPo8PBQtc</youtube><br />
| [https://youtu.be/FvdPo8PBQtc ''How to Construct a Binary Search Tree'' ('''edutechional''')]<br />
<br />
''In this algorithm tutorial, I walk through how to construct a binary search tree given an unordered array, and then how to find elements inside of the tree.''<br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=FSAs_and_Regular_Expressions&diff=643FSAs and Regular Expressions2020-06-10T06:38:01Z<p>Marc: /* Video Resources */</p>
<hr />
<div>A Finite State Automaton (FSA) is a mathematical model of computation comprising all 4 of the following: 1) a finite number of ''states'', of which exactly one is ''active'' at any given time; 2) ''transition rules'' to change the active state; 3) an ''initial state''; and 4) one or more ''final states''. We can draw an FSA by representing each state as a circle, the final state as a double circle, the start state as the only state with an incoming arrow, and the transition rules as labeled-edges connecting the states. When labels are assigned to states, they appear inside the circle representing the state. <br />
<br />
In this category, FSAs will be limited to parsing strings. That is, determining if a string is valid or not. <br />
<br />
= Basics =<br />
<br />
Here is a drawing of an FSA that is used to parse strings consisting of x's and y's:<br />
<br />
<center><br />
[[File:fsa.svg|250px]]<br />
</center><br />
<br />
In the above FSA, there are three states: A, B, and C. The initial state is A; the final state is C. The only way to go from state A to B is by ''seeing'' the letter x. Once in state B, there are two transition rules: seeing the letter y will cause the FSA to make C the active state, and seeing an x will keep B as the active state. State C is a final state so if the string being parsed is completed and the FSA is in State C, the input string is said to be ''accepted'' by the FSA. In State C, seeing any additional letter y will keep the machine in state C. The FSA above will accept strings composed of one or more x’s followed by one or more y’s (e.g., xy, xxy, xxxyy, xyyy, xxyyyy). <br />
<br />
A Regular Expression (RE) is an algebraic representation of an FSA. For example, the regular expression corresponding to the first FSA given above is xx*yy*. <br />
<br />
The rules for forming a Regular Expression (RE) are as follows:<br />
:1. The null string (λ) is a RE.<br />
:2. If the string a is in the input alphabet, then it is a RE.<br />
:3. if a and b are both REs, then so are the strings built up using the following rules:<br />
::a. CONCATENATION. "ab" (a followed by b).<br />
::b. UNION. "aUb" or "a|b" (a or b). <br />
::c. CLOSURE. "a*" (a repeated zero or more times). This is known as the Kleene Star.<br />
<br />
The order of precedence for Regular Expression operators is: Kleene Star, concatenation, and then union. <br />
Similar to standard Algebra, parentheses can be used to group sub-expressions. <br />
For example, "dca*b" generates strings dcb, dcab, dcaab, and so on, whereas<br />
"d(ca)*b" generates strings db, dcab, dcacab, dcacacab, and so on.<br />
<br />
If we have a Regular Expression, then we can mechanically build an FSA to accept the strings which are generated by the Regular Expression. Conversely, if we have an FSA, we can mechanically develop a Regular Expression which will describe the strings which can be parsed by the FSA. For a given FSA or Regular Expression, there are many others which are equivalent to it. A "most simplified" Regular Expression or FSA is not always well defined.<br />
<br />
= Regular Expression Identities =<br />
<br />
{| Class="wikitable"<br />
|-<br />
|1. (a*)* = a*<br />
|-<br />
|2. aa* = a*a<br />
|-<br />
|3. aa* U λ = a*<br />
|-<br />
|4. a(b U c) = ab U ac<br />
|-<br />
|5. a(ba)* = (ab)*a<br />
|-<br />
|6. (a U b)* = (a* U b*)*<br />
|-<br />
|7. (a U b)* = (a*b*)*<br />
|-<br />
|8. (a U b)* = a*(ba*)*<br />
|}<br />
<br />
= RegEx in Practice =<br />
<br />
Programmers use Regular Expressions (usually referred to as '''regex''') extensively for<br />
expressing patterns to search for. All modern programming languages have regular expression libraries.<br />
<br />
Unfortunately, the specific syntax rules vary depending on the specific <br />
implementation, programming language, or library in use. <br />
Interactive websites for testing regexes are a useful resource for <br />
learning regexes by experimentation. <br />
An excellent online tool is [https://regex101.com/ https://regex101.com/]. <br />
A very nice exposition is [https://automatetheboringstuff.com/2e/chapter7/ Pattern Matching with Regular Expressions] <br />
from the [https://automatetheboringstuff.com/ Automate the Boring Stuff] book and online course.<br />
<br />
Here are the additional syntax rules that we will use. They are pretty universal across all<br />
regex packages. <br />
<br />
{| class="wikitable" style="text-align: left"|<br />
|-<br />
!Pattern<br />
!Description<br />
|-<br />
! <nowiki>|</nowiki><br />
|As described above, a vertical bar separates alternatives. For example, gray<nowiki>|</nowiki>grey can match "gray" or "grey".<br />
|-<br />
!*<br />
|As described above, the asterisk indicates zero or more occurrences of the preceding element. For example, ab*c matches "ac", "abc", "abbc", "abbbc", and so on.<br />
|-<br />
!?<br />
| The question mark indicates zero or one occurrences of the preceding element. For example, colou?r matches both "color" and "colour".<br />
|-<br />
! +<br />
| The plus sign indicates one or more occurrences of the preceding element. For example, ab+c matches "abc", "abbc", "abbbc", and so on, but not "ac".<br />
|-<br />
! .<br />
| The wildcard . matches any character. For example, a.b matches any string that contains an "a", then any other character, and then a "b" such as "a7b", "a&b", or "arb", but not "abbb". Therefore, a.*b matches any string that contains an "a" and a "b" with 0 or more characters in between. This includes "ab", "acb", or "a123456789b".<br />
|-<br />
! [ ]<br />
| A bracket expression matches a single character that is contained within the brackets. For example, [abc] matches "a", "b", or "c". [a-z] specifies a range which matches any lowercase letter from "a" to "z". These forms can be mixed: [abcx-z] matches "a", "b", "c", "x", "y", or "z", as does [a-cx-z].<br />
|-<br />
! [^ ]<br />
|Matches a single character that is not contained within the brackets. For example, [^abc] matches any character other than "a", "b", or "c". [^a-z] matches any single character that is not a lowercase letter from "a" to "z". Likewise, literal characters and ranges can be mixed.<br />
|-<br />
!( )<br />
| <br />
As described above, parentheses define a sub-expression. For example, the pattern H(ä|ae?)ndel matches "Handel", "Händel", and "Haendel".<br />
|}<br />
<br />
= Sample Problems =<br />
<br />
Typical problems in the category will include: translate an FSA to a Regular Expression; simplify a Regular Expression; determine which Regular Expressions or FSAs are equivalent; and determine which strings are accepted by either an FSA or a Regular Expression.<br />
<br />
== Problem 1 ==<br />
<br />
Find a simplified Regular Expression for the following FSA:<br />
<br />
[[File:fsa_s1.png]]<br />
<br />
'''Solution:'''<br />
<br />
The expression 01*01 is read directly from the FSA. It is in its most simplified form.<br />
<br />
== Problem 2 ==<br />
<br />
Which of the following strings are accepted by the following Regular Expression "00*1*1U11*0*0" ?<br />
<br />
::A. 0000001111111<br />
::B. 1010101010<br />
::C. 1111111<br />
::D. 0110<br />
::E. 10 <br />
<br />
'''Solution:'''<br />
<br />
This Regular Expression parses strings described by the union of 00*1*1 and 11*0*0. The RE 00*1*1 matches strings starting with one or more 0s followed by one or more 1s: 01, 001, 0001111, and so on. The RE 11*0*0 matches strings with one or more 1s followed by one or more 0s: 10, 1110, 1111100, and so on. In other words, strings of the form: 0s followed by some 1s; or 1s followed by some 0s. Choice A and E following this pattern.<br />
<br />
<!--<br />
== Problem 3 ==<br />
<br />
Which, if any, of the following Regular Expressions are equivalent?<br />
::A. (a U b)(ab*)(b* U a)<br />
::B. (aab* U bab*)a<br />
::C. aab* U bab* U aaba U bab*a<br />
::D. aab* U bab* U aab*a U bab*a<br />
::E. a* U b* <br />
<br />
'''Solution:'''<br />
<br />
Choice B can be discarded because it is the only RE whose strings '''must''' end with an a. Choice E can be discarded since it is the only RE that can accept a null string. Choices C and D are not equal. After expanding choice A, we must compare it to choices C and D. It is equal to choice D, but not to choice C. The only REs that are equivalent are choices A and D.<br />
--><br />
== Problem 3 ==<br />
<br />
Which of the following strings match the regular expression pattern "[A-D]*[a-d]*[0-9]" ?<br />
<br />
::1. ABCD8<br />
::2. abcd5<br />
::3. ABcd9<br />
::4. AbCd7<br />
::5. X<br />
::6. abCD7<br />
::7. DCCBBBaaaa5<br />
<br />
'''Solution:'''<br />
<br />
The pattern describes strings the start with zero or more uppercase letters A, B, C, or D (in any order), followed<br />
by zero or more lowercase letter a, b, c, or d (in any order), followed by a single digit.<br />
The strings that are represented by this pattern are 1, 2, 3, and 7.<br />
<br />
== Problem 4 ==<br />
<br />
Which of the following strings match the regular expression pattern "Hi?g+h+[^a-ceiou]" ?<br />
<br />
::1. Highb<br />
::2. HiiighS<br />
::3. HigghhhC<br />
::4. Hih<br />
::5. Hghe<br />
::6. Highd<br />
::7. HgggggghX<br />
<br />
'''Solution:'''<br />
<br />
The ? indicates 0 or 1 "i"s. The + indicates 1 or more "g"s followed by 1 or more "h"s. <br />
The ^ indicates that the last character cannot be lower-case a, b, c, e, i, o, or u.<br />
The strings that are represented by this pattern are 3, 6, and 7. <br />
<br />
<!--<br />
== Problem 5 ==<br />
<br />
Which of the following strings match the regular expression pattern "[A-E|a-e]*(00[01])|([10]11)" ?<br />
<br />
::1. DAD001<br />
::2. bad000<br />
::3. aCe0011<br />
::4. AbE111<br />
::5. AAAbbC<br />
::6. aBBBe011<br />
::7. 001011<br />
<br />
'''Solution:'''<br />
<br />
The [A-E|a-e]* allows for any of those letters in any order 0 or more times. Therefore, all of the <br />
choices match at the beginning of the string. The end of the string must match "000", "001", "111", <br />
or "011". That means that 1, 2, 4, and 6 match.<br />
--><br />
<br />
= Video Resources =<br />
<br />
Nice two-part video showing the relationship between FSAs and REs. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/GwsU2LPs85U</youtube><br />
| [https://youtu.be/GwsU2LPs85U ''1 - Convert Regular Expression to Finite-State Automaton'' ('''Barry Brown''')]<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/shN_kHBFOUE</youtube><br />
| [https://youtu.be/shN_kHBFOUE ''2 - Convert Regular Expression to Finite-State Automaton'' ('''Barry Brown''')]<br />
<br />
This video uses the symbol "+" to mean "1 or more matches of the previous term". For example, "ab+" is the same as "abb*". In terms of the Kleene Star, zz* = z*z = z+.<br />
|}<br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/vI_yv0WuAhk</youtube><br />
| [https://youtu.be/vI_yv0WuAhk ''ACSL Test Prep - Finite State Automaton & Regular Expressions Explained'' ('''Mrs. Gupta''')]<br />
<br />
A talked-over presentation discussing the finite state automatons and regular expressions as needed for the American Computer Science League and its tests. <br />
|}<br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--></div>Marchttp://www.categories.acsl.org/wiki/index.php?title=FSAs_and_Regular_Expressions&diff=642FSAs and Regular Expressions2020-04-25T17:51:14Z<p>Marc: /* RegEx in Practice */</p>
<hr />
<div>A Finite State Automaton (FSA) is a mathematical model of computation comprising all 4 of the following: 1) a finite number of ''states'', of which exactly one is ''active'' at any given time; 2) ''transition rules'' to change the active state; 3) an ''initial state''; and 4) one or more ''final states''. We can draw an FSA by representing each state as a circle, the final state as a double circle, the start state as the only state with an incoming arrow, and the transition rules as labeled-edges connecting the states. When labels are assigned to states, they appear inside the circle representing the state. <br />
<br />
In this category, FSAs will be limited to parsing strings. That is, determining if a string is valid or not. <br />
<br />
= Basics =<br />
<br />
Here is a drawing of an FSA that is used to parse strings consisting of x's and y's:<br />
<br />
<center><br />
[[File:fsa.svg|250px]]<br />
</center><br />
<br />
In the above FSA, there are three states: A, B, and C. The initial state is A; the final state is C. The only way to go from state A to B is by ''seeing'' the letter x. Once in state B, there are two transition rules: seeing the letter y will cause the FSA to make C the active state, and seeing an x will keep B as the active state. State C is a final state so if the string being parsed is completed and the FSA is in State C, the input string is said to be ''accepted'' by the FSA. In State C, seeing any additional letter y will keep the machine in state C. The FSA above will accept strings composed of one or more x’s followed by one or more y’s (e.g., xy, xxy, xxxyy, xyyy, xxyyyy). <br />
<br />
A Regular Expression (RE) is an algebraic representation of an FSA. For example, the regular expression corresponding to the first FSA given above is xx*yy*. <br />
<br />
The rules for forming a Regular Expression (RE) are as follows:<br />
:1. The null string (λ) is a RE.<br />
:2. If the string a is in the input alphabet, then it is a RE.<br />
:3. if a and b are both REs, then so are the strings built up using the following rules:<br />
::a. CONCATENATION. "ab" (a followed by b).<br />
::b. UNION. "aUb" or "a|b" (a or b). <br />
::c. CLOSURE. "a*" (a repeated zero or more times). This is known as the Kleene Star.<br />
<br />
The order of precedence for Regular Expression operators is: Kleene Star, concatenation, and then union. <br />
Similar to standard Algebra, parentheses can be used to group sub-expressions. <br />
For example, "dca*b" generates strings dcb, dcab, dcaab, and so on, whereas<br />
"d(ca)*b" generates strings db, dcab, dcacab, dcacacab, and so on.<br />
<br />
If we have a Regular Expression, then we can mechanically build an FSA to accept the strings which are generated by the Regular Expression. Conversely, if we have an FSA, we can mechanically develop a Regular Expression which will describe the strings which can be parsed by the FSA. For a given FSA or Regular Expression, there are many others which are equivalent to it. A "most simplified" Regular Expression or FSA is not always well defined.<br />
<br />
= Regular Expression Identities =<br />
<br />
{| Class="wikitable"<br />
|-<br />
|1. (a*)* = a*<br />
|-<br />
|2. aa* = a*a<br />
|-<br />
|3. aa* U λ = a*<br />
|-<br />
|4. a(b U c) = ab U ac<br />
|-<br />
|5. a(ba)* = (ab)*a<br />
|-<br />
|6. (a U b)* = (a* U b*)*<br />
|-<br />
|7. (a U b)* = (a*b*)*<br />
|-<br />
|8. (a U b)* = a*(ba*)*<br />
|}<br />
<br />
= RegEx in Practice =<br />
<br />
Programmers use Regular Expressions (usually referred to as '''regex''') extensively for<br />
expressing patterns to search for. All modern programming languages have regular expression libraries.<br />
<br />
Unfortunately, the specific syntax rules vary depending on the specific <br />
implementation, programming language, or library in use. <br />
Interactive websites for testing regexes are a useful resource for <br />
learning regexes by experimentation. <br />
An excellent online tool is [https://regex101.com/ https://regex101.com/]. <br />
A very nice exposition is [https://automatetheboringstuff.com/2e/chapter7/ Pattern Matching with Regular Expressions] <br />
from the [https://automatetheboringstuff.com/ Automate the Boring Stuff] book and online course.<br />
<br />
Here are the additional syntax rules that we will use. They are pretty universal across all<br />
regex packages. <br />
<br />
{| class="wikitable" style="text-align: left"|<br />
|-<br />
!Pattern<br />
!Description<br />
|-<br />
! <nowiki>|</nowiki><br />
|As described above, a vertical bar separates alternatives. For example, gray<nowiki>|</nowiki>grey can match "gray" or "grey".<br />
|-<br />
!*<br />
|As described above, the asterisk indicates zero or more occurrences of the preceding element. For example, ab*c matches "ac", "abc", "abbc", "abbbc", and so on.<br />
|-<br />
!?<br />
| The question mark indicates zero or one occurrences of the preceding element. For example, colou?r matches both "color" and "colour".<br />
|-<br />
! +<br />
| The plus sign indicates one or more occurrences of the preceding element. For example, ab+c matches "abc", "abbc", "abbbc", and so on, but not "ac".<br />
|-<br />
! .<br />
| The wildcard . matches any character. For example, a.b matches any string that contains an "a", then any other character, and then a "b" such as "a7b", "a&b", or "arb", but not "abbb". Therefore, a.*b matches any string that contains an "a" and a "b" with 0 or more characters in between. This includes "ab", "acb", or "a123456789b".<br />
|-<br />
! [ ]<br />
| A bracket expression matches a single character that is contained within the brackets. For example, [abc] matches "a", "b", or "c". [a-z] specifies a range which matches any lowercase letter from "a" to "z". These forms can be mixed: [abcx-z] matches "a", "b", "c", "x", "y", or "z", as does [a-cx-z].<br />
|-<br />
! [^ ]<br />
|Matches a single character that is not contained within the brackets. For example, [^abc] matches any character other than "a", "b", or "c". [^a-z] matches any single character that is not a lowercase letter from "a" to "z". Likewise, literal characters and ranges can be mixed.<br />
|-<br />
!( )<br />
| <br />
As described above, parentheses define a sub-expression. For example, the pattern H(ä|ae?)ndel matches "Handel", "Händel", and "Haendel".<br />
|}<br />
<br />
= Sample Problems =<br />
<br />
Typical problems in the category will include: translate an FSA to a Regular Expression; simplify a Regular Expression; determine which Regular Expressions or FSAs are equivalent; and determine which strings are accepted by either an FSA or a Regular Expression.<br />
<br />
== Problem 1 ==<br />
<br />
Find a simplified Regular Expression for the following FSA:<br />
<br />
[[File:fsa_s1.png]]<br />
<br />
'''Solution:'''<br />
<br />
The expression 01*01 is read directly from the FSA. It is in its most simplified form.<br />
<br />
== Problem 2 ==<br />
<br />
Which of the following strings are accepted by the following Regular Expression "00*1*1U11*0*0" ?<br />
<br />
::A. 0000001111111<br />
::B. 1010101010<br />
::C. 1111111<br />
::D. 0110<br />
::E. 10 <br />
<br />
'''Solution:'''<br />
<br />
This Regular Expression parses strings described by the union of 00*1*1 and 11*0*0. The RE 00*1*1 matches strings starting with one or more 0s followed by one or more 1s: 01, 001, 0001111, and so on. The RE 11*0*0 matches strings with one or more 1s followed by one or more 0s: 10, 1110, 1111100, and so on. In other words, strings of the form: 0s followed by some 1s; or 1s followed by some 0s. Choice A and E following this pattern.<br />
<br />
<!--<br />
== Problem 3 ==<br />
<br />
Which, if any, of the following Regular Expressions are equivalent?<br />
::A. (a U b)(ab*)(b* U a)<br />
::B. (aab* U bab*)a<br />
::C. aab* U bab* U aaba U bab*a<br />
::D. aab* U bab* U aab*a U bab*a<br />
::E. a* U b* <br />
<br />
'''Solution:'''<br />
<br />
Choice B can be discarded because it is the only RE whose strings '''must''' end with an a. Choice E can be discarded since it is the only RE that can accept a null string. Choices C and D are not equal. After expanding choice A, we must compare it to choices C and D. It is equal to choice D, but not to choice C. The only REs that are equivalent are choices A and D.<br />
--><br />
== Problem 3 ==<br />
<br />
Which of the following strings match the regular expression pattern "[A-D]*[a-d]*[0-9]" ?<br />
<br />
::1. ABCD8<br />
::2. abcd5<br />
::3. ABcd9<br />
::4. AbCd7<br />
::5. X<br />
::6. abCD7<br />
::7. DCCBBBaaaa5<br />
<br />
'''Solution:'''<br />
<br />
The pattern describes strings the start with zero or more uppercase letters A, B, C, or D (in any order), followed<br />
by zero or more lowercase letter a, b, c, or d (in any order), followed by a single digit.<br />
The strings that are represented by this pattern are 1, 2, 3, and 7.<br />
<br />
== Problem 4 ==<br />
<br />
Which of the following strings match the regular expression pattern "Hi?g+h+[^a-ceiou]" ?<br />
<br />
::1. Highb<br />
::2. HiiighS<br />
::3. HigghhhC<br />
::4. Hih<br />
::5. Hghe<br />
::6. Highd<br />
::7. HgggggghX<br />
<br />
'''Solution:'''<br />
<br />
The ? indicates 0 or 1 "i"s. The + indicates 1 or more "g"s followed by 1 or more "h"s. <br />
The ^ indicates that the last character cannot be lower-case a, b, c, e, i, o, or u.<br />
The strings that are represented by this pattern are 3, 6, and 7. <br />
<br />
<!--<br />
== Problem 5 ==<br />
<br />
Which of the following strings match the regular expression pattern "[A-E|a-e]*(00[01])|([10]11)" ?<br />
<br />
::1. DAD001<br />
::2. bad000<br />
::3. aCe0011<br />
::4. AbE111<br />
::5. AAAbbC<br />
::6. aBBBe011<br />
::7. 001011<br />
<br />
'''Solution:'''<br />
<br />
The [A-E|a-e]* allows for any of those letters in any order 0 or more times. Therefore, all of the <br />
choices match at the beginning of the string. The end of the string must match "000", "001", "111", <br />
or "011". That means that 1, 2, 4, and 6 match.<br />
--><br />
<br />
= Video Resources =<br />
<br />
Nice two-part video showing the relationship between FSAs and REs. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/GwsU2LPs85U</youtube><br />
| [https://youtu.be/GwsU2LPs85U ''1 - Convert Regular Expression to Finite-State Automaton'' ('''Barry Brown''')]<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/shN_kHBFOUE</youtube><br />
| [https://youtu.be/shN_kHBFOUE ''2 - Convert Regular Expression to Finite-State Automaton'' ('''Barry Brown''')]<br />
<br />
This video uses the symbol "+" to mean "1 or more matches of the previous term". For example, "ab+" is the same as "abb*". In terms of the Kleene Star, zz* = z*z = z+.<br />
|}<br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--></div>Marchttp://www.categories.acsl.org/wiki/index.php?title=LISP&diff=639LISP2019-04-16T17:21:02Z<p>Marc: /* Basic Functions (SET, SETQ, EVAL, ATOM) */</p>
<hr />
<div>LISP is one of the simplest computer languages in terms of syntax and semantics, and also one of the most powerful. It was developed in the mid-1950’s by John McCarthy at M.I.T. as a “LISt Processing language.” It has been historically used for virtually all Artificial Intelligence programs and is often the environment of choice for applications which require a powerful interactive working environment. LISP presents a very different way to think about programming from the “algorithmic” languages, such as Python, C++, and Java.<br />
<br />
== Syntax ==<br />
<br />
As its name implies, the basis of LISP is a list. One constructs a list by enumerating elements inside a pair of parentheses. For example, here is a list with four elements (the second element is also a list):<br />
<br />
:<code>(23 (this is easy) hello 821)</code><br />
<br />
The elements in the list, which are not lists, are called "atoms." For example, the atoms in the list above are: 23, 'this, 'hello, 821, 'easy, and 'is. Literals are identified with a single leading quote. Everything in LISP is either an atom or a list (but not both). The only exception is "NIL," which is both an atom and a list. It can also be written as "()" – a pair of parentheses with nothing inside.<br />
<br />
All statements in LISP are function calls with the following syntax: (function arg1 arg2 arg3 … argn). To evaluate a LISP statement, each of the arguments (possibly functions themselves) are evaluated, and then the function is invoked with the arguments. For example, (MULT (ADD 2 3) (ADD 1 4 2)) has a value of 35, since (ADD 2 3) has a value of 5, (ADD 1 4 2) has a value of 7, and (MULT 5 7) has a value of 35. Some functions have an arbitrary number of arguments; others require a fixed number. All statements return a value, which is either an atom or a list.<br />
<br />
== Basic Functions (SET, SETQ, EVAL, ATOM) ==<br />
<br />
We may assign values to variables using the function SET. For example, the statement (SET ’test 6) would have a value of a 6, and (more importantly, however) would also cause the atom ‘test to be bound to the atom 6. The function SETQ is the same as SET, but it causes LISP to act as if the first argument was quoted. <br />
Observe the following examples:<br />
<br />
:{| class="wikitable" style="text-align: left"<br />
!Statement || Value || Comment <br />
|-<br />
|(SET ’a (MULT 2 3))<br />
|6<br />
|a is an atom with a value of 6<br />
|-<br />
|(SET ’a ’(MULT 2 3))<br />
|(MULT 2 3)<br />
|a is a list with 3 elements<br />
|-<br />
|(SET ’b ’a)<br />
|a<br />
|b is an atom with a value of the character a<br />
|-<br />
|(SET ’c a)<br />
|(MULT 2 3)<br />
|c is a list with 3 elements<br />
|-<br />
|(SETQ EX (ADD 3 (MULT 2 5)))<br />
|13<br />
|The variable EX has a value of 13<br />
|-<br />
|(SETQ VOWELS ’(A E I O U))<br />
|(A E I O U)<br />
|VOWELS is a list of 5 elements<br />
|}<br />
<br />
The function EVAL returns the value of its argument, after it has been evaluated. For example, (SETQ z ’(ADD 2 3)) has a value of the list (ADD 2 3); the function (EVAL ’z) has a value of (ADD 2 3); the function (EVAL z) has a value of 5 (but the binding of the atom z has not changed). In this last example, you can think of z being “resolved” twice: once because it is an argument to a function and LISP evaluates all arguments to functions before the function is invoked, and once when the function EVAL is invoked to resolve arguments. The function ATOM can be used to determine whether an item is an atom or a list. It returns either "true", or "NIL" for false. Consider the following examples:<br />
<br />
:{| class="wikitable" style="text-align: left"<br />
!Statement || Value || Comment <br />
|-<br />
|(SETQ p '(ADD 1 2 3 4))<br />
|(ADD 1 2 3 4)<br />
|p is a list with 5 elements<br />
|-<br />
|(ATOM 'p)<br />
|true<br />
|The argument to ATOM is the atom p<br />
|-<br />
|(ATOM p)<br />
|NIL<br />
|Because p is not quoted, it is evaluated to the 5-element list.<br />
|-<br />
|(EVAL p)<br />
|10<br />
|The argument to EVAL is the value of p; the value of p is 10. <br />
|}<br />
<br />
== List Functions (CAR, CDR, CONS, REVERSE)==<br />
<br />
The two most famous LISP functions are CAR and CDR (pronounced: could-er), named after registers of a now long-forgotten IBM machine on which LISP was first developed. The function (CAR x) returns the first item of the list x (and x must be a list or an error will occur); (CDR x) returns the list without its first element (again, x must be a list). The function CONS takes two arguments, of which the second must be a list. It returns a list which is composed by placing the first argument as the first element in the second argument’s list. The function REVERSE returns a list which is its arguments in reverse order. <br />
<br />
The CAR and CDR functions are used extensively to grab specific elements of a list or sublist, that there's a shorthand for this: (CADR x) is the same as (CAR (CDR x)), which retrieves the second element of the list x; (CAADDAR x) is a shorthand for (CAR (CAR (CDR (CDR (CAR x))))), and so on.<br />
<br />
The following examples illustrate the use of CAR, CDR, CONS, and REVERSE: <br />
<br />
:{| class="wikitable" style="text-align: left"<br />
!Statement || Value<br />
|-<br />
|(CAR '(This is a list))<br />
|This<br />
|-<br />
|(CDR '(This is a list))<br />
|(is a list)<br />
|-<br />
|(CONS 'red '(white blue))<br />
|(red white blue)<br />
|-<br />
|(SETQ z (CONS '(red white blue) (CDR '(This is a list))))<br />
|((red white blue) is a list)<br />
|-<br />
|(REVERSE z)<br />
|(list a is (red white blue))<br />
|-<br />
|(CDDAR z)<br />
|(blue)<br />
|}<br />
<br />
== Arithmetic Functions (ADD, MULT, ...)==<br />
<br />
As you have probably already figured out, the function ADD simply summed its arguments. We’ll also be using the following arithmetic functions:<br />
<br />
:{| class="wikitable" style="text-align: left"<br />
!'''Function''' || '''Result'''<br />
|-<br />
|(ADD x1 x2 …)<br />
|sum of all arguments<br />
|-<br />
|(SUB a b)<br />
|a-b<br />
|-<br />
|(MULT x1 x2 …)<br />
|product of all arguments<br />
|-<br />
|(DIV a b)<br />
|a/b<br />
|-<br />
|(SQUARE a)<br />
|a*a<br />
|-<br />
|(EXP a n)<br />
|a<sup>n</sup><br />
|-<br />
|(EQ a b)<br />
|true if a and b are equal, NIL otherwise<br />
|-<br />
|(POS a)<br />
|true if a is positive, NIL otherwise<br />
|-<br />
|(NEG a)<br />
|true if a is negative, NIL otherwise<br />
|-<br />
|}<br />
<br />
Functions ADD, SUB, MULT, and DIV can be written as their common mathematical symbols, +, -, *, and /. Here are some examples of these functions:<br />
<br />
:{| class="wikitable"<br />
!'''Statement''' || '''Value'''<br />
|-<br />
|(ADD (EXP 2 3) (SUB 4 1) (DIV 54 4))<br />
|24.5<br />
|-<br />
|(- (* 3 2) (- 12 (+ 1 2 1)))<br />
| -2<br />
|-<br />
|(ADD (SQUARE 3) (SQUARE 4))<br />
|25<br />
|}<br />
<br />
== User-defined Functions ==<br />
<br />
LISP also allows us to create our own functions using the DEF function. (We will sometimes use DEFUN rather than DEF, as it is a bit more standard terminology.) For example,<br />
(DEF SECOND (args) (CAR (CDR args)))<br />
defines a new function called SECOND which operates on a single parameter named “args”. SECOND will take the CDR of the parameter and then the CAR of that result. So, for example:<br />
(SECOND ’(a b c d e))<br />
would first CDR the list to give (b c d e), and then CAR that value returning the single character “b”. Consider the following program fragment:<br />
(SETQ X ’(a c s l))<br />
(DEF WHAT(args) (CONS args (REVERSE (CDR args))))<br />
(DEF SECOND(args) (CONS (CAR (CDR args)) NIL))<br />
<br />
The following chart illustrates the use of the user-defined functions WHAT and SECOND:<br />
<br />
:{| class="wikitable"<br />
!Statement || Value<br />
|-<br />
|(WHAT X) || ((a c s l) l s c)<br />
|-<br />
|(SECOND X) || (c)<br />
|-<br />
|(SECOND (WHAT X)) || (l)<br />
|-<br />
|(WHAT (SECOND X)) || ((c))<br />
|}<br />
<br />
== Online Interpreters ==<br />
<br />
There are many online LISP interpreters available on the Internet. The one that ACSL uses for testing its programs is CLISP that is accessible from [https://www.jdoodle.com/execute-clisp-online JDoodle]. This interpreter is nice because it is quite peppy in running programs, and functions are not case sensitive. So, <code>(CAR (CDR x))</code> is legal as is <code>(car (cdr x))</code> One drawback of this interpreter is the print function changes lowercase input into uppercase. <br />
<br />
== Sample Problems ==<br />
<br />
Questions from this topic will typically present a line of LISP code or a short sequence of statements and ask what is the value of the (final) statement.<br />
<br />
=== Problem 1 ===<br />
<br />
Evaluate the following expression. <code>(MULT (ADD 6 5 0) (MULT 5 1 2 2) (DIV 9 (SUB 2 5)))</code><br />
<br />
'''Solution:'''<br />
(MULT (ADD 6 5 0) (MULT 5 1 2 2) (DIV 6 (SUB 2 5)))<br />
(MULT 11 20 (DIV 6 -3))<br />
(MULT 11 20 -2)<br />
-440<br />
<br />
=== Problem 2 ===<br />
<br />
Evaluate the following expression: <code>(CDR ’((2 (3))(4 (5 6) 7)))</code><br />
<br />
'''Solution:'''<br />
The CDR function takes the first element of its parameter (which is assumed to be a list) and returns the list with the first element removed. The first element of the list is (2 (3)) and the list without this element is<br />
((4 (5 6) 7)), a list with one element.<br />
<br />
=== Problem 3 ===<br />
<br />
Consider the following program fragment:<br />
(SETQ X ’(RI VA FL CA TX))<br />
(CAR (CDR (REVERSE X)))<br />
What is the value of the CAR expression? <br />
<br />
'''Solution:'''<br />
The first statement binds variable X to the list ‘(RI VA FL CA TX).<br />
The REVERSE of this list is ‘(TX CA FL VA RI)<br />
whose CDR is ‘(CA FL VA RI). The CAR of this list is just the atom CA (without the quote).<br />
<br />
<!-- <br />
<br />
Too scary of a sample problem! --marc 9/3/2018<br />
<br />
=== Problem 4 ===<br />
<br />
Given the function definitions for HY and FY as follows:<br />
(DEFUN HY(PARM) (REVERSE (CDR PARM)))<br />
(DEFUN FY(PARM) (CAR (HY (CDR PARM))))<br />
What is the value of the following?<br />
(FY ’(DO RE (MI FA) SO))<br />
<br />
'''Solution:'''<br />
To evaluate (FY ’(DO RE (MI FA) SO)), we must evaluate<br />
(CAR (HY (CDR ’(DO RE (MI FA) SO)))).<br />
Thus, HY is invoked with <br />
PARM = ’(RE (MI FA) SO), and we evaluate<br />
(REVERSE (CDR ’(RE (MI FA) SO)))<br />
This has a value of (SO (MI FA)) which is returned to FY. FY now takes the CAR of this which is SO (without the quotes).<br />
--><br />
<br />
== Video Resources ==<br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--><br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/jWFgmE279eQ</youtube><br />
| [https://youtu.be/jWFgmE279eQ ''LISP very gentle intro'' ('''CalculusNguyenify''')]<br />
<br />
A very gentle introduction to the LISP category, covering the LISP syntax and the basic operators, SETQ, ADD, SUB, MULT, and DIV.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/mRpbbss48sw</youtube><br />
| [https://youtu.be/mRpbbss48sw ''LISP Basics (for ACSL)'' ('''Tangerine Code''')]<br />
<br />
Explains the LISP operators CAR, CDR, and REVERSE, in the context of solving an ACSL All-Star Contest problem.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/50wj_f51kBM</youtube><br />
| [https://youtu.be/50wj_f51kBM ''LISP Basics (for ACSL)'' ('''Tangerine Code''')]<br />
<br />
Completes the problem that was started in the above video. <br />
|}</div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Assembly_Language_Programming&diff=638Assembly Language Programming2019-03-19T19:11:06Z<p>Marc: /* Reference Manual */</p>
<hr />
<div>Programs written in high-level languages are traditionally converted by compilers into assembly language, which is turned into machine language programs – sequences of 1’s and 0’s – by an assembler. Even today, with very good quality compilers available, there is the need for programmers to understand assembly language. First, it provides programmers with a better understanding of the compiler and its constraints. Second, on occasion, programmers find themselves needing to program directly in assembly language in order to meet constraints in execution speed or space. <br />
<br />
ACSL chose to define its own assembly language rather than use a “real” one in order to eliminate the many sticky details associated with real languages. The basic concepts of our ACSL topic description are common to all assembly languages. <br />
<br />
== Reference Manual ==<br />
<br />
Execution starts at the first line of the program and continues sequentially, except for branch instructions (BG, BE, BL, BU), until the end instruction (END) is encountered. The result of each operation is stored in a special word of memory, called the “accumulator” (ACC). The initial value of the ACC is 0. Each line of an assembly language program has the following fields:<br />
<br />
LABEL OPCODE LOC<br />
<br />
The LABEL field, if present, is an alphanumeric character string beginning in the first column. A label must begin with an alphabetic character(A through Z, or a through z), and labels are case-sensitive. Valid OPCODEs are listed in the chart below; they are also case-sensitive and uppercase. Opcodes are reserved words of the language and (the uppercase version) many not be used a label. The LOC field is either a reference to a label or ''immediate data''. For example, “LOAD A” would put the contents referenced by the label “A” into the ACC; “LOAD =123” would store the value 123 in the ACC. Only those instructions that do not modify the LOC field can use the “immediate data” format. In the following chart, they are indicated by an asterisk in the first column.<br />
<br />
{| class="wikitable" style="text-align: left"|<br />
|-<br />
!OP CODE<br />
!DESCRIPTION<br />
<br />
|-<br />
!*LOAD<br />
|The contents of LOC are placed in the ACC. LOC is unchanged.<br />
<br />
|-<br />
!STORE<br />
|The contents of the ACC are placed in the LOC. ACC is unchanged.<br />
<br />
|-<br />
!*ADD<br />
|The contents of LOC are added to the contents of the ACC. The sum is stored in the ACC. LOC is unchanged. Addition is modulo 1,000,000.<br />
<br />
|-<br />
!*SUB<br />
|The contents of LOC are subtracted from the contents of the ACC. The difference is stored in the ACC. LOC is unchanged. Subtraction is modulo 1,000,000.<br />
<br />
|-<br />
!*MULT<br />
|The contents of LOC are multiplied by the contents of the ACC. The product is stored in the ACC. LOC is unchanged. Multiplication is modulo 1,000,000.<br />
<br />
|-<br />
!*DIV<br />
|The contents of LOC are divided into the contents of the ACC. The signed integer part of the quotient is stored in the ACC. LOC is unchanged.<br />
.<br />
|-<br />
!BG<br />
|<br />
Branch to the instruction labeled with LOC if ACC>0.<br />
<br />
|-<br />
!BE<br />
|<br />
Branch to the instruction labeled with LOC if ACC=0.<br />
<br />
|-<br />
!BL<br />
|<br />
Branch to the instruction labeled with LOC if ACC<0.<br />
<br />
|-<br />
!BU<br />
|Branch to the instruction labeled with LOC.<br />
<br />
|-<br />
!READ<br />
|<br />
Read a signed integer (modulo 1,000,000) into LOC.<br />
<br />
|-<br />
!PRINT<br />
|<br />
Print the contents of LOC.<br />
<br />
|-<br />
!DC<br />
|<br />
The value of the memory word defined by the LABEL field is defined to contain the specified constant. The LABEL field is mandatory for this opcode. The ACC is not modified.<br />
<br />
|-<br />
!END<br />
| <br />
Program terminates. LOC field is ignored and must be empty.<br />
|}<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
After the following program is executed, <br />
what value is in location TEMP?<br />
<br />
{|class="wikitable" style="text-align: left"<br />
|TEMP || DC || 0<br />
|-<br />
|A||DC||8<br />
|-<br />
|B||DC||-2<br />
|-<br />
|C||DC||3<br />
|-<br />
| ||LOAD||B<br />
|-<br />
| ||MULT||C<br />
|-<br />
| ||ADD||A<br />
|-<br />
| ||DIV||B<br />
|-<br />
| ||SUB||A<br />
|-<br />
| ||STORE||TEMP<br />
|-<br />
| ||END||<br />
|}<br />
<br />
'''Solution:''' The ACC takes on values -2, -6, 2, -1, and -9 in that order. The last value, -9, is stored in location TEMP.<br />
<br />
=== Problem 2 ===<br />
<br />
If the following program has an input value<br />
of N, what is the final value of X which is<br />
computed? Express X as an algebraic<br />
expression in terms of N. <br />
<br />
{|class="wikitable" style="text-align: left"<br />
| || READ || X<br />
|-<br />
| || LOAD || X<br />
|-<br />
|TOP || SUB || =1<br />
|-<br />
| || BE || DONE<br />
|-<br />
| || STORE || A<br />
|-<br />
| || MULT || X<br />
|-<br />
| || STORE || X<br />
|-<br />
| || LOAD || A<br />
|-<br />
| || BU || TOP<br />
|-<br />
|DONE || END || <br />
|}<br />
<br />
'''Solution:''' This program loops between labels TOP and<br />
DONE for A times. A has an initial value of X and subsequent terms of N,<br />
then values of A-1, A-2, …, 1. Each time through the loop,<br />
X is multiplied by the the current value of A.<br />
Thus, X = A * (A-1) * (A-2) * … * 1 or X=A! or A factorial.<br />
For example, 5! = 5 * 4 * 3 * 2 * 1 = 120. Since the<br />
initial value of A is the number input (i.e. N),<br />
the algebraic expression is X = N!.<br />
<br />
== Video Resources ==<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/RUm3iHsbO3I</youtube><br />
| [https://youtu.be/RUm3iHsbO3I ''Intro to Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A general introduction into assembly language. In particular, it covers how it fits into the source code to an executable image pipeline.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/i_JYT398O64</youtube><br />
| [https://youtu.be/i_JYT398O64 ''Syntax of ACSL Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A very nice introduction to this ACSL category.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/NEQASUsZ0g4</youtube><br />
| [https://youtu.be/NEQASUsZ0g4 ''Examples'' ('''CalculusNguyenify''')]<br />
<br />
Walks through a couple of ACSL Assembly language programs that have been used in previous contests.<br />
<br />
|}<br />
<br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--></div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Assembly_Language_Programming&diff=637Assembly Language Programming2019-03-13T04:00:30Z<p>Marc: /* Reference Manual */</p>
<hr />
<div>Programs written in high-level languages are traditionally converted by compilers into assembly language, which is turned into machine language programs – sequences of 1’s and 0’s – by an assembler. Even today, with very good quality compilers available, there is the need for programmers to understand assembly language. First, it provides programmers with a better understanding of the compiler and its constraints. Second, on occasion, programmers find themselves needing to program directly in assembly language in order to meet constraints in execution speed or space. <br />
<br />
ACSL chose to define its own assembly language rather than use a “real” one in order to eliminate the many sticky details associated with real languages. The basic concepts of our ACSL topic description are common to all assembly languages. <br />
<br />
== Reference Manual ==<br />
<br />
Execution starts at the first line of the program and continues sequentially, except for branch instructions (BG, BE, BL, BU), until the end instruction (END) is encountered. The result of each operation is stored in a special word of memory, called the “accumulator” (ACC). The initial value of the ACC is 0. Each line of an assembly language program has the following fields:<br />
<br />
LABEL OPCODE LOC<br />
<br />
The LABEL field, if present, is an alphanumeric character string beginning in the first column. A label must begin with an alphabetic character(A through Z, or a through z), and labels are case-sensitive. Valid OPCODEs are listed in the chart below; they are not case sensitive. Opcodes are reserved words of the language and many not be used a label. The LOC field is either a reference to a label or ''immediate data''. For example, “LOAD A” would put the contents referenced by the label “A” into the ACC; “LOAD =123” would store the value 123 in the ACC. Only those instructions that do not modify the LOC field can use the “immediate data” format. In the following chart, they are indicated by an asterisk in the first column.<br />
<br />
{| class="wikitable" style="text-align: left"|<br />
|-<br />
!OP CODE<br />
!DESCRIPTION<br />
<br />
|-<br />
!*LOAD<br />
|The contents of LOC are placed in the ACC. LOC is unchanged.<br />
<br />
|-<br />
!STORE<br />
|The contents of the ACC are placed in the LOC. ACC is unchanged.<br />
<br />
|-<br />
!*ADD<br />
|The contents of LOC are added to the contents of the ACC. The sum is stored in the ACC. LOC is unchanged. Addition is modulo 1,000,000.<br />
<br />
|-<br />
!*SUB<br />
|The contents of LOC are subtracted from the contents of the ACC. The difference is stored in the ACC. LOC is unchanged. Subtraction is modulo 1,000,000.<br />
<br />
|-<br />
!*MULT<br />
|The contents of LOC are multiplied by the contents of the ACC. The product is stored in the ACC. LOC is unchanged. Multiplication is modulo 1,000,000.<br />
<br />
|-<br />
!*DIV<br />
|The contents of LOC are divided into the contents of the ACC. The signed integer part of the quotient is stored in the ACC. LOC is unchanged.<br />
.<br />
|-<br />
!BG<br />
|<br />
Branch to the instruction labeled with LOC if ACC>0.<br />
<br />
|-<br />
!BE<br />
|<br />
Branch to the instruction labeled with LOC if ACC=0.<br />
<br />
|-<br />
!BL<br />
|<br />
Branch to the instruction labeled with LOC if ACC<0.<br />
<br />
|-<br />
!BU<br />
|Branch to the instruction labeled with LOC.<br />
<br />
|-<br />
!READ<br />
|<br />
Read a signed integer (modulo 1,000,000) into LOC.<br />
<br />
|-<br />
!PRINT<br />
|<br />
Print the contents of LOC.<br />
<br />
|-<br />
!DC<br />
|<br />
The value of the memory word defined by the LABEL field is defined to contain the specified constant. The LABEL field is mandatory for this opcode. The ACC is not modified.<br />
<br />
|-<br />
!END<br />
| <br />
Program terminates. LOC field is ignored and must be empty.<br />
|}<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
After the following program is executed, <br />
what value is in location TEMP?<br />
<br />
{|class="wikitable" style="text-align: left"<br />
|TEMP || DC || 0<br />
|-<br />
|A||DC||8<br />
|-<br />
|B||DC||-2<br />
|-<br />
|C||DC||3<br />
|-<br />
| ||LOAD||B<br />
|-<br />
| ||MULT||C<br />
|-<br />
| ||ADD||A<br />
|-<br />
| ||DIV||B<br />
|-<br />
| ||SUB||A<br />
|-<br />
| ||STORE||TEMP<br />
|-<br />
| ||END||<br />
|}<br />
<br />
'''Solution:''' The ACC takes on values -2, -6, 2, -1, and -9 in that order. The last value, -9, is stored in location TEMP.<br />
<br />
=== Problem 2 ===<br />
<br />
If the following program has an input value<br />
of N, what is the final value of X which is<br />
computed? Express X as an algebraic<br />
expression in terms of N. <br />
<br />
{|class="wikitable" style="text-align: left"<br />
| || READ || X<br />
|-<br />
| || LOAD || X<br />
|-<br />
|TOP || SUB || =1<br />
|-<br />
| || BE || DONE<br />
|-<br />
| || STORE || A<br />
|-<br />
| || MULT || X<br />
|-<br />
| || STORE || X<br />
|-<br />
| || LOAD || A<br />
|-<br />
| || BU || TOP<br />
|-<br />
|DONE || END || <br />
|}<br />
<br />
'''Solution:''' This program loops between labels TOP and<br />
DONE for A times. A has an initial value of X and subsequent terms of N,<br />
then values of A-1, A-2, …, 1. Each time through the loop,<br />
X is multiplied by the the current value of A.<br />
Thus, X = A * (A-1) * (A-2) * … * 1 or X=A! or A factorial.<br />
For example, 5! = 5 * 4 * 3 * 2 * 1 = 120. Since the<br />
initial value of A is the number input (i.e. N),<br />
the algebraic expression is X = N!.<br />
<br />
== Video Resources ==<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/RUm3iHsbO3I</youtube><br />
| [https://youtu.be/RUm3iHsbO3I ''Intro to Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A general introduction into assembly language. In particular, it covers how it fits into the source code to an executable image pipeline.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/i_JYT398O64</youtube><br />
| [https://youtu.be/i_JYT398O64 ''Syntax of ACSL Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A very nice introduction to this ACSL category.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/NEQASUsZ0g4</youtube><br />
| [https://youtu.be/NEQASUsZ0g4 ''Examples'' ('''CalculusNguyenify''')]<br />
<br />
Walks through a couple of ACSL Assembly language programs that have been used in previous contests.<br />
<br />
|}<br />
<br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--></div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Assembly_Language_Programming&diff=636Assembly Language Programming2019-03-13T02:40:11Z<p>Marc: /* Reference Manual */</p>
<hr />
<div>Programs written in high-level languages are traditionally converted by compilers into assembly language, which is turned into machine language programs – sequences of 1’s and 0’s – by an assembler. Even today, with very good quality compilers available, there is the need for programmers to understand assembly language. First, it provides programmers with a better understanding of the compiler and its constraints. Second, on occasion, programmers find themselves needing to program directly in assembly language in order to meet constraints in execution speed or space. <br />
<br />
ACSL chose to define its own assembly language rather than use a “real” one in order to eliminate the many sticky details associated with real languages. The basic concepts of our ACSL topic description are common to all assembly languages. <br />
<br />
== Reference Manual ==<br />
<br />
Execution starts at the first line of the program and continues sequentially, except for branch instructions (BG, BE, BL, BU), until the end instruction (END) is encountered. The result of each operation is stored in a special word of memory, called the “accumulator” (ACC). The initial value of the ACC is 0. Each line of an assembly language program has the following fields:<br />
<br />
LABEL OPCODE LOC<br />
<br />
The LABEL field, if ipresent, is an alphanumeric character string beginning in the first column. A label must begin with an alphabetic character(A through Z, or a through z), and labels are case-sensitive. Valid OPCODEs are listed in the chart below; they are not case sensitive. Opcodes are reserved words of the language and many not be used a label. The LOC field is either a reference to a label or ''immediate data''. For example, “LOAD A” would put the contents referenced by the label “A” into the ACC; “LOAD =123” would store the value 123 in the ACC. Only those instructions that do not modify the LOC field can use the “immediate data” format. In the following chart, they are indicated by an asterisk in the first column.<br />
<br />
{| class="wikitable" style="text-align: left"|<br />
|-<br />
!OP CODE<br />
!DESCRIPTION<br />
<br />
|-<br />
!*LOAD<br />
|The contents of LOC are placed in the ACC. LOC is unchanged.<br />
<br />
|-<br />
!STORE<br />
|The contents of the ACC are placed in the LOC. ACC is unchanged.<br />
<br />
|-<br />
!*ADD<br />
|The contents of LOC are added to the contents of the ACC. The sum is stored in the ACC. LOC is unchanged. Addition is modulo 1,000,000.<br />
<br />
|-<br />
!*SUB<br />
|The contents of LOC are subtracted from the contents of the ACC. The difference is stored in the ACC. LOC is unchanged. Subtraction is modulo 1,000,000.<br />
<br />
|-<br />
!*MULT<br />
|The contents of LOC are multiplied by the contents of the ACC. The product is stored in the ACC. LOC is unchanged. Multiplication is modulo 1,000,000.<br />
<br />
|-<br />
!*DIV<br />
|The contents of LOC are divided into the contents of the ACC. The signed integer part of the quotient is stored in the ACC. LOC is unchanged.<br />
.<br />
|-<br />
!BG<br />
|<br />
Branch to the instruction labeled with LOC if ACC>0.<br />
<br />
|-<br />
!BE<br />
|<br />
Branch to the instruction labeled with LOC if ACC=0.<br />
<br />
|-<br />
!BL<br />
|<br />
Branch to the instruction labeled with LOC if ACC<0.<br />
<br />
|-<br />
!BU<br />
|Branch to the instruction labeled with LOC.<br />
<br />
|-<br />
!READ<br />
|<br />
Read a signed integer (modulo 1,000,000) into LOC.<br />
<br />
|-<br />
!PRINT<br />
|<br />
Print the contents of LOC.<br />
<br />
|-<br />
!DC<br />
|<br />
The value of the memory word defined by the LABEL field is defined to contain the specified constant. The LABEL field is mandatory for this opcode. The ACC is not modified.<br />
<br />
|-<br />
!END<br />
| <br />
Program terminates. LOC field is ignored and must be empty.<br />
|}<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
After the following program is executed, <br />
what value is in location TEMP?<br />
<br />
{|class="wikitable" style="text-align: left"<br />
|TEMP || DC || 0<br />
|-<br />
|A||DC||8<br />
|-<br />
|B||DC||-2<br />
|-<br />
|C||DC||3<br />
|-<br />
| ||LOAD||B<br />
|-<br />
| ||MULT||C<br />
|-<br />
| ||ADD||A<br />
|-<br />
| ||DIV||B<br />
|-<br />
| ||SUB||A<br />
|-<br />
| ||STORE||TEMP<br />
|-<br />
| ||END||<br />
|}<br />
<br />
'''Solution:''' The ACC takes on values -2, -6, 2, -1, and -9 in that order. The last value, -9, is stored in location TEMP.<br />
<br />
=== Problem 2 ===<br />
<br />
If the following program has an input value<br />
of N, what is the final value of X which is<br />
computed? Express X as an algebraic<br />
expression in terms of N. <br />
<br />
{|class="wikitable" style="text-align: left"<br />
| || READ || X<br />
|-<br />
| || LOAD || X<br />
|-<br />
|TOP || SUB || =1<br />
|-<br />
| || BE || DONE<br />
|-<br />
| || STORE || A<br />
|-<br />
| || MULT || X<br />
|-<br />
| || STORE || X<br />
|-<br />
| || LOAD || A<br />
|-<br />
| || BU || TOP<br />
|-<br />
|DONE || END || <br />
|}<br />
<br />
'''Solution:''' This program loops between labels TOP and<br />
DONE for A times. A has an initial value of X and subsequent terms of N,<br />
then values of A-1, A-2, …, 1. Each time through the loop,<br />
X is multiplied by the the current value of A.<br />
Thus, X = A * (A-1) * (A-2) * … * 1 or X=A! or A factorial.<br />
For example, 5! = 5 * 4 * 3 * 2 * 1 = 120. Since the<br />
initial value of A is the number input (i.e. N),<br />
the algebraic expression is X = N!.<br />
<br />
== Video Resources ==<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/RUm3iHsbO3I</youtube><br />
| [https://youtu.be/RUm3iHsbO3I ''Intro to Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A general introduction into assembly language. In particular, it covers how it fits into the source code to an executable image pipeline.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/i_JYT398O64</youtube><br />
| [https://youtu.be/i_JYT398O64 ''Syntax of ACSL Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A very nice introduction to this ACSL category.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/NEQASUsZ0g4</youtube><br />
| [https://youtu.be/NEQASUsZ0g4 ''Examples'' ('''CalculusNguyenify''')]<br />
<br />
Walks through a couple of ACSL Assembly language programs that have been used in previous contests.<br />
<br />
|}<br />
<br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--></div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Assembly_Language_Programming&diff=635Assembly Language Programming2019-03-03T01:13:06Z<p>Marc: /* Reference Manual */</p>
<hr />
<div>Programs written in high-level languages are traditionally converted by compilers into assembly language, which is turned into machine language programs – sequences of 1’s and 0’s – by an assembler. Even today, with very good quality compilers available, there is the need for programmers to understand assembly language. First, it provides programmers with a better understanding of the compiler and its constraints. Second, on occasion, programmers find themselves needing to program directly in assembly language in order to meet constraints in execution speed or space. <br />
<br />
ACSL chose to define its own assembly language rather than use a “real” one in order to eliminate the many sticky details associated with real languages. The basic concepts of our ACSL topic description are common to all assembly languages. <br />
<br />
== Reference Manual ==<br />
<br />
Execution starts at the first line of the program and continues sequentially, except for branch instructions (BG, BE, BL, BU), until the end instruction (END) is encountered. The result of each operation is stored in a special word of memory, called the “accumulator” (ACC). The initial value of the ACC is 0. Each line of an assembly language program has the following fields (lower-case indicates optional component):<br />
<br />
label OPCODE LOC<br />
<br />
The ''label,'' if ipresent, is an alphanumeric character string beginning in the first column. A label must begin with an alphabetic character(A through Z, or a through z), and labels are case-sensitive. Valid OPCODEs are listed in the chart below; they are not case sensitive. Opcodes are reserved words of the language and many not be used a label. The LOC field is either a reference to a label or ''immediate data''. For example, “LOAD A” would put the contents referenced by the label “A” into the ACC; “LOAD =123” would store the value 123 in the ACC. Only those instructions that do not modify the LOC field can use the “immediate data” format. In the following chart, they are indicated by an asterisk in the first column.<br />
<br />
{| class="wikitable" style="text-align: left"|<br />
|-<br />
!OP CODE<br />
!DESCRIPTION<br />
<br />
|-<br />
!*LOAD<br />
|The contents of LOC are placed in the ACC. LOC is unchanged.<br />
<br />
|-<br />
!STORE<br />
|The contents of the ACC are placed in the LOC. ACC is unchanged.<br />
<br />
|-<br />
!*ADD<br />
|The contents of LOC are added to the contents of the ACC. The sum is stored in the ACC. LOC is unchanged. Addition is modulo 1,000,000.<br />
<br />
|-<br />
!*SUB<br />
|The contents of LOC are subtracted from the contents of the ACC. The difference is stored in the ACC. LOC is unchanged. Subtraction is modulo 1,000,000.<br />
<br />
|-<br />
!*MULT<br />
|The contents of LOC are multiplied by the contents of the ACC. The product is stored in the ACC. LOC is unchanged. Multiplication is modulo 1,000,000.<br />
<br />
|-<br />
!*DIV<br />
|The contents of LOC are divided into the contents of the ACC. The signed integer part of the quotient is stored in the ACC. LOC is unchanged.<br />
.<br />
|-<br />
!BG<br />
|<br />
Branch to the instruction labeled with LOC if ACC>0.<br />
<br />
|-<br />
!BE<br />
|<br />
Branch to the instruction labeled with LOC if ACC=0.<br />
<br />
|-<br />
!BL<br />
|<br />
Branch to the instruction labeled with LOC if ACC<0.<br />
<br />
|-<br />
!BU<br />
|Branch to the instruction labeled with LOC.<br />
<br />
|-<br />
!READ<br />
|<br />
Read a signed integer (modulo 1,000,000) into LOC.<br />
<br />
|-<br />
!PRINT<br />
|<br />
Print the contents of LOC.<br />
<br />
|-<br />
!DC<br />
|<br />
The value of the memory word defined by the LABEL field is defined to contain the specified constant. The LABEL field is mandatory for this opcode. The ACC is not modified.<br />
<br />
|-<br />
!END<br />
| <br />
Program terminates. LOC field is ignored.<br />
|}<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
After the following program is executed, <br />
what value is in location TEMP?<br />
<br />
{|class="wikitable" style="text-align: left"<br />
|TEMP || DC || 0<br />
|-<br />
|A||DC||8<br />
|-<br />
|B||DC||-2<br />
|-<br />
|C||DC||3<br />
|-<br />
| ||LOAD||B<br />
|-<br />
| ||MULT||C<br />
|-<br />
| ||ADD||A<br />
|-<br />
| ||DIV||B<br />
|-<br />
| ||SUB||A<br />
|-<br />
| ||STORE||TEMP<br />
|-<br />
| ||END||<br />
|}<br />
<br />
'''Solution:''' The ACC takes on values -2, -6, 2, -1, and -9 in that order. The last value, -9, is stored in location TEMP.<br />
<br />
=== Problem 2 ===<br />
<br />
If the following program has an input value<br />
of N, what is the final value of X which is<br />
computed? Express X as an algebraic<br />
expression in terms of N. <br />
<br />
{|class="wikitable" style="text-align: left"<br />
| || READ || X<br />
|-<br />
| || LOAD || X<br />
|-<br />
|TOP || SUB || =1<br />
|-<br />
| || BE || DONE<br />
|-<br />
| || STORE || A<br />
|-<br />
| || MULT || X<br />
|-<br />
| || STORE || X<br />
|-<br />
| || LOAD || A<br />
|-<br />
| || BU || TOP<br />
|-<br />
|DONE || END || <br />
|}<br />
<br />
'''Solution:''' This program loops between labels TOP and<br />
DONE for A times. A has an initial value of X and subsequent terms of N,<br />
then values of A-1, A-2, …, 1. Each time through the loop,<br />
X is multiplied by the the current value of A.<br />
Thus, X = A * (A-1) * (A-2) * … * 1 or X=A! or A factorial.<br />
For example, 5! = 5 * 4 * 3 * 2 * 1 = 120. Since the<br />
initial value of A is the number input (i.e. N),<br />
the algebraic expression is X = N!.<br />
<br />
== Video Resources ==<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/RUm3iHsbO3I</youtube><br />
| [https://youtu.be/RUm3iHsbO3I ''Intro to Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A general introduction into assembly language. In particular, it covers how it fits into the source code to an executable image pipeline.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/i_JYT398O64</youtube><br />
| [https://youtu.be/i_JYT398O64 ''Syntax of ACSL Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A very nice introduction to this ACSL category.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/NEQASUsZ0g4</youtube><br />
| [https://youtu.be/NEQASUsZ0g4 ''Examples'' ('''CalculusNguyenify''')]<br />
<br />
Walks through a couple of ACSL Assembly language programs that have been used in previous contests.<br />
<br />
|}<br />
<br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--></div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Assembly_Language_Programming&diff=634Assembly Language Programming2019-03-03T00:57:36Z<p>Marc: /* Reference Manual */</p>
<hr />
<div>Programs written in high-level languages are traditionally converted by compilers into assembly language, which is turned into machine language programs – sequences of 1’s and 0’s – by an assembler. Even today, with very good quality compilers available, there is the need for programmers to understand assembly language. First, it provides programmers with a better understanding of the compiler and its constraints. Second, on occasion, programmers find themselves needing to program directly in assembly language in order to meet constraints in execution speed or space. <br />
<br />
ACSL chose to define its own assembly language rather than use a “real” one in order to eliminate the many sticky details associated with real languages. The basic concepts of our ACSL topic description are common to all assembly languages. <br />
<br />
== Reference Manual ==<br />
<br />
Execution starts at the first line of the program and continues sequentially, except for branch instructions (BG, BE, BL, BU), until the end instruction (END) is encountered. The result of each operation is stored in a special word of memory, called the “accumulator” (ACC). Each line of an assembly language program has the following fields (lower-case indicates optional component):<br />
<br />
label OPCODE LOC<br />
<br />
The ''label,'' if ipresent, is an alphanumeric character string beginning in the first column. A label must begin with an alphabetic character(A through Z, or a through z), and labels are case-sensitive. Valid OPCODEs are listed in the chart below; they are not case sensitive. Opcodes are reserved words of the language and many not be used a label. The LOC field is either a reference to a label or ''immediate data''. For example, “LOAD A” would put the contents referenced by the label “A” into the ACC; “LOAD =123” would store the value 123 in the ACC. Only those instructions that do not modify the LOC field can use the “immediate data” format. In the following chart, they are indicated by an asterisk in the first column.<br />
<br />
{| class="wikitable" style="text-align: left"|<br />
|-<br />
!OP CODE<br />
!DESCRIPTION<br />
<br />
|-<br />
!*LOAD<br />
|The contents of LOC are placed in the ACC. LOC is unchanged.<br />
<br />
|-<br />
!STORE<br />
|The contents of the ACC are placed in the LOC. ACC is unchanged.<br />
<br />
|-<br />
!*ADD<br />
|The contents of LOC are added to the contents of the ACC. The sum is stored in the ACC. LOC is unchanged. Addition is modulo 1,000,000.<br />
<br />
|-<br />
!*SUB<br />
|The contents of LOC are subtracted from the contents of the ACC. The difference is stored in the ACC. LOC is unchanged. Subtraction is modulo 1,000,000.<br />
<br />
|-<br />
!*MULT<br />
|The contents of LOC are multiplied by the contents of the ACC. The product is stored in the ACC. LOC is unchanged. Multiplication is modulo 1,000,000.<br />
<br />
|-<br />
!*DIV<br />
|The contents of LOC are divided into the contents of the ACC. The signed integer part of the quotient is stored in the ACC. LOC is unchanged.<br />
.<br />
|-<br />
!BG<br />
|<br />
Branch to the instruction labeled with LOC if ACC>0.<br />
<br />
|-<br />
!BE<br />
|<br />
Branch to the instruction labeled with LOC if ACC=0.<br />
<br />
|-<br />
!BL<br />
|<br />
Branch to the instruction labeled with LOC if ACC<0.<br />
<br />
|-<br />
!BU<br />
|Branch to the instruction labeled with LOC.<br />
<br />
|-<br />
!READ<br />
|<br />
Read a signed integer (modulo 1,000,000) into LOC.<br />
<br />
|-<br />
!PRINT<br />
|<br />
Print the contents of LOC.<br />
<br />
|-<br />
!DC<br />
|<br />
The value of the memory word defined by the LABEL field is defined to contain the specified constant. The LABEL field is mandatory for this opcode. The ACC is not modified.<br />
<br />
|-<br />
!END<br />
| <br />
Program terminates. LOC field is ignored.<br />
|}<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
After the following program is executed, <br />
what value is in location TEMP?<br />
<br />
{|class="wikitable" style="text-align: left"<br />
|TEMP || DC || 0<br />
|-<br />
|A||DC||8<br />
|-<br />
|B||DC||-2<br />
|-<br />
|C||DC||3<br />
|-<br />
| ||LOAD||B<br />
|-<br />
| ||MULT||C<br />
|-<br />
| ||ADD||A<br />
|-<br />
| ||DIV||B<br />
|-<br />
| ||SUB||A<br />
|-<br />
| ||STORE||TEMP<br />
|-<br />
| ||END||<br />
|}<br />
<br />
'''Solution:''' The ACC takes on values -2, -6, 2, -1, and -9 in that order. The last value, -9, is stored in location TEMP.<br />
<br />
=== Problem 2 ===<br />
<br />
If the following program has an input value<br />
of N, what is the final value of X which is<br />
computed? Express X as an algebraic<br />
expression in terms of N. <br />
<br />
{|class="wikitable" style="text-align: left"<br />
| || READ || X<br />
|-<br />
| || LOAD || X<br />
|-<br />
|TOP || SUB || =1<br />
|-<br />
| || BE || DONE<br />
|-<br />
| || STORE || A<br />
|-<br />
| || MULT || X<br />
|-<br />
| || STORE || X<br />
|-<br />
| || LOAD || A<br />
|-<br />
| || BU || TOP<br />
|-<br />
|DONE || END || <br />
|}<br />
<br />
'''Solution:''' This program loops between labels TOP and<br />
DONE for A times. A has an initial value of X and subsequent terms of N,<br />
then values of A-1, A-2, …, 1. Each time through the loop,<br />
X is multiplied by the the current value of A.<br />
Thus, X = A * (A-1) * (A-2) * … * 1 or X=A! or A factorial.<br />
For example, 5! = 5 * 4 * 3 * 2 * 1 = 120. Since the<br />
initial value of A is the number input (i.e. N),<br />
the algebraic expression is X = N!.<br />
<br />
== Video Resources ==<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/RUm3iHsbO3I</youtube><br />
| [https://youtu.be/RUm3iHsbO3I ''Intro to Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A general introduction into assembly language. In particular, it covers how it fits into the source code to an executable image pipeline.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/i_JYT398O64</youtube><br />
| [https://youtu.be/i_JYT398O64 ''Syntax of ACSL Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A very nice introduction to this ACSL category.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/NEQASUsZ0g4</youtube><br />
| [https://youtu.be/NEQASUsZ0g4 ''Examples'' ('''CalculusNguyenify''')]<br />
<br />
Walks through a couple of ACSL Assembly language programs that have been used in previous contests.<br />
<br />
|}<br />
<br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--></div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Assembly_Language_Programming&diff=633Assembly Language Programming2019-03-03T00:37:36Z<p>Marc: /* Reference Manual */</p>
<hr />
<div>Programs written in high-level languages are traditionally converted by compilers into assembly language, which is turned into machine language programs – sequences of 1’s and 0’s – by an assembler. Even today, with very good quality compilers available, there is the need for programmers to understand assembly language. First, it provides programmers with a better understanding of the compiler and its constraints. Second, on occasion, programmers find themselves needing to program directly in assembly language in order to meet constraints in execution speed or space. <br />
<br />
ACSL chose to define its own assembly language rather than use a “real” one in order to eliminate the many sticky details associated with real languages. The basic concepts of our ACSL topic description are common to all assembly languages. <br />
<br />
== Reference Manual ==<br />
<br />
Execution starts at the first line of the program and continues sequentially, except for branch instructions (BG, BE, BL, BU), until the end instruction (END) is encountered. The result of each operation is stored in a special word of memory, called the “accumulator” (ACC). Each line of an assembly language program has the following fields (lower-case indicates optional component):<br />
<br />
label OPCODE LOC<br />
<br />
The ''label,'' if ipresent, is an alphanumeric character string beginning in the first column. A label must begin with an alphabetic character(A through Z), and labels are case-sensitive. Valid OPCODEs are listed in the chart below; they are not case sensitive. Opcodes are reserved words of the language and many not be used a label. The LOC field is either a reference to a label or ''immediate data''. For example, “LOAD A” would put the contents referenced by the label “A” into the ACC; “LOAD =123” would store the value 123 in the ACC. Only those instructions that do not modify the LOC field can use the “immediate data” format. In the following chart, they are indicated by an asterisk in the first column.<br />
<br />
{| class="wikitable" style="text-align: left"|<br />
|-<br />
!OP CODE<br />
!DESCRIPTION<br />
<br />
|-<br />
!*LOAD<br />
|The contents of LOC are placed in the ACC. LOC is unchanged.<br />
<br />
|-<br />
!STORE<br />
|The contents of the ACC are placed in the LOC. ACC is unchanged.<br />
<br />
|-<br />
!*ADD<br />
|The contents of LOC are added to the contents of the ACC. The sum is stored in the ACC. LOC is unchanged. Addition is modulo 1,000,000.<br />
<br />
|-<br />
!*SUB<br />
|The contents of LOC are subtracted from the contents of the ACC. The difference is stored in the ACC. LOC is unchanged. Subtraction is modulo 1,000,000.<br />
<br />
|-<br />
!*MULT<br />
|The contents of LOC are multiplied by the contents of the ACC. The product is stored in the ACC. LOC is unchanged. Multiplication is modulo 1,000,000.<br />
<br />
|-<br />
!*DIV<br />
|The contents of LOC are divided into the contents of the ACC. The signed integer part of the quotient is stored in the ACC. LOC is unchanged.<br />
.<br />
|-<br />
!BG<br />
|<br />
Branch to the instruction labeled with LOC if ACC>0.<br />
<br />
|-<br />
!BE<br />
|<br />
Branch to the instruction labeled with LOC if ACC=0.<br />
<br />
|-<br />
!BL<br />
|<br />
Branch to the instruction labeled with LOC if ACC<0.<br />
<br />
|-<br />
!BU<br />
|Branch to the instruction labeled with LOC.<br />
<br />
|-<br />
!READ<br />
|<br />
Read a signed integer (modulo 1,000,000) into LOC.<br />
<br />
|-<br />
!PRINT<br />
|<br />
Print the contents of LOC.<br />
<br />
|-<br />
!DC<br />
|<br />
The value of the memory word defined by the LABEL field is defined to contain the specified constant. The LABEL field is mandatory for this opcode. The ACC is not modified.<br />
<br />
|-<br />
!END<br />
| <br />
Program terminates. LOC field is ignored.<br />
|}<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
After the following program is executed, <br />
what value is in location TEMP?<br />
<br />
{|class="wikitable" style="text-align: left"<br />
|TEMP || DC || 0<br />
|-<br />
|A||DC||8<br />
|-<br />
|B||DC||-2<br />
|-<br />
|C||DC||3<br />
|-<br />
| ||LOAD||B<br />
|-<br />
| ||MULT||C<br />
|-<br />
| ||ADD||A<br />
|-<br />
| ||DIV||B<br />
|-<br />
| ||SUB||A<br />
|-<br />
| ||STORE||TEMP<br />
|-<br />
| ||END||<br />
|}<br />
<br />
'''Solution:''' The ACC takes on values -2, -6, 2, -1, and -9 in that order. The last value, -9, is stored in location TEMP.<br />
<br />
=== Problem 2 ===<br />
<br />
If the following program has an input value<br />
of N, what is the final value of X which is<br />
computed? Express X as an algebraic<br />
expression in terms of N. <br />
<br />
{|class="wikitable" style="text-align: left"<br />
| || READ || X<br />
|-<br />
| || LOAD || X<br />
|-<br />
|TOP || SUB || =1<br />
|-<br />
| || BE || DONE<br />
|-<br />
| || STORE || A<br />
|-<br />
| || MULT || X<br />
|-<br />
| || STORE || X<br />
|-<br />
| || LOAD || A<br />
|-<br />
| || BU || TOP<br />
|-<br />
|DONE || END || <br />
|}<br />
<br />
'''Solution:''' This program loops between labels TOP and<br />
DONE for A times. A has an initial value of X and subsequent terms of N,<br />
then values of A-1, A-2, …, 1. Each time through the loop,<br />
X is multiplied by the the current value of A.<br />
Thus, X = A * (A-1) * (A-2) * … * 1 or X=A! or A factorial.<br />
For example, 5! = 5 * 4 * 3 * 2 * 1 = 120. Since the<br />
initial value of A is the number input (i.e. N),<br />
the algebraic expression is X = N!.<br />
<br />
== Video Resources ==<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/RUm3iHsbO3I</youtube><br />
| [https://youtu.be/RUm3iHsbO3I ''Intro to Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A general introduction into assembly language. In particular, it covers how it fits into the source code to an executable image pipeline.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/i_JYT398O64</youtube><br />
| [https://youtu.be/i_JYT398O64 ''Syntax of ACSL Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A very nice introduction to this ACSL category.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/NEQASUsZ0g4</youtube><br />
| [https://youtu.be/NEQASUsZ0g4 ''Examples'' ('''CalculusNguyenify''')]<br />
<br />
Walks through a couple of ACSL Assembly language programs that have been used in previous contests.<br />
<br />
|}<br />
<br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--></div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Assembly_Language_Programming&diff=632Assembly Language Programming2019-03-03T00:23:49Z<p>Marc: /* Reference Manual */</p>
<hr />
<div>Programs written in high-level languages are traditionally converted by compilers into assembly language, which is turned into machine language programs – sequences of 1’s and 0’s – by an assembler. Even today, with very good quality compilers available, there is the need for programmers to understand assembly language. First, it provides programmers with a better understanding of the compiler and its constraints. Second, on occasion, programmers find themselves needing to program directly in assembly language in order to meet constraints in execution speed or space. <br />
<br />
ACSL chose to define its own assembly language rather than use a “real” one in order to eliminate the many sticky details associated with real languages. The basic concepts of our ACSL topic description are common to all assembly languages. <br />
<br />
== Reference Manual ==<br />
<br />
Execution starts at the first line of the program and continues sequentially, except for branch instructions (BG, BE, BL, BU), until the end instruction (END) is encountered. The result of each operation is stored in a special word of memory, called the “accumulator” (ACC). Each line of an assembly language program has the following fields (lower-case indicates optional components):<br />
<br />
label OPCODE LOC<br />
<br />
The ''label'' is a character string beginning in the first column. Valid OPCODE’s are listed in the chart below. The LOC field is either a reference to a label or ''immediate data''. For example, “LOAD A” would put the contents referenced by the label “A” into the ACC; “LOAD =123” would store the value 123 in the ACC. Only those instructions that do not modify the LOC field can use the “immediate data” format. In the following chart, they are indicated by an asterisk in the first column.<br />
<br />
{| class="wikitable" style="text-align: left"|<br />
|-<br />
!OP CODE<br />
!DESCRIPTION<br />
<br />
|-<br />
!*LOAD<br />
|The contents of LOC are placed in the ACC. LOC is unchanged.<br />
<br />
|-<br />
!STORE<br />
|The contents of the ACC are placed in the LOC. ACC is unchanged.<br />
<br />
|-<br />
!*ADD<br />
|The contents of LOC are added to the contents of the ACC. The sum is stored in the ACC. LOC is unchanged. Addition is modulo 1,000,000.<br />
<br />
|-<br />
!*SUB<br />
|The contents of LOC are subtracted from the contents of the ACC. The difference is stored in the ACC. LOC is unchanged. Subtraction is modulo 1,000,000.<br />
<br />
|-<br />
!*MULT<br />
|The contents of LOC are multiplied by the contents of the ACC. The product is stored in the ACC. LOC is unchanged. Multiplication is modulo 1,000,000.<br />
<br />
|-<br />
!*DIV<br />
|The contents of LOC are divided into the contents of the ACC. The signed integer part of the quotient is stored in the ACC. LOC is unchanged.<br />
.<br />
|-<br />
!BG<br />
|<br />
Branch to the instruction labeled with LOC if ACC>0.<br />
<br />
|-<br />
!BE<br />
|<br />
Branch to the instruction labeled with LOC if ACC=0.<br />
<br />
|-<br />
!BL<br />
|<br />
Branch to the instruction labeled with LOC if ACC<0.<br />
<br />
|-<br />
!BU<br />
|Branch to the instruction labeled with LOC.<br />
<br />
|-<br />
!READ<br />
|<br />
Read a signed integer (modulo 1,000,000) into LOC.<br />
<br />
|-<br />
!PRINT<br />
|<br />
Print the contents of LOC.<br />
<br />
|-<br />
!DC<br />
|<br />
The value of the memory word defined by the LABEL field is defined to contain the specified constant. The LABEL field is mandatory for this opcode. The ACC is not modified.<br />
<br />
|-<br />
!END<br />
| <br />
Program terminates. LOC field is ignored.<br />
|}<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
After the following program is executed, <br />
what value is in location TEMP?<br />
<br />
{|class="wikitable" style="text-align: left"<br />
|TEMP || DC || 0<br />
|-<br />
|A||DC||8<br />
|-<br />
|B||DC||-2<br />
|-<br />
|C||DC||3<br />
|-<br />
| ||LOAD||B<br />
|-<br />
| ||MULT||C<br />
|-<br />
| ||ADD||A<br />
|-<br />
| ||DIV||B<br />
|-<br />
| ||SUB||A<br />
|-<br />
| ||STORE||TEMP<br />
|-<br />
| ||END||<br />
|}<br />
<br />
'''Solution:''' The ACC takes on values -2, -6, 2, -1, and -9 in that order. The last value, -9, is stored in location TEMP.<br />
<br />
=== Problem 2 ===<br />
<br />
If the following program has an input value<br />
of N, what is the final value of X which is<br />
computed? Express X as an algebraic<br />
expression in terms of N. <br />
<br />
{|class="wikitable" style="text-align: left"<br />
| || READ || X<br />
|-<br />
| || LOAD || X<br />
|-<br />
|TOP || SUB || =1<br />
|-<br />
| || BE || DONE<br />
|-<br />
| || STORE || A<br />
|-<br />
| || MULT || X<br />
|-<br />
| || STORE || X<br />
|-<br />
| || LOAD || A<br />
|-<br />
| || BU || TOP<br />
|-<br />
|DONE || END || <br />
|}<br />
<br />
'''Solution:''' This program loops between labels TOP and<br />
DONE for A times. A has an initial value of X and subsequent terms of N,<br />
then values of A-1, A-2, …, 1. Each time through the loop,<br />
X is multiplied by the the current value of A.<br />
Thus, X = A * (A-1) * (A-2) * … * 1 or X=A! or A factorial.<br />
For example, 5! = 5 * 4 * 3 * 2 * 1 = 120. Since the<br />
initial value of A is the number input (i.e. N),<br />
the algebraic expression is X = N!.<br />
<br />
== Video Resources ==<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/RUm3iHsbO3I</youtube><br />
| [https://youtu.be/RUm3iHsbO3I ''Intro to Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A general introduction into assembly language. In particular, it covers how it fits into the source code to an executable image pipeline.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/i_JYT398O64</youtube><br />
| [https://youtu.be/i_JYT398O64 ''Syntax of ACSL Assembly Language'' ('''CalculusNguyenify''')]<br />
<br />
A very nice introduction to this ACSL category.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/NEQASUsZ0g4</youtube><br />
| [https://youtu.be/NEQASUsZ0g4 ''Examples'' ('''CalculusNguyenify''')]<br />
<br />
Walks through a couple of ACSL Assembly language programs that have been used in previous contests.<br />
<br />
|}<br />
<br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--></div>Marchttp://www.categories.acsl.org/wiki/index.php?title=Data_Structures&diff=631Data Structures2019-02-11T00:55:07Z<p>Marc: /* Problem 1 */</p>
<hr />
<div>At the heart of virtually every computer program are its algorithms and its data structures. It is hard to separate these two items, for data structures are meaningless without algorithms to create and manipulate them, and algorithms are usually trivial unless there are data structures on which to operate. The bigger the data sets, the more important data structures are in various algorithms.<br />
<br />
This category concentrates on four of the most basic structures: '''stacks''', '''queues''', '''binary search trees''', and '''priority queues'''. Questions will cover these data structures and implicit algorithms, not specific to implementation language details.<br />
<br />
A ''stack'' is usually used to save information that will need to be processed later. Items are processed in a “last-in, first-out” (LIFO) order. A ''queue'' is usually used to process items in the order in which requests are generated; a new item is not processed until all items currently on the queue are processed. This is also known as “first-in, first-out” (FIFO) order. A ''binary search tree'' is used when one is storing items and needs to be able to efficiently process the operations of insertion, deletion, and query (i.e. find out if a particular item is found in the list of items and if not, which item is close to the item in question). A ''priority queue'' is used like a binary search tree, except one cannot delete an arbitrary item, nor can one make an arbitrary query. One can only find out or delete the smallest element of the list.<br />
<br />
There are many online resources covering these basic data structures; indeed there are many books and entire courses devoted to fundamental data structures. Implementation varies by computer language, but for our purposes, they can all be represented as a list of items that might contain duplicates. The rest of this page is an overview of these structures.<br />
<br />
== Stacks and Queues ==<br />
<br />
A ''stack'' supports two operations: PUSH and POP. A command of the form PUSH("A") puts the key "A" at the top of the stack; the command “X = POP()” removes the top item from the stack and stores its value into variable X. If the stack was empty (because nothing had ever been pushed on it, or if all elements have been popped off of it), then X is given the special value of NIL. An analogy to this is a stack of books on a desk: a new book is placed on the top of the stack (pushed) and a book is removed from the top also (popped). Some textbooks call this data structure a “push-down stack” or a “LIFO stack”.<br />
<br />
''Queues'' operate just like stacks, except items are removed from the bottom instead of the top. A command of the form PUSH("A") puts the key "A" at the top of the queue; the command “X = POP()” removes the item from the bottom of the queue and stores its value into variable X. If the queue was empty (because nothing had ever been pushed on it, or if all elements have been popped off of it), then X is given the special value of NIL. A good physical analogy of this is the way a train conductor or newspaper boy uses a coin machine to give change: new coins are added to the tops of the piles, and change is given from the bottom of each. Sometimes the top and bottom of a queue are referred to as the rear and the front respectively. Therefore, items are pushed/enqueued at the rear of the queue and popped/dequeued at the front of the queue. There is a similarity to the Britsh "queueing up". Some textbooks refer to this data structure as a “FIFO stack”.<br />
<br />
Consider the following sequence of 14 operations:<br />
<br />
<pre><br />
PUSH("A")<br />
PUSH("M")<br />
PUSH("E")<br />
X = POP()<br />
PUSH("R")<br />
X = POP()<br />
PUSH("I")<br />
X = POP()<br />
X = POP()<br />
X = POP()<br />
X = POP()<br />
PUSH("C")<br />
PUSH("A")<br />
PUSH("N")<br />
</pre><br />
<br />
If these operations are applied to a stack, then the values of the pops are: E, R, I, M, A and NIL. After all of the operations, there are three items still on the stack: the N is at the top (it will be the next to be popped, if nothing else is pushed before the pop command), and C is at the bottom. If, instead of using a stack we used a queue, then the values popped would be: A, M, E, R, I and NIL. There would be three items still on the queue: N at the top and C on the bottom. Since items are removed from the bottom of a queue, C would be the next item to be popped regardless of any additional pushes.<br />
<br />
=== Pre-2018 Syntax ===<br />
<br />
ACSL Contests pre-2018 used a slightly different, and more liberal, syntax. Consider the following sequence on a stack:<br />
<br />
<pre><br />
PUSH(A)<br />
PUSH(B)<br />
PUSH(C)<br />
POP(X)<br />
POP(Z)<br />
</pre><br />
<br />
After the 5 operations, the stack would be left with A as the only element on the stack, the value of C in variable X, and the value of B in variable Z. <br />
<br />
Were the above operations applied to a queue, the queue would be left with C; variable X would contain A; and variable Z would contain B.<br />
<br />
== Trees ==<br />
<br />
''Trees'', in general, use the following terminology: the ''root'' is the top node in the tree; ''children'' are the nodes that are immediately below a ''parent'' node; ''leaves'' are the bottom-most nodes on every branch of the tree; and ''siblings'' are nodes that have the same immediate parent. <br />
<br />
A ''binary search tree'' is composed of nodes having three parts: information (or a key), a pointer to a left child, and a pointer to a right child. It has the property that the key at every node is always greater than or equal to the key of its left child, and less than the key of its right child.<br />
<br />
The following tree is built from the keys A, M, E, R, I, C, A, N in that order:<br />
<br />
<center><br />
[[File:bst-american.svg|200px]]<br />
</center><br />
<br />
The ''root'' of the resulting tree is the node containing the key A. Our ACSL convention places duplicate keys into the tree as if they were less than their equal key. (In some textbooks and software libraries, duplicate keys may be considered larger than their equal key.) The tree has a ''depth'' (sometimes called height) of 3 because the deepest node is 3 nodes below the root. The root node has a depth of 0. Nodes with no children are called leaf nodes; there are four of them in the tree: A, C, I and N. Our ACSL convention is that an ''external node'' is the name given to a place where a new node could be attached to the tree. (In some textbooks, an external node is synonymous with a leaf node.) In the final tree above, there are 9 external nodes; these are not drawn. The tree has an ''internal path length'' of 15 which is the sum of the depths of all nodes. It has an ''external path length'' of 31 which is the sum of the depths of all external nodes. To insert the N (the last key inserted), 3 ''comparisons'' were needed against the root A (>), the M (>), and the R (≤).<br />
<br />
To perform an ''inorder'' traversal of the tree, recursively traverse the tree by first visiting the left child, then the root, then the right child. In the tree above, the nodes are visited in the following order: A, A, C, E, I, M, N, and R. A ''preorder'' travel (root, left, right) visits in the following order: A, A, M, E, C, I, R, and N. A ''postorder'' traversal (left, right, root) is: A, C, I, E, N, R, M, A. Inorder traversals are typically used to list the contents of the tree in sorted order. <br />
<br />
A binary search tree can support the operations insert, delete, and search. Moreover, it handles the operations efficiently for ''balanced'' trees. In a tree with 1 million items, one can search for a particular value in about log<sub>2</sub> 1,000,000 ≈ 20 steps. Items can be inserted or deleted in about as many steps, too. However, consider the binary search tree resulting from inserting the keys A, E, I, O, U, Y which places all of the other letters on the right side of the root "A". This is very unbalanced; therefore, sophisticated algorithms can be used to maintain balanced trees. Binary search trees are “dynamic” data structures that can support many operations in any order and introduces or removes nodes as needed. <br />
<br />
To search for a node in a binary tree, the following algorithm (in pseudo-code) is used:<br />
<br />
<syntaxhighlight lang="text"><br />
p = root<br />
found = FALSE<br />
while (p ≠ NIL) and (not found)<br />
if (x < p’s key)<br />
p = p’s left child<br />
else if (x > p’s key)<br />
p = p’s right child<br />
else<br />
found = TRUE<br />
end if<br />
end while<br />
</syntaxhighlight><br />
<br />
Deleting from a binary search tree is a bit more complicated. The algorithm we’ll use is as follows:<br />
<br />
<syntaxhighlight lang="text"><br />
p = node to delete<br />
f = father of p<br />
if (p has no children)<br />
delete p<br />
else if (p has one child)<br />
make p’s child become f’s child<br />
delete p<br />
else if (p has two children)<br />
l = p’s left child (it might also have children)<br />
r = p’s right child (it might also have children)<br />
make l become f’s child instead of p<br />
stick r onto the l tree<br />
delete p<br />
end if<br />
</syntaxhighlight><br />
<br />
These diagrams illustrate the algorithm using the tree above. At the left, we delete I (0 children); in the middle, we delete the R (1 child); and at the right, we delete the M (2 children).<br />
<br />
<center><br />
{| class ="wikitable"<br />
|[[File:bst-american-del-i.svg|200px]]<br />
|[[File:bst-american-del-r.svg|200px]]<br />
|[[File:bst-american-del-m.svg|200px]] <br />
|}<br />
</center><br />
<br />
There are also general trees that use the same terminology, but they have 0 or more ''subnodes'' which can be accessed with an array or linked list of pointers. ''Pre-order'' and ''post-order'' traversals are possible with these trees, but the other algorithms do not work in the same way. Applications of general trees include game theory, organizational charts, family trees, etc.<br />
<br />
''Balanced'' trees minimize searching time when every leaf node has a depth of within 1 of every other leaf node. ''Complete'' trees are filled in at every level and are always balanced. ''Strictly binary'' trees ensure that every node has either 0 or 2 subnodes. You may want to consider how there are exactly 5 strictly binary trees with 7 nodes.<br />
<br />
== Priority Queues ==<br />
<br />
A ''priority queue'' is quite similar to a binary search tree, but one can only delete the smallest item and retrieve the smallest item only. These insert and delete operations can be done in a guaranteed time proportional to the log (base 2) of the number of items; the retrieve-the-smallest can be done in constant time. <br />
<br />
The standard way to implement a priority queue is using a ''heap'' data structure. A heap uses a binary tree (that is, a tree with two children) and maintains the following two properties: every node is less than or equal to both of its two children (nothing is said about the relative magnitude of the two children), and the resulting tree contains no “holes”. That is, all levels of the tree are completely filled, except the bottom level, which is filled in from the left to the right.<br />
<br />
The algorithm for insertion is not too difficult: put the new node at the bottom of the tree and then go up the tree, making exchanges with its parent, until the tree is valid. <br />
<!--here would be the place to show building the heap with AMERICAN--><br />
The heap at the left<br />
was building from the letters A, M, E, R, I, C, A, N (in that order); the heap at the right is after a C has been added.<br />
<br />
<center><br />
{| class="wikitable"<br />
|[[File:heap-american.svg|220px]]||<br />
[[File:heap-american-insert-c.svg|220px]]<br />
|}<br />
</center><br />
<br />
The smallest value is always the root. To delete it (and one can only delete the smallest value), one replaces it with the bottom-most and right-most element, and then walks down the tree making exchanges with the smaller child in order to ensure that the tree is valid. The following pseudo-code formalizes this notion:<br />
<br />
<syntaxhighlight lang="text"><br />
b = bottom-most and right-most element<br />
p = root of tree<br />
p’s key = b’s key<br />
delete b<br />
while (p is larger than either child)<br />
exchange p with smaller child<br />
p = smaller child<br />
end while<br />
</syntaxhighlight><br />
<br />
When the smallest item is at the root of the heap, the heap is called a ''min-heap''. Of course, A ''max-heap'' is also possible and is common in practice. An efficient implementation of a heap uses an array that can be understood conceptually by using a tree data structure.<br />
<br />
== Sample Problems ==<br />
<br />
=== Problem 1 ===<br />
<br />
Consider an initially empty stack. After the following operations are performed, what is the value of Z?<br />
<br />
<syntaxhighlight lang="text"><br />
PUSH(3)<br />
PUSH(6)<br />
PUSH(8)<br />
Y = POP()<br />
X = POP()<br />
PUSH(X-Y)<br />
Z = POP() <br />
</syntaxhighlight><br />
<br />
'''Solution:''' The first POP stores 8 in Y. The second POP stores 6 in X. Then, 6-8=-2 is pushed onto the stack. Finally, the last POP removes the -2 and stores it in Z.<br />
<br />
=== Problem 2 ===<br />
<br />
Create a min-heap with the letters in the word PROGRAMMING. What are the letters in the bottom-most row, from left to right?<br />
<br />
'''Solution:''' The bottom row contains the letters RORN, from left-to-right. Here is the entire heap:<br />
<br />
<center><br />
[[File:heap-programming.svg|200px]]<br />
</center><br />
<br />
=== Problem 3 ===<br />
<br />
Create a binary search tree from the letters in the word PROGRAM. What is the internal path length? <br />
<br />
'''Solution:''' When drawing the tree, P has a depth of 0, O and R have a depth of 1, G and R have a depth of 2, and A and M have a depth of 3. Therefore, the internal path length is 2*1 + 2*2 + 2*3 = 12. Here is the tree:<br />
<br />
<center><br />
[[File:bst-program.svg|200px]]<br />
</center><br />
<br />
= Video Resources =<br />
<br />
== ACSL Videos ==<br />
<br />
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. <br />
<br />
<!--<br />
{|<br />
|-<br />
| <youtube width="300" height="180">URL</youtube><br />
| [URL ''TITLE'' ('''AUTHOR''')]<br />
<br />
DESCRIPTION<br />
|}<br />
--><br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/gXj7K_petqo</youtube><br />
| [https://youtu.be/gXj7K_petqo ''Data Structures (Stacks and Queues)'' ('''Tangerine Code''')]<br />
<br />
A general tutorial on stacks and queues.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/_BnbbOhyroQ</youtube><br />
| [https://youtu.be/_BnbbOhyroQ ''Construct a Binary Search Tree'' ('''Tangerine Code''')]<br />
<br />
Shows how to build a binary search tree from the letters '''S U S H I'''.<br />
<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/l9aMO7lgHj0</youtube><br />
| [https://youtu.be/l9aMO7lgHj0 ''Binary Search Tree ACSL Problem (Internal Path Length)'' ('''Tangerine Code''')]<br />
<br />
A general tutorial on internal path length of a binary search tree<br />
|}<br />
<br />
== Other Videos ==<br />
<br />
There is no shortage of instructional video material covering basic data structures. Here is a series that combines teaching concepts with showing Java code that implements the concepts. Most of the ACSL questions will not involve coding the basic operations on the data structures; rather, the problems will involve high-level understanding of them. However, seeing the code is an excellent way to thoroughly understand these data structures, and what it takes to implement them.<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/wjI1WNcIntg</youtube><br />
| [https://youtu.be/wjI1WNcIntg ''Data Structures: Stacks and Queues'' ('''HackerRank''')]<br />
<br />
The first half of the video is a nice description of stacks and queues; the second half walks through very clean Java code that implements the fundamental methods on these data structures.<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/njTh_OwMljA</youtube><br />
| [https://youtu.be/njTh_OwMljA ''Data Structures: Linked Lists'' ('''HackerRank''')]<br />
<br />
Although ACSL does not cover linked lists per se, they are a great preliminary study for binary search trees. <br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/oSWTXtMglKE</youtube><br />
| [https://youtu.be/oSWTXtMglKE ''Data Structures: Trees'' ('''HackerRank''')]<br />
<br />
Learn about binary search trees. This video is a part of HackerRank's ''Cracking The Coding Interview Tutorial'' with Gayle Laakmann McDowell. The first half of the video is a clear and eloquent description of binary search trees; the second half walks through very clean Java code that implements the fundamental methods. <br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/t0Cq6tVNRBA</youtube><br />
| [https://youtu.be/t0Cq6tVNRBA ''Data Structures: Heaps'' ('''HackerRank''')]<br />
<br />
Learn about heaps. This video is a part of HackerRank's ''Cracking The Coding Interview Tutorial'' with Gayle Laakmann McDowell. The first half of the video is a clear and eloquent description of heaps; the second half walks through very clean Java code that implements the fundamental methods. <br />
|}<br />
<br />
Here are a few more videos covering the basics of binary search trees.<br />
<br />
{|<br />
|-<br />
| <youtube width="300" height="180">https://youtu.be/FvdPo8PBQtc</youtube><br />
| [https://youtu.be/FvdPo8PBQtc ''How to Construct a Binary Search Tree'' ('''edutechional''')]<br />
<br />
''In this algorithm tutorial, I walk through how to construct a binary search tree given an unordered array, and then how to find elements inside of the tree.''<br />
|}</div>Marc