Anyone who is moderately into low level code optimization know about the perils of branching, be it implemented as if-statements, loops or select-statements the possibility of a branch misprediction is a terrible clock-wasting thing.
Simple problems can be solved much better with simple arithmetic, so let's do that.
For the following problems all variables are 32 bit unsigned integers and the only allowed code is plain set statements involving only the following operators:
+ addition
- subtraction
* multiplication
/ integer division, rounds down, division by 0 not allowed
% modulo
& binary and
| binary or
^ binary exclusive or
>> bitshift right
<< bitshift left
Logic operators, return 1 if the expression is true and 0 if it is false.
== equal
!= not equal
< less than
<= less than or equal
> greater than
>= greater than or equal
Set operator
=
Every line must consist of a variable identifier followed by a set operator, followed by an expression.
An expression may not contain additional set operators, but may contain variable identifiers, literal numbers and parentheses.
The golfing score shall only count the number of operators.
Example:
myvar = ( ( ( foo + 5 ) * bar ) % 7 ) == 3
Has a score of 5 operators.
A solution may include as many variables as the author see fit.
Variables that have not been set have value 0
.
Overflow and underflow is allowed, all negative numbers underflow, so 3 - 5
is 4294967294
, even as part of a larger statement.
Task 1: Max
###Task 1: Max
TwoTwo values, A
and B
, exist in the scope, make the RESULT
variable contain the largest of those values when the program terminates.
Task 2: Median
###Task 2: Median
ThreeThree values, A
, B
and C
, exist in the scope, make the RESULT
variable contain the median of those values when the program terminates.
Task 3: Square root
###Task 3: Square root
OneOne value, A
, exist in the scope, make the RESULT
variable contain the square root of A
, rounded down, when the program terminates.
It is ok to post an answer to only one or two of the questions, to some of you just finding valid solutions will be a challenge.