# Guess the code!

Show 80 post(s) from this thread on one page
Page 1 of 7 1234567 Last
• 11-06-2006
TriKri
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? :p
• 11-06-2006
bumfluff
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! :D
• 11-06-2006
anon
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(); }```
• 11-06-2006
TriKri
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.
• 11-06-2006
Mario F.
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.
• 11-06-2006
swgh
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
• 11-06-2006
Mario F.
Gee, I don't know, but if you replace x - 1 by x + 1, you have my salary graphic for the next 20 years
• 11-06-2006
swgh
Mario, you have been around the boards for a while, give us a real brain teaser
• 11-06-2006
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;`
• 11-06-2006
swgh
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-06-2006
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)
• 11-06-2006
Dark_Phoenix
Quote:

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........ ?
• 11-06-2006
Mario F.
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;`
• 11-06-2006
Dark_Phoenix
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
• 11-06-2006
King Mir
Quote:

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.
Show 80 post(s) from this thread on one page
Page 1 of 7 1234567 Last