Shortest C \ C++ function to generate -1, 0 ,1

6

3

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.

nbubis

Posted 2014-01-14T00:48:52.870

Reputation: 1 019

Question was closed 2018-01-08T09:47:45.173

16Why limit this to C/C++? – Kendall Frey – 2014-01-14T01:52:30.967

4probably because the author realizes it is 3-4 chars in golfscript, and wants to make the competition more fair – mniip – 2014-01-14T13:32:25.180

3@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 – 2014-01-15T07:59:22.080

2@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 – 2014-01-15T08:10:31.150

1@SergeyS Even if I agreed, the rest of the site does not. Language-specific questions are discouraged here. – Kendall Frey – 2014-01-15T12:50:49.053

By the way, do you had in mind some truth-table approach when proposing the question? – Gabriele D'Antona – 2014-01-15T13:11:11.567

How is this unclear? – Stan Strum – 2018-01-10T17:17:19.580

Answers

7

32

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

SergeyS

Posted 2014-01-14T00:48:52.870

Reputation: 290

A winner by three chars :) – nbubis – 2014-01-15T06:55:43.487

10

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

mniip

Posted 2014-01-14T00:48:52.870

Reputation: 9 396

You can save 8 bytes by writing f(int i,int j) as f(i,j) (the arguments are int by default) – r3mainer – 2014-01-14T02:40:22.257

I see, i actually tried removing those ints, but somehow failed – mniip – 2014-01-14T02:41:59.813

10

C99, 34

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

Austin Hastings

Posted 2014-01-14T00:48:52.870

Reputation: 258

1Also works in C89. And function text is 34 chars – AMK – 2014-01-14T19:48:35.323

2I 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 – 2014-01-14T19:54:10.687

5

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

user77369

Posted 2014-01-14T00:48:52.870

Reputation:

2

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 – 2018-01-07T20:56:25.237

4

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

fsw

Posted 2014-01-14T00:48:52.870

Reputation: 141

You can save 2 chars if you change (1-(i-j&2)) to ~-(j&2-i) – mniip – 2014-01-14T13:29:04.870

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 – 2014-01-14T13:40:52.910

oh, I kind of overlooked that, still can save 1 by replacing with -~-(i-j&2) – mniip – 2014-01-14T13:45:07.310

@mniip oh yes now it works, thx – fsw – 2014-01-14T13:49:34.673

3

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.

Gabriele D'Antona

Posted 2014-01-14T00:48:52.870

Reputation: 1 336

1you 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 – 2014-01-15T00:13:53.380

2

C#

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

thepirat000

Posted 2014-01-14T00:48:52.870

Reputation: 588

34 spaces will make that code highlighted... – Kyle Kanos – 2014-01-14T02:11:33.223

2You 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 – 2014-01-14T05:08:16.423

2

48

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

izabera

Posted 2014-01-14T00:48:52.870

Reputation: 879

1

C, 45

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

ValarDohaeris

Posted 2014-01-14T00:48:52.870

Reputation: 231