Skip to main content
gamedev.net
🔒 Locked

C++ question

Started by Alessandro 23 replies 5k views
swiftcoder
swiftcoder
Adds one to nX if there are 1, 2 or 5 triangles.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
DevFred
DevFred
The ?: part is redundant, booleans are implicitly converted to 1 and 0.

And if nX is of type int, which I presume it is, you don't need the float cast either.
result = nX + ( nTri == 1 || nTri == 2 || nTri == 5 );
Cornstalks
Cornstalks
e1 ? e2 : e3 means this:
If e1 is true, then e2, else e3. So look at the following:

bool flag = false;
int x = flag ? 42 : 3;

This means if flag is true, then 42, else 3. So in that example, x = 3 since flag is false, so 3 is returned from the boolean expression.

e1 ? e2 : e3 is the ternary operator in C++. MSDN documentation.
DevFred
DevFred
If nTri is guaranteed to always be less than 32, you can make the code even shorter:
result = nX + ((38 >> nTri) & 1);

If you wonder where the magic number 38 comes from, it's 32 + 4 + 2 = 2^5 + 2^2 + 2^1.
summaky
summaky
Quote:
Original post by DevFred
If nTri is guaranteed to always be less than 32, you can make the code even shorter:
result = nX + ((38 >> nTri) & 1);

If you wonder where the magic number 38 comes from, it's 32 + 4 + 2 = 2^5 + 2^2 + 2^1.


Even if you know where the magic number comes from, do not use this. There's nothing to gain in making this code "shorter", we just make it harder to understand.
jpetrie
jpetrie
I prefer the shorter one just a bit, in this case -- magic number notwithstanding. The expression is simpler, although it does obfuscate the conditional semantic.

However I find neither one of them ideal, I think I'd rather have an explicit "if" in this particular scenario.
alvaro
alvaro
Why are 1, 2 and 5 triangles special cases? I would try to find out, give it a name and make a function.

result = nX + is_number_of_triangles_special(nTri);

The implementation of is_number_of_triangles_special can use whatever trick you want, but make sure to include a good comment explaining what it does, and why 1, 2 and 5 are special.
Daggerbot
Daggerbot
Quote:
Original post by summaky
Quote:
Original post by DevFred
If nTri is guaranteed to always be less than 32, you can make the code even shorter:
result = nX + ((38 >> nTri) & 1);

If you wonder where the magic number 38 comes from, it's 32 + 4 + 2 = 2^5 + 2^2 + 2^1.


Even if you know where the magic number comes from, do not use this. There's nothing to gain in making this code "shorter", we just make it harder to understand.


I would go with the shorter one if performance was an issue, but also supply a comment with the original expression to clarify its functionality.
Zahlman
Zahlman
I'm bored.

// very evil#define is +struct in {  int bitset;  in(int initial): bitset(1 << initial) {}  in& operator()(int next) { bitset |= 1 << next; }};bool operator+(int lhs, const in& rhs) {  return (rhs.bitset >> lhs) & 1;}result = nX + float(nTri is in(1)(2)(5));
someboddy
someboddy
Quote:
Original post by Zahlman
I'm bored.

*** Source Snippet Removed ***


Shouldn't "in" be a class instead of a struct?
-----------------------------------------Everyboddy need someboddy!
swiftcoder
swiftcoder
Quote:
Original post by Zahlman
I'm bored.
*** Source Snippet Removed ***
And Zahlman pulls into an early lead for the 2009 edition of 'most evil perversion of C++ syntax' [wink]
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
rip-off
rip-off
Quote:
Original post by someboddy
Quote:
Original post by Zahlman
I'm bored.

*** Source Snippet Removed ***


Shouldn't "in" be a class instead of a struct?


Why? You are aware of the differences between classes and structs in C++, yes?
SiCrane
SiCrane
Quote:
Original post by swiftcoder
And Zahlman pulls into an early lead for the 2009 edition of 'most evil perversion of C++ syntax' [wink]


At least he didn't overload operator , this time.
someboddy
someboddy
Quote:
Original post by rip-off
Quote:
Original post by someboddy
Quote:
Original post by Zahlman
I'm bored.

*** Source Snippet Removed ***


Shouldn't "in" be a class instead of a struct?


Why? You are aware of the differences between classes and structs in C++, yes?


That classes can have functions that automatically get a reference to the object, and structs can't?
-----------------------------------------Everyboddy need someboddy!
rip-off
rip-off
Quote:

That classes can have functions that automatically get a reference to the object, and structs can't?

Sure they can. The only difference between a struct and a class in C++ is default visibility. A struct's members are public by default, while a classes are private. When using inheritance, a struct defaults to public inheritance and a class to private. A struct can have member functions, constructors and a destructor etc.
Zahlman
Zahlman
Quote:
Original post by SiCrane
Quote:
Original post by swiftcoder
And Zahlman pulls into an early lead for the 2009 edition of 'most evil perversion of C++ syntax' [wink]


At least he didn't overload operator , this time.


Be careful what you wish for? :s

// variadic macro support required. Removed the 'is' to match Python syntax.#define in(x) + (IN(), __VA_ARGS__)struct IN {  int bitset;  IN(int initial): bitset(1 << initial) {}  // Ack, I forgot to 'return *this;' in the previous version and noone -  // oh, nm, well spotted, iMalc :)  IN& operator,(int next) { bitset |= 1 << next; return *this; }};bool operator+(int lhs, const IN& rhs) {  return (rhs.bitset >> lhs) & 1;}result = nX + float(nTri in(1, 2, 5));
Cornstalks
Cornstalks
Quote:
Original post by Zahlman
Be careful what you wish for? :s

*** Source Snippet Removed ***
Gaaa my eyes! Do you really ever write code like that in your programs?
spx2
spx2
This is total awesomeness !

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.