tags:

views:

812

answers:

12

... and that has helped a lot to remember some programming concept.

A: 

nQueens with GA (genetic algorithm)

Davorin
+1  A: 

Trivia: overflowing an integer in C is undefined behaviour (C89 at least, haven't checked C99), but in C++ is defined for unsigned integral types.

Certainly helps remember to assess what size integer you should be using, if you pretend that overflowing it will crash the system instead of wrapping around. Obviously very few compilers/systems ever actually did that for arithmetic overflow in C.

Steve Jessop
I'm not sure what you mean by it not being undefined in C++ - the standard specifically says that "most existing implementations of C++ ignore integer overflows". An implementation is not required to throw an `overflow_error` for an arithmetic expression that overflows (though they could).
Michael Burr
3.1.9-4: "unsigned integers ... shall obey the laws of arithmetic modulo 2^n where n is the number of bits in the value representation". I'll correct my first sentence to specify unsigned, thanks. I guess modulo arithmetic is impossible in 1's comp or sgn-mag ints anyway due to the "missing" values.
Steve Jessop
+7  A: 

I think Duff's Device qualifies as trivia.

dsend(to, from, count)
char *to, *from;
int count;
{
    int n = (count + 7) / 8;
    switch (count % 8) {
    case 0: do { *to = *from++;
    case 7:      *to = *from++;
    case 6:      *to = *from++;
    case 5:      *to = *from++;
    case 4:      *to = *from++;
    case 3:      *to = *from++;
    case 2:      *to = *from++;
    case 1:      *to = *from++;
               } while (--n > 0);
    }
}

The concept that it illustrates is that there's always a better way to obfuscate (or optimize, take your pick) your code.

Bill the Lizard
+1  A: 
c=a+++++b;               // for one thing, spaces are not required in C.
gbarry
What does this one do?
Gonzalo Quero
Adding spaces: c = a++ + ++b. Increments b, adds it to a and assigns the result to c, then increments a.
Graeme Perrow
Which is to say, ++b; c = a + b; a++;
Bill the Lizard
Cool, thanks. I wasn't sure of the order.
Gonzalo Quero
Except that this is a syntax error in C/C++ because the parser is 'greedy'. It tries to parse the expression as `c = a++ ++ + b`.
Michael Burr
@Mike B: Spot on. You need at least one space to disambiguate (compiling with gcc).
Bill the Lizard
+7  A: 

Along the lines of pointless obfuscation in the name of optimization:

a ^= b; b ^= a; a ^= b;

(Try working it through with a couple binary numbers)

a=0110 (6) b=1010 (10)

a^=b; // a=1100
b^=a; // b=0110
a^=b; // a=1010

The only way I know of to swap two variables without using ANY additional memory.

Bill K
Perfectly illustrates "trivial" code. It's interesting to know that this clever hack^H^H^H^Htrick exists, but it should never actually be used.
Bill the Lizard
You can do the same with addition and subtraction. a+=b; b = a-b; a-=b;
Bill the Lizard
Hmm, you could also do "a=a+b;b=a-b;a=a-b;" or "a=a*b;b=a/b;a=a/b;" or dozens of such operations (anything with an inverse, really)... some of them can possibly overflow, though.
ShreevatsaR
@ShreevatsaR: You're right. The ^= implementation is the only one that won't overflow under any circumstances.
Bill the Lizard
If both values happen to be in registers, a simple xchg reg, reg works on the x86 platform. :-)
Brian Knoblauch
@Brian: Now that's trivial! :)
Bill the Lizard
+2  A: 

An important programming concept is to limit and control side effects.

State of the art in doing this right would have to include the language Haskell. State of the art in doing this wrong would have to include this ancient piece of trivia:

The old IBM mainframes had an assembler language instruction called EX for 'execute'. In one low level machine cycle, EX enabled the programmer to copy a piece of addressable memory from anywhere, perform a boolean operation on the bits, and then try to execute those bits. This makes COBOL's worst 'alter goto' instruction seem tame. As if 'goto' wasn't bad enough, 'alter goto' changes the 'goto' based on dynamic state.

dongilmore
I do not envy the programmer stuck maintaining code using those statements...
chills42
A: 

Your basic Int16 has a maximum range of 32,767 (positive or negative) for its value.

It got me some street cred with some friends watching Jeopardy once and actually helped us solve a problem due to an Id field or something to that effect was only using the Int16 and the records had far exceeded that.

Dillie-O
Not quite. If you're using two's complement arithmetic, Int16 has a maximum value of 32,767 and a minimum value of -32,768. This is because two's complement doesn't have separate bit patterns for +0 and -0, so you get one extra bit on the negative side. You should probably read this: http://en.wikipedia.org/wiki/Two%27s_complement
Daniel Pryden
+4  A: 

I found this in real code, it's very elegant:

unsigned char a[256];
a[0] = 0;
for (int i=1; i < 256; i++)
    a[i] = (a[i>>1]>>1) | ((i&1)<<7);

Took me quite a while to understand what it does, but after that dynamic programming came easily.

If you solve it put it in the comments :-)

orip
Builds a table of reverse_the_bits(b) for every possible byte b, right? That would be quite clear with a reasonable variable name. :-)
Darius Bacon
@Darius - yes it does, and yes it would :-)
orip
+1  A: 

I think the origin of the Oracle DUAL table is pretty trivial, and pretty interesting. See here: http://stackoverflow.com/questions/73751/what-is-the-dual-table-in-oracle and scan to the quote from Chuck Weiss.

SeaDrive
+3  A: 

I always found xor linked lists interesting.

DShook
Thanks, I'd never seen this. When I see stuff like this I want to play with it by implementing something using it then erase the code and object files and write over the erased space 5 times and never do it again.
Bill K
+2  A: 

while (*p++=*q++);

Svante
A: 
1<<10 =~ 1K
1<<20 =~ 1M
AShelly