Corning Community College
CSCS2330 Discrete Structures
Influenced by the unix/usr0 'urev' skills review activity, and discrete/dsr0, create a program that does the same but for bitwise logic on nibble-sized groups of binary values ranging from 4 to 16 bits.
This does not have to be written for Vircon32, nor does it specifically have to be in C.
You will want to go here to edit and fill in the various sections of the document:
A | B | AND | OR | XOR | NAND | NOR | XNOR |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
0 | 1 | 0 | 1 | 1 | 1 | 0 | 0 |
1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
1 | 1 | 1 | 1 | 0 | 0 | 0 | 1 |
A | NOT |
---|---|
0 | 1 |
1 | 0 |
One way to do AND two values is manually.
One thing to keep in mind when doing it manually is that your numbers have to be the same length. One way to do that is to set a max length and then print it while padding it with spaces or zeros and capturing that output into a variable. Do this with both randomly generated numbers.
Then you can read each variable character by character against the other and do you operation on them.
For example (in bash):
for(( jndex=0; jndex<16; jndex++ )) do if [[ "${array1[$jndex]}" == 1 ]] && [[ "${array2[$jndex]}" == 1 ]] ; then VarArray+=1 else VarArray+=0 fi done
This is done for the programs answers to then check your user provided answer against later in the code.
You can also use bash pockets of arithmetic to do any of the bitwise operations, although they are very picky with number bases, so you will have to convert your numbers being compared into decimal to convert, then back into their base after doing logic. One way you can go about this is like this:
echo "obase=${ORIGINBASE}; $(( $(echo "obase=10; ibase=${ORIGINBASE}; ${FIRSTNUMBER}" | bc) & $(echo "obase=10; ibase=${ORIGINBASE}; ${SECONDNUMBER}" | bc) ))" | bc
You can use the same method(s) for iOR as you did with and, as the manual method works by giving it the cases for the operation, similarly to a truth table, and the pocket of arithmetic has a built in iOR operation “|”
You can use the same method(s) for iOR as you did with and, as the manual method works by giving it the cases for the operation, similarly to a truth table, and the pocket of arithmetic has a built in iOR operation “^”
echo 01010101 | tr '10' '01' or you can do tr '01' '10' both just flips it
If you use the bitwise NOT operation ~ (such as ~number1) and you do not want to use tr to flip the result, you can also use ‘and’ masking. (1«bit_length) - 1
where bit_length is a random number of bits (4, 8, 12, 16). This shifts the value 1 to the left by bit_length positions. With this mask, only the least significant bits are set to 1. For example, if bit_length
(or whatever variable you use) is 8, mask will be 11111111 (255 in binary). What this may look like: (~number) & mask
. You can also use this same ‘and’ masking for any other operations using bitwise NOT ~ (such as NAND, NOR, and XNOR).
Sort of like what you could do with the NOT operation you can do similar to the NAND operation. So, you could perform your AND operation first and then get the result from that and flip the bits to get your NAND result.
Part 1:
-Generate random number of nibbles 1-4
-Generate random number for each generated nibble
-Convert numbers to a base (2,4,8,16)
-Convert those BACK to binary
-Select a NEW base (not the previous one)
-The player needs to convert the binary to the NEW base's value
Example:
Let there be 2 nibbles.
Let nibble1 = 10 (decimal)
Let nibble2 = 5 (decimal)
First base is 16
10 → A / 5 → 5
In binary: 1010 / 0101
NEW base = 8
Together: 010100101 (ADD A ZERO TO THE FRONT TO MAKE 9 DIGITS, bc 2^3 = 8 ← NEED SETS OF 3)
The player needs to break this down mentally into 010 100 010 → 2 4 5
The answer: 245
What the player will see:
2 nibbles in base16: A and 5
ANSWER: 245
Part 2:
-Generate random binary number (ideally 4,8,12,16) digits in length
-Generate second binary number in same length
-Do bitwise logic on it
What the player will see:
1010 && 0110 ==== 0010
Let part 1 be something of a warmup. After X rounds, part 2 should kick in
To be successful in this project, the following criteria (or their equivalent) must be met:
Let's say you have completed work on the project, and are ready to submit, you would do the following:
lab46:~/src/SEMESTER/DESIG/PROJECT$ submit DESIG PROJECT file1 file2 file3 ... fileN
You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.
I'll be evaluating the project based on the following criteria:
182:dsr1:final tally of results (182/182) *:dsr1:displays table on early problems [26/26] *:dsr1:supports generation of similar size terms [26/26] *:dsr1:generates problems in groups of nibbles, from 4 to 16 bits [26/26] *:dsr1:supports bitwise AND, OR, NOT, XOR, XNOR, NAND, and NOR [26/26] *:dsr1:keeps score and track of correct and incorrect responses [26/26] *:dsr1:serves 12 problems every 4 hours and 20 minutes [26/26] *:dsr1:allow for erasing of mistakes by getting a run correct [26/26]