What does this floating point formula mean?

OK, I was given a formula to determine the float value between 0.0 and 1.0 using i and j (the coordinates of the value in a 2D array). I just want to know exactly what this formula does. That makes no sense to me. I implemented it in my own function, where the int and i values ​​are passed as parameters. Can anyone give an explanation? I do not need to understand this, because he gave it to us only for use as it is, but I really want to know.

float col = float (((i & 0x08) == 0) ^ ((j & 0x08) == 0)); 

What exactly is going on here?

+6
source share
4 answers

The result, if you draw with i,j as the x, y coordinates, will be a chessboard with squares of 8x8 pixels.

i & 0x08 and j & 0x08 just check one bit of each axis. This bit will change state every 8 pixels.

== 0 converts each result into a boolean value, which evaluates to 0 or 1. It also inverts the result, but I do not think that this matches the general formula.

The exclusive operator ^ will return 0 if they match, or 1 if they are different. The way you get a chessboard - the result alternates every time either i or j crosses the border.

+7
source

There are many logical and bitwise Boolean operators . Let me try to answer in parts .. Allows first to break into pieces

A:(i & 0x08)
The execution of bitwise and on i is basically performed on each bit i and 0x08 (1000 in binary format)

B:A==0
Check bitwise and false value for EVERY BIT basically checks 4th bit from last 0

C: B ^ B'
Bitwise XOR- returns 1 if one of them, and not both of them is true (bitwise)

D:float(C)
Easy, throws C into the swim.

The end result - I don’t know ..

+2
source
 float col = float (((i & 0x08) == 0) ^ ((j & 0x08) == 0)); 

& 0x08 performs bitwise and with 8, which means that it extracts the fourth least significant bit (1 is the least and then 2, 4, 8) from the numbers i and j . ^ is an exceptional OR operation: if both bits are the same, the result is 0, if they are different, the result is 1. This goes up to float external = float(...) , so col becomes 0.0 if i and j match, but 1.0 if they differ.

Why might this be helpful? It depends on what i and j . Presumably, the 4th bit encodes a specific condition or flag (logical), for example: whether a person is male or female. Operation & retrieves this, and then ^ says "are they different?". Why do you want a boolean expression to be converted to a float? There are not many good reasons to be honest - you can always allow the conversion to be done implicitly in the place where it was used (under the assumption of a man / woman):

 bool hetero = i & 0x08 ^ j & 0x08; float estimated_children_from_coupling = 1.3 * hetero; // same as hetero ? 1.3 : 0; 
+1
source

In short, "col = 1.0" if "i" OR "j" is 0x08 (decimal value 8).

(i and 0x08): not equal to zero if "i == 0x08", zero otherwise (i and 0x08) == 0: 1 / true if "i! = 0x08", 0 / false if "i == 0x08 "the same for" j "

Therefore, the "exclusive" or "operator" of the two sides will be fair for any of them, it is true, but not the other thing that happens when I OR j is 0x08.

Finally, it makes the result swim for any reason.

0
source

All Articles