F
Francois Grieu
Quiz:
Find (one of) the shortest C89/C90 macro, behaving as a pure function,
which, given x = 1<<b with b in [0..7], returns b.
F(1) evaluates to 0
F(2) evaluates to 1
F(4) evaluates to 2
F(8) evaluates to 3
F(16) evaluates to 4
F(32 evaluates to 5
F(64) evaluates to 6
F(128) evaluates to 7
The macro must not depend on headers (e.g. <math.h>) or code/constants
outside the macro.
So far my best attempt is 39 chars long, including
17 for what (I think is) obligatory:
#define F(x)((x))
I guess it can be beaten.
Francois Grieu
Caution: spoiler follows
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define F(x)((0xE4FD71B>>(x)%11*3)+5&7)
Find (one of) the shortest C89/C90 macro, behaving as a pure function,
which, given x = 1<<b with b in [0..7], returns b.
F(1) evaluates to 0
F(2) evaluates to 1
F(4) evaluates to 2
F(8) evaluates to 3
F(16) evaluates to 4
F(32 evaluates to 5
F(64) evaluates to 6
F(128) evaluates to 7
The macro must not depend on headers (e.g. <math.h>) or code/constants
outside the macro.
So far my best attempt is 39 chars long, including
17 for what (I think is) obligatory:
#define F(x)((x))
I guess it can be beaten.
Francois Grieu
Caution: spoiler follows
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define F(x)((0xE4FD71B>>(x)%11*3)+5&7)