Discuss Scratch

Zro716
Scratcher
1000+ posts

Bitwise Logic Calculator

my class was starting a chapter on bitwise operations in computers and since I found the topic interesting I thought I might try it out in Scratch. It only took a day to make this (with some help from @Paddle2see's shunting-yard calculator and wikipedia). and so and so and soo…

it works great and also has a nice help system with visual aids

to use it, go here: http://scratch.mit.edu/projects/26517078

if you'd like to learn how it works, here's the breakdown plus examples that demonstrate the use of each operation.

This isn't like a normal calculator in which you enter 2 + 2 to get 4. This works with bitwise operators that do all sorts of cool tricks with binary code and logic gates. So to make your input, take the time to formulate from this list of logic gates:
AND (&)
Compares two inputs and returns true if and only if both inputs are true
0 and 0 = 0
0 and 1 = 0
1 and 0 = 0
1 and 1 = 1

OR (|)
Compares two inputs and returns true if either of the inputs are true
0 or 0 = 0
0 or 1 = 1
1 or 0 = 1
1 or 1 = 1

NAND (!&)
Compares two inputs and returns true if and only if the sum of the inputs is not true
0 nand 0 = 1
0 nand 1 = 1
1 nand 0 = 1
1 nand 1 = 0

NOR (!|)
Compares two inputs and returns true if both are false
0 nor 0 = 1
0 nor 1 = 0
1 nor 0 = 0
1 nor 1 = 0

XOR (x|)
Compares two inputs and returns true if only one input is true
0 xor 0 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0

XNOR (x!|)
Compares two inputs and returns true if both are true or both are false
0 xnor 0 = 1
0 xnor 1 = 0
1 xnor 0 = 0
1 xnor 1 = 1

NOT (!)
Inverts the truth of the input
not 0 = 1
not 1 = 0
So here's an example in C:
// assign the binary values of 133 and 105 to x and y respectively
int unsigned x = 10001001
int unsigned y = 01010011
// assign a third var z with the operation "x AND y"
int z = x & y
// z has a binary value of 00000001 because only the last bit matched
int w = x | y
// w has a binary value of 11011011 because the 1's were superimposed
w = (x ^ y) & z
/* breaking this down, x and y are compared in the XOR gate,
 * which returns a value of 1101 1010. Then, when compared with z,
 * the & operand returns 0000 0000 because none of the 1's matched. */

The calculator also offers some advanced bitwise manipulation like shifting and rotating:
LSHIFT (<<)
(Left Shift)
shift a byte to the left by n bits, adding zeros at the right for every bit discarded on the left
Ex: 00110000 << 2 = 11000000

RSHIFT (>>)
(Right Shift)
shift a byte to the right by n bits, adding zeros at the left for every bit discarded on the right
Ex: 00110000 >> 2 = 00001100

LCIRCSHIFT (c<<)
(Left Circle Shift)
move n bits from the left side of the byte to the right
Ex: 11100001 c<< 3 = 00001111

RCIRCSHIFT (c>>)
(Right Circle Shift)
move n bits from the right side of the byte to the left
Ex: 00111001 c>> 2 = 01001110
And last, you can assign variables (binary or decimal, doesn't matter) with the operator :=
Ex: derp := 99 & 01100011

I hope this proves valuable to your understanding of computers and how they operate with bits and bytes, so leave a love-it to let me know my hard work was worth it! Thanks for reading, Scratch on!~

As a long time Scratcher, I have found new meaning to the name “Scratch”: for me, it means to “scratch that itch”, to come back again and again to realize new ideas in this toy language, even when I'm capable of creating my projects in real programming languages years later. It's a friend that helped me to pursue programming and get me to enjoy its fruit. I'm certain many others who have walked this path as well have grown fond of its importance in their life.
Zro716
Scratcher
1000+ posts

Bitwise Logic Calculator

bump

As a long time Scratcher, I have found new meaning to the name “Scratch”: for me, it means to “scratch that itch”, to come back again and again to realize new ideas in this toy language, even when I'm capable of creating my projects in real programming languages years later. It's a friend that helped me to pursue programming and get me to enjoy its fruit. I'm certain many others who have walked this path as well have grown fond of its importance in their life.

Powered by DjangoBB