# Guess the code!

Show 80 post(s) from this thread on one page
Page 2 of 7 First 1234567 Last
• 11-06-2006
Dave_Sinkula
Quote:

Originally Posted by King Mir
That would produce the highest exponent of two storable in each of those datatypes.

Give that man a cigar!

Or as I might say, these are several methods that result in "a value with only the most significant bit set".

New one:
Code:

```#define DECIMALS(itype) \ sizeof(itype) * (CHAR_BIT * 12655ul) / 42039 ((itype) -1 < 0) + 1 )```
• 11-07-2006
Mario F.
Quote:

Originally Posted by Dark_Phoenix
Well, thats interesting...
That compiles and runs fine... I get 25

God! I have to say it again?
Any number of plus signs! No try and do that with one more plus sign and see if it compiles. Sheesh! What's the point of a brain teaser if there is no teasing? Surely a superior mind like yourself can read what I said before attempting a solution.
• 11-07-2006
TriKri
Quote:

Originally Posted by Dave_Sinkula
Code:

```#define DECIMALS(itype) \ sizeof(itype) * (CHAR_BIT * 12655ul) / 42039 ((itype) -1 < 0) + 1 )```

((itype) -1 < 0) + 1 gives 2 if itype is a signed type, else 1. I'm not sure what 42039 ((itype) -1 < 0) + 1 ) does, though, and I wonder where you got the numbers from... O.O

Edit: My new one is a complete program this time:

Code:

```#include <stdio.h> #include <complex> int main() {     const double h = 0.001;     std::complex<double> z(1.0, 0.0), f(1.0, h);     int i = 0;     do {z *= f; i++;} while (imag(z) > 0);     printf("%1.9lf\n", i*h - imag(z)/real(z));         system("pause");     return 0; }```
• 11-07-2006
jmajeremy
Quote:

Originally Posted by Mario F.

Think of that while you give an answer to this:
I'm not anywhere close to be a C++ guru to even pretend I can create brain teasers... So, I'll cheat with someone else's brain teaser.

Without using macros or any other preprocessor tricks, have an apparently infinite number of plus signs appear consecutively, without spaces in between, and still be perfectly valid code? (note: no, not inside a comment. code that the compiler will process)

Here's your code for creating an apparently infinite number of plus signs...
Code:

```#include <iostream> using namespace std; int main() {     int val = 1;     while (val = 1)     {           cout << "+";     }     return 0; }```
I tested it; untill you exit the program you will get an infinite amount of consecutive + signs!
• 11-07-2006
Mario F.
I give up.
• 11-07-2006
Dark_Phoenix
Let me clarify, I did not know you could use more than two plus signs for an increment until you posted that question yesterday. As I said, interesting... I was not offering a solution.

As for adding one more plus sign, that will give a non-lvalue error or something I am sure as the increment uses 2 plus signs, having an odd number of +'s should be illegal.....
• 11-07-2006
King Mir
Quote:

Originally Posted by Mario F.
God! I have to say it again?
Any number of plus signs! No try and do that with one more plus sign and see if it compiles. Sheesh! What's the point of a brain teaser if there is no teasing? Surely a superior mind like yourself can read what I said before attempting a solution.

I think you could make it work if you made val an object and overrode unairy plus (as in +val) to return a reference. You would also have to override prefix ++ to similarly return a reference.
• 11-07-2006
jafet
These three C programs are of the same length, and essentially do the same thing. What is it they do? ;)

Code:

```main(a){while(a=getchar())putchar(a-1/((a^32&a)/13*2-11)*13);} main(a){while(a=getchar())putchar(a-1/(~(~a|32)/13*2-11)*13);} main(a){while(a=getchar())putchar(a+1/(11-(a&32^a)/13*2)*13);}```
It might be helpful to consult the binary representation of ASCII characters, or just input "furrfu sheesh" into stdin.
• 11-07-2006
Mario F.
Quote:

Originally Posted by King Mir
I think you could make it work if you made val an object and overrode unairy plus (as in +val) to return a reference. You would also have to override prefix ++ to similarly return a reference.

That's the answer yes. The key being the operator+() returning a reference instead of the usual rvalue.

Code:

```class CClass { public:     CClass(int v):val_(v) {}     CClass& operator+ () { return *this; }     CClass& operator++() { ++val_; return *this; }     friend std::ostream& operator<<(std::ostream&, const CClass&); private:     int val_; }; std::ostream& operator<<(std::ostream& os, const CClass& buff) {     os << buff.val_;     return os; } int main() {     CClass val = 7;     std::cout << +++++++val;     return EXIT_SUCCESS; }```
+++++++val is tokenized by the compiler as ++ ++ ++ +. With built-in types this always gives an error because contrary to the ++ operator, the unary + operator returns a rvalue, hence the compiler error. However, if we create a class and overload both operators it is possible to slightly cheat by making the unary operator return a reference while still maintaining the same behavior.

Of course, this new type CClass is an oddity since it becomes possible to write things like:

+val = 13;
• 11-07-2006
maxorator
Code:

```COORD c;   c.X = x - 1 ;   c.Y = y - 1 ;```
If you run this into a loop, it would do nothing useful, because you just declared c, you didn't assign any value to it. And even if you did assing, let's say 0 to it, it will always be -1.
• 11-07-2006
Dave_Sinkula
Oops.
Code:

```#define DECIMALS(itype) \ ( sizeof(itype) * (CHAR_BIT * 12655UL) / 42039 + ((itype) -1 < 0) + 1 )```
• 11-07-2006
manutd
Try this one:
Code:

```int firstnum = factorial(a); int secondnum = factorial(b); int thirdnum = factorial(a-b); thenum = firstnum/(secondnum*thirdnum);```
If factorial is a function that gives the factorial.
• 11-07-2006
TriKri
Quote:

Originally Posted by Dave_Sinkula
Oops.
Code:

```#define DECIMALS(itype) \ ( sizeof(itype) * (CHAR_BIT * 12655UL) / 42039 + ((itype) -1 < 0) + 1 )```

Aha, it is the maximum number of characters an integer type itype can require! I didn't see the definition was a macro until I noticed the \ sign, hehe. So I guess 12655/42039 is an approximation of log(2) then, it's pretty close. :)
• 11-07-2006
TriKri
Quote:

Originally Posted by manutd
Try this one:
Code:

```int firstnum = factorial(a); int secondnum = factorial(b); int thirdnum = factorial(a-b); thenum = firstnum/(secondnum*thirdnum);```
If factorial is a function the give the factorial.

The number of combinations you can choose b things among a things?
• 11-07-2006
manutd
Correct!
Show 80 post(s) from this thread on one page
Page 2 of 7 First 1234567 Last