User Tools

Site Tools


haas:fall2019:c4eng:projects:bcf0

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
haas:fall2019:c4eng:projects:bcf0 [2019/09/30 12:46] – [Sample incrementation run] wedgehaas:fall2019:c4eng:projects:bcf0 [2019/10/05 20:16] (current) – [Method 2: bitwise AND the place values] wedge
Line 13: Line 13:
  
 =====Program===== =====Program=====
-It is your task to write a program that, upon accepting various pieces of input from the user, acts as a binary counter, displaying the resultant values in binary as from a starting to and ending progression.+It is your task to write a program that, upon accepting various pieces of input from the user, acts as a binary counter, displaying the resultant values in binary as from a starting to an ending progression.
  
 =====Specifications===== =====Specifications=====
Line 111: Line 111:
 In the C4ENG public directory, inside the **bcf0** subdirectory, will be a copy of my implementation (in executable form), which abides by the project specifications. Please compare its output against that of your implementation. In the C4ENG public directory, inside the **bcf0** subdirectory, will be a copy of my implementation (in executable form), which abides by the project specifications. Please compare its output against that of your implementation.
  
 +=====Obtaining binary values=====
 +You might wonder how, when you are limited to non-binary input, how you can obtain the binary value so that you can work with it.
 +
 +There are a couple ways to go about this:
 +
 +====Method one: convert from decimal to binary====
 +Using the division method, you can convert a decimal value to binary, continually dividing the value (or its quotient) by the base, until the quotient is 0, then we use the remainders to give us the binary value:
 +
 +  * value / base
 +  * value = 15
 +  * value / 2
 +    * ie: 15 / 2
 +      * quotient: 7
 +      * remainder: 1
 +  * value = quotient
 +  * value / 2
 +    * ie: 7 / 2
 +      * quotient: 3
 +      * remainder: 1
 +  * value = quotient
 +  * value / 2
 +    * ie: 3 / 2
 +      * quotient: 1
 +      * remainder: 1
 +  * value = quotient
 +  * value / 2
 +    * ie: 1 / 2
 +      * quotient: 0
 +      * remainder: 1
 +
 +The binary for 15 (decimal) is 1111 (binary)
 +
 +Or:
 +  * value = 11
 +  * value / 2
 +    * quotient: 5
 +    * remainder: 1
 +  * value = 5
 +  * value / 2
 +    * quotient: 2
 +    * remainder: 1
 +  * value = 2
 +  * value / 2
 +    * quotient: 1
 +    * remainder: 0
 +  * value = 1
 +  * value / 2
 +    * quotient: 0
 +    * remainder: 1
 +
 +The binary for 11 (decimal) is 1011 (binary).
 +
 +NOTE that the order in which we get the remainders produces the number from right to left.
 +
 +====Method 2: bitwise AND the place values====
 +If one understands the weight values corresponding with the places of each bit in a binary number, we can simply do a bitwise AND and see if the result is greater than 0 or not:
 +
 +^  2 to the 7  ^  2 to the 6  ^  2 to the 5  ^  2 to the 4  ^  2 to the 3  ^  2 to the 2  ^  2 to the 1  ^  2 to the 0  ^
 +|  128  |  64  |  32  |  16  |  8  |  4  |  2  |  1  |
 +
 +So, if we wanted to see if the 2 to the 7th bit is active, we can simply:
 +
 +<code c>
 +    value  = number & 128;
 +</code>
 +
 +We can then check of value is greater than 0; if it is, we've got a 1 in that position, if it isn't, we have a 0.
 +
 +We can then repeat the operation for 64, 32, 16, etc. down to 1, to get each bit of our number.
 +
 +NOTE that I have only taken us out to 8-bits. You may need to extend this to incorporate all the allowed values for this project.
 +
 +====Using Arrays====
 +Depending on the approach taken, you might have a need to store each of the bits for later display (for example, if you produce the bits in reverse order to the manner you wish to display them).
 +
 +An array is like a regular variable, although instead of being able to store exactly one value, it acts as a container for MANY variables (all of the same type). We can then address each value through an offset (0 being the first slot, 1 being the second, etc.)
 +
 +===The utility of arrays===
 +First, we need to identify a need; just as we needed to do with loops.
 +
 +Let's say we had the following standalone variables:
 +
 +<code c>
 +    int num1  = 13;
 +    int num2  = 73;
 +    int num3  = 26;
 +    int num4  = 57;
 +</code>
 +
 +We likely understand how to work with each of the four independent variables, but we can't exactly automate accessing them, such as through a loop.
 +
 +This is where an array can come in handy. Witness, the equivalent storage of numbers using an array:
 +
 +<code c>
 +    int num[4]; // declare a 4 element integer array
 +    num[0]  = 13;
 +    num[1]  = 73;
 +    num[2]  = 26;
 +    num[3]  = 57;
 +</code>
 +
 +What value does this offer us? Well, for one, we can automate the access of the array. Let's say we wanted to display the array contents (we have to do so one element at a time):
 +
 +<code c>
 +    int index  = 0;
 +    int max    = 4;
 +    for (index = 0; index < max; index = index + 1)
 +    {
 +        fprintf (stdout, "%d\n", num[index]);
 +    }
 +</code>
 +
 +Perhaps an array can be of some use in this project?
 =====Submission===== =====Submission=====
 To successfully complete this project, the following criteria must be met: To successfully complete this project, the following criteria must be met:
haas/fall2019/c4eng/projects/bcf0.1569847570.txt.gz · Last modified: 2019/09/30 12:46 by wedge