Minus, Plus, Times, Exponentiation?

This is a CMC (chat mini challenge) that I posted in our chatroom, The Ninteenth Byte, a bit ago.

The Challenge

Given a positive integer x, depending on the last 2 bits of x, do the following:

x & 3 == 0: 0
x & 3 == 1: x + x
x & 3 == 2: x * x
x & 3 == 3: x ^ x (exponentiation)

Input/Output

Single Integer -> Single Integer

A trailing newline is permitted in the output. No other whitespace is permitted.

Testcases

input       output
1            2
2            4
3           27
4            0
5           10
6           36
7       823543
8            0
9           18
10          100
11 285311670611
12            0

This is a challenge, so the shortest code wins!

• Shouldn't the 0 case be x + 2, seeing as how the others are x * 2, x ^ 2, and x ^^ 2 (tetration)? :P – ETHproductions May 1 '17 at 13:47
• What is the largest output we should support (regarding x ^ x)? 32-bit is already not enough for test case 11, and 64-bit is not enough for test case 19. – Kevin Cruijssen May 1 '17 at 14:23
• @KevinCruijssen I will say that you only have to handle cases where the input and output are in the scope of your language, as long as your program would theoretically work given infinite number size. – HyperNeutrino May 1 '17 at 14:28
• @HyperNeutrino Ok, in that case I've fixed my (Java 7) code (for +72 bytes.. xD) – Kevin Cruijssen May 1 '17 at 14:42

vba, 101

Function x(f)
Select Case f Mod 4
Case 0:x=0
Case 1:x=f+f
Case 2:x=f*f
Case 3:x=f ^f
End Select
End Function

Dyvil, 24 Bytes

x=>[0,x+x,x*x,x**x][x&3]

Usage:

let f: int->int = x=>[0,x+x,x*x,x**x][x&3]
print f(10) // => 100

Explanation:

The code creates a lambda expression that takes and returns a 32-bit integer. The body stores the result of all calculations in an array and retrieves the result from the index given by the last 2 bits of x.

TI-BASIC, 27, 23 bytes

:Prompt X          //Get input as X, 3 bytes
:{2X,X²,X^X,0      //Implicitly store the list {2X,X²,X^X,0} to Ans, 16 bytes
:Ans(remainder(X,4 //Compute X&3 (as X%4), get that element of the list, and implicitly print 8 bytes

TI-BASIC doesn't have bitwise operators, so I used the modulus operator (remainder() instead.

Swift, 10568 67 bytes

Here is the body of the function:

i in let a=i%4;return [0,i+i,i*i,Int(pow(Double(i),Double(i)))][a]

Here is the full implementation:

import Foundation;var e:(Int)->Int={i in let a=i%4;return [0,i+i,i*i,Int(pow(Double(i),Double(i)))][a]}

Try it here

oK, 20 bytes

{*/(."-+*#"4!x).x,x}

Try it online!

Pure Bash, 35

a=(- + * **)
echo $[$1${a[$1%4]}\$1]

Axiom, 59 33 bytes

h(x)==[0,x+x,x*x,x^x].(1+x rem 4)

traslate in Axiom the answer of Mego... this is the old 59 bytes

h(x)==(x quo 2 rem 2=1=>(x rem 2=0=>x^2;x^x);(x rem 2)*2*x)

results

(24) -> for i in 0..30 repeat output [i,h(i)]
[0,0]
[1,2]
[2,4]
[3,27]
[4,0]
[5,10]
[6,36]
[7,823543]
[8,0]
[9,18]
[10,100]
[11,285311670611]
[12,0]
[13,26]
[14,196]
[15,437893890380859375]
[16,0]
[17,34]
[18,324]
[19,1978419655660313589123979]
[20,0]
[21,42]
[22,484]
[23,20880467999847912034355032910567]
[24,0]
[25,50]
[26,676]
[27,443426488243037769948249630619149892803]
[28,0]

C (gcc), 59 bytes

#include<math.h>
h(x){return x&2?pow(x,x&1?x:2):(x&1)*2*x;}

Test and results. It overflows at 11.

main()
{int i;
for(i=0;i<20;++i)
printf("%u %u\n", i, h(i));
}

0 0
1 2
2 4
3 27
4 0
5 10
6 36
7 823543
8 0
9 18
10 100
11 1843829075
12 0
13 26
14 196
15 1500973039
16 0
17 34
18 324
19 0

Ideone

Try it online!

• Why someone downvote this? – RosLuP May 4 '17 at 14:15
• does not compile.int main(); int h(int x) please! – user69099 May 9 '17 at 21:54
• @xakepp35 here ideone.com/4eI270 it compile; if it is for arguments of function, in a old standard they are seen from compiler all as int... – RosLuP May 9 '17 at 22:03
• In "tio" gcc compier not compile even this: #include <math.h> #include <stdlib.h> double h(int x){return x&2?pow((double)x,(double)(x&1?x:2)):(double)((x&1)*2*x);}; it says the linker not find the pow function (double pow(double, double) in math.h header) – RosLuP May 9 '17 at 22:35
• The trick it is to add "-lm" [link math functions] to the line compiler is called – RosLuP May 10 '17 at 7:44