Discuss Scratch

maths_genius1234
Scratcher
82 posts

XOR logic gate with hexadecimal

Hi there, I need some help with creating an XOR gate that works directly with hexadecimal or base-16. I only need it to be two inputs. Been searching around for XOR gates, but most only support binary, let alone hexadecimal. Wondering if anyone could help me. Thanks!

Sorry, I find logic gates hard…

-maths_genius1234
awesome-llama
Scratcher
1000+ posts

XOR logic gate with hexadecimal

you want to XOR the binary representation of a base 16 number? How many digits?
maths_genius1234
Scratcher
82 posts

XOR logic gate with hexadecimal

Would it be possible to XOR two inputs directly with hexadecimal without converting to binary? I'm wondering if it will be more efficient.

Thanks!
awesome-llama
Scratcher
1000+ posts

XOR logic gate with hexadecimal

maths_genius1234 wrote:

Would it be possible to XOR two inputs directly with hexadecimal without converting to binary? I'm wondering if it will be more efficient.
Try constructing a table from every possible pair of inputs (try 1 hexadecimal digit). I don't know what it'll look like. You might find some patterns you can exploit with maths. If not, at least you now have a lookup table you can implement in Scratch.

Last edited by awesome-llama (March 22, 2025 08:53:24)

maths_genius1234
Scratcher
82 posts

XOR logic gate with hexadecimal

Thanks; I will try to make a table. I'm not the most experienced with stuff like this.
I'm also a big fan of your projects!
-sbossbattle_
Scratcher
100+ posts

XOR logic gate with hexadecimal

I don't know much about XOR gates, but I think maybe you can have each hexadecimal digit (0-9,A-F) linked to an ID corresponding to their value (0-16). Then, for each hexadecimal, turn it into a list (Ex: “FF16” -> (“F”,“F”,“1”,“6”) -> (16,16,1,6)) then use the corresponding IDs to compare the number. Hope this works! (probably doesn't though lol)

Last edited by -sbossbattle_ (March 22, 2025 23:13:39)

maths_genius1234
Scratcher
82 posts

XOR logic gate with hexadecimal

-sbossbattle_ wrote:

I don't know much about XOR gates, but I think maybe you can have each hexadecimal digit (0-9,A-F) linked to an ID corresponding to their value (0-16). Then, for each hexadecimal, turn it into a list (Ex: “FF16” -> (“F”,“F”,“1”,“6”) -> (16,16,1,6)) then use the corresponding IDs to compare the number. Hope this works! (probably doesn't though lol)
Thanks! For now, I have an XOR lookup table. Unfortunately, I need to XOR two digits of hexadecimal values together, which is why I have a 256*256 grid of values. Thanks for your help, though. I will try your method if the table doesn't work.
-sbossbattle_
Scratcher
100+ posts

XOR logic gate with hexadecimal

maths_genius1234 wrote:

-sbossbattle_ wrote:

I don't know much about XOR gates, but I think maybe you can have each hexadecimal digit (0-9,A-F) linked to an ID corresponding to their value (0-16). Then, for each hexadecimal, turn it into a list (Ex: “FF16” -> (“F”,“F”,“1”,“6”) -> (16,16,1,6)) then use the corresponding IDs to compare the number. Hope this works! (probably doesn't though lol)
Thanks! For now, I have an XOR lookup table. Unfortunately, I need to XOR two digits of hexadecimal values together, which is why I have a 256*256 grid of values. Thanks for your help, though. I will try your method if the table doesn't work.
lol i just realized my method is just converting it to base 10
PlatoHero_
Scratcher
500+ posts

XOR logic gate with hexadecimal

maths_genius1234 wrote:

-sbossbattle_ wrote:

I don't know much about XOR gates, but I think maybe you can have each hexadecimal digit (0-9,A-F) linked to an ID corresponding to their value (0-16). Then, for each hexadecimal, turn it into a list (Ex: “FF16” -> (“F”,“F”,“1”,“6”) -> (16,16,1,6)) then use the corresponding IDs to compare the number. Hope this works! (probably doesn't though lol)
Thanks! For now, I have an XOR lookup table. Unfortunately, I need to XOR two digits of hexadecimal values together, which is why I have a 256*256 grid of values. Thanks for your help, though. I will try your method if the table doesn't work.
All you should need is a look-up table of 256 values, since there are 16² variations of one-digit hexadecimal operands. When handling the 2-digit hexadecimal numbers, all you really need to do is XOR the digits one-by-one. Here is an example of that:

F6 XOR C1
(F XOR C) = 3
(6 XOR 1) = 7
Therefore, (F6 XOR C1) = 37
Scratch-Minion
Scratcher
1000+ posts

XOR logic gate with hexadecimal

I don't think you need look up tables.

First convert each hex digit to 4 digit binary numbers.
(You could use a table with 16 binary values 0000,0001,0010, … 1111 to do the conversions)

Then using the “letter of …” operator, extract the four binary digits separately.

Now the XOR.
If the binary digits are different from the two numbers, then there will be a 1 in the XOR otherwise a 0.
Combine these XOR digits into a 4 digit binary number, then convert to hex.

