Obfuscated C++

Last Month's Obfuscated C++
Last month we asked you to explain the semantics of this function:

unsigned f(unsigned g ) {
  return (1 - (bool)(g/0x64))*(0x7D0-0x64*(g/0x32))+g;
We'll start by translating the unhelpful hex constants into decimal:

unsigned f(unsigned g ) {
  return (1 - (bool)(g/100))*(2000-100*(g/50))+g;
This function implements a "windowing" Y2K fix for two-digit unsigned integers. Any argument in the range [0..49] is converted to a 21st-century date by adding 2000; any argument in the range [50..99] is converted to a 20th-century date by adding 1900. Any argument greater than or equal to 100 is returned unchanged.

The expression (1 - (bool)(g/100)) evaluates to 1 for values less than 100, 0 for larger values. This causes the function to simply return its argument if the input argument is 100 or larger. For inputs less than 100, the expression (2000-100*(g/50)) is the offset to be added to the input argument. If the argument is 50 or more, this expression evaluates to 1900; otherwise it evaluates to 2000. This factor is added to the argument and the result returned.

This Month's Obfuscated C++

Consider this code fragment:

int* ip = &ip;
if (ip == &ip) cout << "Success\n";
This does not compile, because the types for the initialization and comparison don't match. An int* cannot be initialized with or compared to an int** without a cast:

int *ip = reinterpret_cast<int*>(&ip);
if (ip == reinterpret_cast<int*>(&ip)) cout << "Success\n";
For this month's puzzle, suggest a declaration or definition that will (1) allow the comparison without the casts and (2) evaluate to true. That is:

//What declaration of x?
if (x == &x) cout << "Success\n";

Rob Murray is Director of Operations at the Irvine office of Nuforia, an object-oriented software consulting company based in Houston, TX. He has taught C++ at technical conferences since 1987 and is the author of C++ Strategies and Tactics. He was the founding editor of C++ Report and can be contacted at rmurray@nuforia.com.


Upcoming Events


Sign up for our newsletter.

I agree to this site's Privacy Policy.