Handling both little and big endian architectures in calculating distances