Hello!
I don’t know Ruby but I need to understand how the input value is manipulated in the lines below.
I think that the input is converted to the sum of the values of its characters but the second line is confusing; does it take the final value of the sum and perform the bitwise operations or per iteration? Could you provide a simple explanation of the steps followed?
ch.ord is the binary value of a character
^ is exclusive OR ( XOR) between 2 numbers
‘<< 1’ is shifting a (binary) number (mulltiply it by 2)
‘>> 1’ is right shifting a (binary) number (div it by 2)
the value of the last expression of the bloc is the new value of ‘sum’, and the value returned by inject().
sum << 8 is shift of one octet the current ‘sum’ value.
(ch.ord) ^ (sum >> 4) is XOR between number value of curent character XOR current sum divided by 16