# Find the maximum of 3 numbers without branching

## Javascript

**6 tokens**

```
function maxOf3(a, b, c) {
(b>a) && (a=b);
(c>a) && (a=c);
return a;
}
```

## Javascript 10 tokens

*Edit* Using < and * instead of bit fiddling - as pointed out in comments, bits operations may fail for input near the range limit (over 30 bits)

```
function Max(x,y,z)
{
var d=y-x;
x=y-d*(d<0);
d=x-z;
return x-d*(d<0);
}
```

## C 8 tokens

Language agnostic in fact, any C like language will do. To be picky, in standard C it's not portable because right shift may not extend the sign (but in common implementations it does).

In C (and C++, C#, and Java I think) we can easily handle overflow problems using bigger temporary values:

```
int Max(int x, int y, int z)
{
long long X = x;
long long Y = y;
long long Z = z;
long long D = Y-X;
X=Y-((D>>63)&D);
D=X-Z;
return (int) (X-((D>>63)&D));
}
```

### C: 10 tokens

```
int max(int a, int b, int c)
{
a += (b > a) * (b - a);
a += (c > a) * (c - a);
return a;
}
```

Inspired by @openorclose's answer, but converted to C and made branchless using multiplication rather than short circuit boolean operators.