User Tools

Site Tools


notes:fall2024:projects:dsr1

dsr1

Table

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

bitwise AND

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

bitwise iOR

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 “|”

bitwise XOR

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 “^”

bitwise NOT

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).

bitwise NAND

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.

bitwise NOR

bitwise XNOR

Super Awesome Notes

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

notes/fall2024/projects/dsr1.txt · Last modified: 2024/10/10 03:47 by bpatrice