Let a function receive two integers i and j between 0 and 3, it should return the value in position (i,j) in the following 2D array:
1, 0, -1, 0 1, 0, -1, 0 0, 1, 0, -1 0, -1, 0, 1 Shortest code in any variant of C / C++ wins.
f(i,j){return~-(2434352710>>8*i+2*j&3);} A hackish 35 solution (xxd dump):
0000000: 6628 692c 6a29 7b72 6574 7572 6e7e 2d28 f(i,j){return~-( 0000010: 695b 2246 4619 9122 5d3e 3e32 2a6a 2633 i["FF.."]>>2*j&3 0000020: 293b 7d );} If proper octal escapes are used, it's the same 40 characters:
f(i,j){return~-(i["FF\31\221"]>>2*j&3);} f(int i,int j) as f(i,j) (the arguments are int by default) \$\endgroup\$ f(i,j){return(i<3?1-j+i/2:j-2)%2;} f(i,j){return~-(j+2-i*i/3&3)%2;} f(i,j){return-~-(i*i/3^j)%2;} is 29. I found it by running an exhaustive(ish) search. I can't post it because the question is closed, so I'll put it here. \$\endgroup\$ I know this one seems kinda unfair but does it actually work for anyone? ^_^
f(i,j){j=("&&4+"[i]>>j)%3-1;} f(i,j){return(i/2+j+1)*(1-(i-j&2))%2;} f(i,j){return(i/2+j+1)*-~-(i-j&2)%2;} (1-(i-j&2)) to ~-(j&2-i) \$\endgroup\$ -~-(i-j&2) \$\endgroup\$ f(i,j){return(j/2&i^~i&~j>>1)*~-(j-i&2)%2;} The Karnaugh map approach, mixed with a bit of fsw's solution.
If A is (j>>1) and D is (i&1), the function should return "-~-(j-i&2)%2" (fsw's solution) multiplied by the Karnaugh map ~A~D + AD.
I feel there is a better solution for this using the Karnaugh map, but it's too late to think of it. Maybe tomorrow :)
Thanks for the comments, shaved off many chars.
((~(j>>1)&~(i&1)^j>>1&(i&1))&1) also you can move the &1 into inside the left side of ^ : (~(j>>1)&~(i&1)&1^j>>1&(i&1)) \$\endgroup\$ int f(int i,int j){return "2101210112101012"[i*4+j]-49;} return. But it's arguable whether C# can be considered a variant of C/C++, despite the name. It's derived from C, but so is Java. \$\endgroup\$
returnkeyword ;) \$\endgroup\$