No boolean<->int typecasting?

Someone please tell me that I don't have to make my own bool2int and int2bool functions to be able to multiply a value by a bool so that the bool is treated as a number, or compare two numbers which will always be either 0 or 1 using bool operators... If that's really the case, what's the "fastest" way of doing this? I can create a script called "AdditionalMathFunctions" or some name that's not so long, let's say "M" (although I wouldn't be happy with such weird naming just for the purpose of simplifying code), but then I have to call M.int2bool(X) instead of just X which would suffice in the first place in, say, C++. That's 12 characters more that I have to type to get what I want and it makes code much harder to read. Is there no better way? Can I perhaps overload operators somehow?

You can also use the "ternary operator" (also known by Microsoft as the "conditional operator"). This is a common way to do this sort of operation which has boolean dependency inline, without an extra function call.

It takes the form:

condition ? value if true : value if false

Eg, if x is a boolean, this will multiply b by 1 if x is true, or by zero if x is false:

var a = b * ( x ? 1 : 0);

This works in both C# and Unity's Javascript.

For more information, see the Wikipedia article and the MSDN documentation.

If you write your own, this is probably the absolute fastest way. It may be more convenient not to do the ref version, or implement two... Of course, you can shorten the names for convenience sake. Typically the ref versions get marginally better performance, if you care.

EDIT I just realized you wanted javascript. Anyway, this class and function will still work in either language.

public class MathLib{
    //ref version
    public static void BoolMultiply(ref bool boolean, ref int val){
        if(boolean == false){
    //non ref version
    public static int BoolMultiply(bool boolean, int val){
        if(boolean == false){
            return 0;
            return val;

and Example usage:

void Start () {
    int xyz = 3;
    bool theBool = false;
    MathLib.BoolMultiply(ref theBool, ref xyz);