If your numbers are each two hex digits, eg. F6 and C1, then do F XOR C and 6 XOR 1 as PlatoHero_ suggest above.


Here is some code that would do this:

setHex Digit 1toAsetBinary for Hex Digit 1toitemjoin0xHexDigit1+1of4 digit binary numbers list with 0000,0001,0010 ... 1111setHex Digit 2toCsetBinary for Hex Digit 2toitemjoin0xHexDigit2+1of4 digit binary numberssetBinary XORtonotletter1ofBinaryforHexDigit1=letter1ofBinaryforHexDigit2+0setBinary XORtojoinBinaryXORnotletter2ofBinaryforHexDigit1=letter2ofBinaryforHexDigit2+0setBinary XORtojoinBinaryXORnotletter3ofBinaryforHexDigit1=letter3ofBinaryforHexDigit2+0setBinary XORtojoinBinaryXORnotletter4ofBinaryforHexDigit1=letter4ofBinaryforHexDigit2+0setHexadecimal XORtoletterjoin0bBinaryXOR+1of0123456789ABCDEF


Notes:

The calculation of Binary XOR could be done in one long line with more “join” operators.

The result of a boolean is true or false, but if the boolean is used with mathematical operators then true becomes 1 and false becomes 0.
This is why I add 0 to the boolean when calculating digits of Binary XOR.

If you join 0x to the front of a hexadecimal number, then use it with mathematical operators, the hex number will be converted to decimal.
If you join 0b to the front of a binary number, then use it with mathematical operators, the binary number will be converted to decimal.

*** EDIT ***
I see that some of the lines calculating Binary XOR have been truncated.
Just click “Quote” at the bottom right, and you can see the complete lines with the “+ 0” included at the end.

Last edited by Scratch-Minion (March 23, 2025 21:23:29)

maths_genius1234
Scratcher
82 posts

XOR logic gate with hexadecimal

Scratch-Minion wrote:

I don't think you need look up tables.

First convert each hex digit to 4 digit binary numbers.
(You could use a table with 16 binary values 0000,0001,0010, … 1111 to do the conversions)

Then using the “letter of …” operator, extract the four binary digits separately.

Now the XOR.
If the binary digits are different from the two numbers, then there will be a 1 in the XOR otherwise a 0.
Combine these XOR digits into a 4 digit binary number, then convert to hex.

If your numbers are each two hex digits, eg. F6 and C1, then do F XOR C and 6 XOR 1 as PlatoHero_ suggest above.


Here is some code that would do this:

setHex Digit 1toAsetBinary for Hex Digit 1toitemjoin0xHexDigit1+1of4 digit binary numbers list with 0000,0001,0010 ... 1111setHex Digit 2toCsetBinary for Hex Digit 2toitemjoin0xHexDigit2+1of4 digit binary numberssetBinary XORtonotletter1ofBinaryforHexDigit1=letter1ofBinaryforHexDigit2+0setBinary XORtojoinBinaryXORnotletter2ofBinaryforHexDigit1=letter2ofBinaryforHexDigit2+0setBinary XORtojoinBinaryXORnotletter3ofBinaryforHexDigit1=letter3ofBinaryforHexDigit2+0setBinary XORtojoinBinaryXORnotletter4ofBinaryforHexDigit1=letter4ofBinaryforHexDigit2+0setHexadecimal XORtoletterjoin0bBinaryXOR+1of0123456789ABCDEF


Notes:

The calculation of Binary XOR could be done in one long line with more “join” operators.

The result of a boolean is true or false, but if the boolean is used with mathematical operators then true becomes 1 and false becomes 0.
This is why I add 0 to the boolean when calculating digits of Binary XOR.

If you join 0x to the front of a hexadecimal number, then use it with mathematical operators, the hex number will be converted to decimal.
If you join 0b to the front of a binary number, then use it with mathematical operators, the binary number will be converted to decimal.

Thanks! I was originally thinking of making the XOR directly in hexadecimal without binary but it appears the only method of doing that is a lookup table which is tedious and annoying to set up. (I'm lazy).
EpicGhoul993
Scratcher
1000+ posts

XOR logic gate with hexadecimal

[Offtopic]

maths_genius1234 wrote:

I was originally thinking of making the XOR directly in hexadecimal without binary
Now I'm curious. How do you even do this?
All the solutions I can find either just use a lookup table or convert numbers to binary.
maths_genius1234
Scratcher
82 posts

XOR logic gate with hexadecimal

EpicGhoul993 wrote:

[Offtopic]

maths_genius1234 wrote:

I was originally thinking of making the XOR directly in hexadecimal without binary
Now I'm curious. How do you even do this?
All the solutions I can find either just use a lookup table or convert numbers to binary.
Yes, it is really weird as XOR operations are usually done in 0's and 1's (binary) so I'm trying to think of more efficient methods with hexadecimal.
MultiTasker801
Scratcher
100+ posts

XOR logic gate with hexadecimal

i came up with this solution (link) using a lookup table
i stuck with a lookup table because it's faster than anything else i could think of, but an approach that converts the hex to decimal and then does xor with that and converts back to hex is definitely possible (and probably not too bad)

Powered by DjangoBB