# Shortest C \ C++ function to generate -1, 0 ,1 [closed]

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.

• Why limit this to C/C++? – Kendall Frey 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. – Kendall Frey Jan 15 '14 at 12:50

## 32

f(i,j){return~-(j+2-i*i/3&3)%2;}

• A winner by three chars :) – nbubis Jan 15 '14 at 6:55

# 40

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);}

• 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

f(i,j){return(i<3?1-j+i/2:j-2)%2;}

• 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

# C, 29

I know this one seems kinda unfair but does it actually work for anyone? ^_^

f(i,j){j=("&&4+"[i]>>j)%3-1;}

• Welcome to the site! You should add your byte count to the post as this is a code golf question. Also, you may want to add a link to an interpreter so that others can test your solution, such as Try It Online! – caird coinheringaahing Jan 7 '18 at 20:56

# 38

f(i,j){return(i/2+j+1)*(1-(i-j&2))%2;}


# 37

f(i,j){return(i/2+j+1)*-~-(i-j&2)%2;}

• 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

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.

• 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

# C#

int f(int i,int j){return "2101210112101012"[i*4+j]-49;}

• 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

# 48

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


## C, 45

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