Code Snippets
2009-07-30 15:49
Fred
Haben wir schon einen Thread über lustige kleine Programmschnippsel?
Bei der Analyse von Primfaktoren zum Generieren von Hashcodes bin ich heute durch Zufall auf ein völliges anderes Phänomen gestoßen, das ich Euch nicht vorenthalten will. Was macht das folgende Programm?
Bei der Analyse von Primfaktoren zum Generieren von Hashcodes bin ich heute durch Zufall auf ein völliges anderes Phänomen gestoßen, das ich Euch nicht vorenthalten will. Was macht das folgende Programm?
#include <stdio.h>
#include <limits.h>
int main(void)
{
int x = INT_MIN;
while (x)
{
int y = 0, a = x & 1, n = 1;
do
{
int b = x < 0;
y = (y << 1) | (a ^ b);
x <<= 1;
a = b;
putchar(32 << b);
} while (n <<= 1);
x = y;
putchar('\n');
}
return 0;
}
Und für die Java-Programmierer ohne C-Compiler:
class WhyDoesEverythingHaveToBeNestedInsideAClass
{
public static void main(String[] args)
{
int x = Integer.MIN_VALUE;
while (x != 0)
{
int y = 0, a = x & 1, n = 1;
do
{
int b = x < 0 ? 1 : 0;
y = (y << 1) | (a ^ b);
x <<= 1;
a = b;
System.out.print((char)(32 << b));
} while ((n <<= 1) != 0);
x = y;
System.out.println();
}
}
}
[28]