# Thread: Guess the code!

1. ## Guess the code!

Now I got bored and thought of starting a "game".

Everyone can write code in this thread. Maybe a function or a simple algorithm or just a single code line.
I'll start with a piece of code. Someone else shall then guess what the code does and write their own piece of code, preferable in C or C++, and not to long.

So, here's the code in php taggs:
PHP Code:
``` (n & (n - 1)) == 0  ```
What does it do?

2. It cuts the x axis at 0 and 1? Duh, you have used so so the answer to that is no.

That was probably a really stupid answer as I dont understand what you are saying or find it in anyway fun!

3. I guess it tests if n is a power (or exponent, what-you-call-it) of 2. But why didn't you post C++ code?

Code:
```#include <iostream>

bool isExponentOfTwo(unsigned int n)
{
return (n&(n-1))==0;
}
int main()
{
std::cout << std::boolalpha;
for (unsigned int i=1; i <= 32; i++)
std::cout << i << " is a exponent of 2? " <<
isExponentOfTwo(i) << std::endl;
std::cin.get();
}```

4. Right! It checks whether n is a power of two or not.

Oh, I forgot to tell that it is not necessary to put the code in a context. Or maybe I'm just lazy!
It is C++ code, but if you are going to write in any other language like assembly, you'll have specify it.

5. I cannot go wrong if I answer instead that it does a bitwise comparison of a value by that same value minus one and compares the result to 0.

6. Ok, my turn:

Code:
```COORD c;
c.X = x - 1 ;
c.Y = y - 1 ;```
It's real easy, but I wanted to see if any noobs knew it

Somone else post a really difficult one

7. Gee, I don't know, but if you replace x - 1 by x + 1, you have my salary graphic for the next 20 years

8. That is a top answer.
Mario, you have been around the boards for a while, give us a real brain teaser

9. Code:
`unsigned char bit = 1 << (CHAR_BIT - 1);`
Code:
`unsigned short bit = (((unsigned short)-1) >> 1) + 1;`
Code:
`unsigned int bit = (-1U >> 1) + 1`
Code:
`unsigned long bit = ULONG_MAX / 2 + 1;`

10. Il get back to you dave, I have to sleep now il check the thread tomorrow. I am not backing out of it but its like 2am now and I have just finished my assignment, its due tomorrow, been working on it all night.

11. What's the other bad thing about having your arms amputated?

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)

12. Originally Posted by Mario F.
have an apparently infinite number of plus signs appear consecutively
Code:
`std::string plus = "++++++++++++++++++++++++++++++++++++....";`
or maybe
Code:
`for (;;) { std::cout<<"+"; }`
will display an infinite number of plus signs to the screen........ ?

13. To clarify it:

How can you make this compile without using any kind of preprocessor tricks and using any other number of plus signs?
Code:
`std::cout << ++++++++++++++++++++++++++++++++++++++++++++++++++val;`

14. Well, thats interesting...
Code:
```#include <iostream>

int main () {
int val = 0;
std::cout << ++++++++++++++++++++++++++++++++++++++++++++++++++val;
std::cin.get();
return 0;
}```
That compiles and runs fine... I get 25

15. Originally Posted by Dave_Sinkula
Code:
`unsigned char bit = 1 << (CHAR_BIT - 1);`
Code:
`unsigned short bit = (((unsigned short)-1) >> 1) + 1;`
Code:
`unsigned int bit = (-1U >> 1) + 1`
Code:
`unsigned long bit = ULONG_MAX / 2 + 1;`
That would produce the highest exponent of two storable in each of those datatypes.

Popular pages Recent additions