OK, I was given a formula to determine a float value between 0.0 and 1.0 using i and j (coordinates of a value in a 2D array). I simply want to know exactly what this formula does. It makes no sense to me. I have implemented it in its own function where the int values of i and j are passed as parameters. Can someone provide an explanation? I don’t HAVE to understand it, as he gave it to us just to use as is, but I really want to know.
float col = float (((i & 0x08) == 0) ^ ((j & 0x08) == 0));
What exactly is going on here?
The result, if plotted with
i,jas the x,y coordinates, will be a checkerboard with squares of 8×8 pixels.The
i & 0x08andj & 0x08are just testing a single bit of each axis. That bit will change state every 8 pixels.The
== 0will convert each result to a boolean, which evaluates to either a 0 or 1. It also inverts the result, but I don’t think that’s relevant in the overall formula.The
^exclusive-or operator will return 0 if the two are the same, or 1 if they’re different. That’s how you get the checkerboard – the result alternates each time eitheriorjcrosses a boundary.