Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
Why limit this to C/C++? – Veronica Deane Jan 14 '14 at 1:52
probably because the author realizes it is 3-4 chars in golfscript, and wants to make the competition more fair – mniip Jan 14 '14 at 13:32
@Kendall Frey - Actually the true code golf contests are possible only when they target single language (or family of languages). Because there is no point to compete when someone is running on foot, someone is swimming, and others use helicopter. – SergeyS Jan 15 '14 at 7:59
@mniip - I do not think this is 3 or 4 characters. I think it is about best C/C++ solution minus length of return keyword ;) – SergeyS Jan 15 '14 at 8:10
@SergeyS Even if I agreed, the rest of the site does not. Language-specific questions are discouraged here. – Veronica Deane Jan 15 '14 at 12:50
up vote 7 down vote accepted


share|improve this answer
A winner by three chars :) – nbubis Jan 15 '14 at 6:55



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:

share|improve this answer
You can save 8 bytes by writing f(int i,int j) as f(i,j) (the arguments are int by default) – squeamish ossifrage Jan 14 '14 at 2:40
I see, i actually tried removing those ints, but somehow failed – mniip Jan 14 '14 at 2:41

C99, 34

share|improve this answer
Also works in C89. And function text is 34 chars – AMK Jan 14 '14 at 19:48
I marked it C99 since C99 actually specifies the behavior of '%', whereas prior versions left it implementation-defined. And yeah, I was counting the newline. I'll fix that. – Austin Hastings Jan 14 '14 at 19:54




share|improve this answer
You can save 2 chars if you change (1-(i-j&2)) to ~-(j&2-i) – mniip Jan 14 '14 at 13:29
Tried your suggestion and it gives wrong results.(i-j&2) is actually ((i-j)&2) so your trick changes the logic here. or am i missing something? – fsw Jan 14 '14 at 13:40
oh, I kind of overlooked that, still can save 1 by replacing with -~-(i-j&2) – mniip Jan 14 '14 at 13:45
@mniip oh yes now it works, thx – fsw Jan 14 '14 at 13:49

C++, 70, 45, 44


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.

share|improve this answer
you don't need most of the parentheses here: ((~(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)) – mniip Jan 15 '14 at 0:13


int f(int i,int j){return "2101210112101012"[i*4+j]-49;}
share|improve this answer
4 spaces will make that code highlighted... – Kyle Kanos Jan 14 '14 at 2:11
You can save one character by removing the space after 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. – Bob Jan 14 '14 at 5:08


f(a,b){return 10308>>4*a+b&1?-1:43605>>4*a+b&1;}
share|improve this answer

C, 45

f(i,j){i=i+(j>1)*(7-2*j)&3;return i&1?0:1-i;}
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.