Some useful commands for Problem Set #4
Math 202 Cryptography
September 16, 2020

T. Ratliff, Wheaton College
ratliff_thomas@wheatoncollege.edu

Expressing values in hexadecimal

We can indicate that we are using a base-16 representation by using the 16^^ notation. 
For example, 1A in hexadecimal represents the decimal value 26

16^^1A

Bitwise comparison of two values

The BitXor[a,b] command will take the XOR of the binary values of a and b. 
For example, BitXor[ 16^^13, 16^^27] performs the calculation 
16^^13 XOR 16^^27 
    =   0001 0011 
XOR  0010 0111
    =   0011 0100
    =  16^^34
    =  52 (base 10)

BitXor[16^^13,16^^27]

Also notice that BitXor[a,b] gives a 0 if a and b agree in a location and 1 if they disagree.
Thus, the total number of 1’s in BitXor[a,b] tells how many bits a and b differ by

One way to count the number of 1’s is to convert the binary representation of the number to a list and then add them up.

IntegerDigits[52, 2]

Total[{1,1,0,1,0,0}]

Putting it all together

We can do this all in one step, as well

Total[ IntegerDigits[ BitXor[16^^13, 16^^27],2]]

And notice this works for longer hex values as well

Total[ IntegerDigits[ BitXor[16^^ddfcfcbe0713919607872e871256f8a6, 16^^574f9474fed01a4fc0fb9b8d531ac398],2]]