C++ Expression

Started by
7 comments, last by Marcus Speight 18 years, 1 month ago
How can this be translated into C#? Its supposed to return a bool, and I know its some kind of bitwise operation, but what exactly does "in" mean? return (( in(z)& ~(in(x)|in(y)) ) & 0x80000000);
Advertisement
Quote:Original post by phb5000
but what exactly does "in" mean?


It could mean a number of things, it definitely isn't a keyword. It could be a call to a macro, call to a function, call to a user-defined type with the function-call operator overloaded, a call to a constructor of user-defined type called in, etc, etc.

Where does it come from and can't you need to find the definition (or at the very least declaration) of in.
Sorry, I didnt notice a bit of code: it was just a reference
I still dont understand how it works. How does it become a bool? :S
How can I convert the return expression to C#?

typedef unsigned int uint32;#define in(a) ((uint32&) a)bool checkPointInTriangle(const VECTOR& point, const VECTOR& pa,const VECTOR& pb, const VECTOR& pc){VECTOR e10=pb-pa;VECTOR e20=pc-pa;float a = e10.dot(e10);float b = e10.dot(e20);float c = e20.dot(e20);float ac_bb=(a*c)-(b*b);VECTOR vp(point.x-pa.x, point.y-pa.y, point.z-pa.z);float d = vp.dot(e10);float e = vp.dot(e20);float x = (d*c)-(e*b);float y = (e*a)-(d*b);float z = x+y-ac_bb;return (( in(z)& ~(in(x)|in(y)) ) & 0x80000000);}
Hey,

If you have the floats x,y and z you should use ((System.UInt32)x) to make the floats into a unsigned int. Thats what de in(a) does in your C code.

Next your function returns an unsigned int, which is either 0 (false in C) or not 0 (true in C).

To make it a bool you could simply add != 0 to the expression.

Your return statement should look something like:

//for readability:
System.UInt32 ix = (System.UInt32) x;
System.UInt32 iy = (System.UInt32) y;
System.Uint32 iz = (System.UInt32) z;

return ((iz & ~(ix | iy)) & 0x80000000) != 0; //which is type 'bool'
Quote:Original post by phb5000
I still dont understand how it works. How does it become a bool? :S
How can I convert the return expression to C#?

*** Source Snippet Removed ***


OK, 'in' here is a macro. It means that every time preprocessor encounters 'in(something)', it replaces it by ((uint32&) something).

So the return statement becomes:
 return (( ((uint32&) z)& ~(((uint32&) x)|((uint32&) y)) ) & 0x80000000); 


How does it translate into a bool? Well, it's an integer. In C++, when an integer is converted to a bool, it takes the value "false" if the integer value is zero, and "true" in all other cases.

One word about the construct ((uint32&) z). Here, the programmer is telling the compiler to use a reference of an object which must be considered as a "uint32" but which is a float variable called 'z'. This way, it converts the float variable to an integer one which has the same internal representation. It means that the mantissa, exponent and sign bit of the float are considered as normal bits of an integer. That's a dirty trick done purposely for optimization. I don't know C# so I don't know if this can be converted easily.
This dirty trick relies on floats being 4 bytes in length and the MSB of a float being its sign (+/-).

It simplifies to

[pseudocode]if z is negative and x is positive and y is positive   return trueelse   return false[/pseudocode]


also this trick is flawed in C++ as 0x80000000 is truncated to false as bool is usually 8 bits in length. Adding !! before the first bracket works.
Marcus SpeightIf at first you don't succeed.
Destroy all evidence that you tried.
The trick is not flawed in C++ (although it is still evil), since the value will undergo the well-defined boolean conversion:
Quote:C++ Final Draft Standard, Section 4.12, Paragraph 1
An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

Σnigma
Ok. Flawed in VC++ 6. But then again so is most things.
Marcus SpeightIf at first you don't succeed.
Destroy all evidence that you tried.

This topic is closed to new replies.

Advertisement