I didn't take a deep dive into your code, but I notice one difference between your C code and your blocks:
Thw ^ operator in C is XOR, the exclusive OR operator.
I don't see you doing a bitwise xor in your blocks.
I don't have the capacity to reverse engineer your blocks any further at this moment.