Click here to Skip to main content
Click here to Skip to main content

Tagged as

Logical and arithmetic operations in C#

, 26 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Some logical and arithmetic operations in C#

There are many uses of logical and arithmetic operators in C#. And also the usage of these operators are different
depending on the situation. For example, here is a simple use of the && operator:

bool booleanResult = MethodReturnBooleanOne() && MethodReturnBooleanTwo() && MethodReturnBooleanThree();

and a simple use of the + operator:

int result = MethodIntReturn1() + MethodIntReturn2() + MethodIntReturn3() + MethodIntReturn4();

We could delegate this logical operation( && or ||) on a specific method and let the method
do the operation and pass back the result to the caller. Also, it is possible to do the same for an arithmetic operation, for example,
3+5+5+5 could be done by just calling a method and passing 3,5,5,5 to that method, and the method will do the job and return back
the result to the caller. To describe the whole point, I created a few methods; for example:

DoSequenceOfAnd(params bool[] sequenceOfBooleanValue)

This method will take the values of boolean and will do an && (AND) operation on those values and return the result to the caller.

DoSequenceOfOr(params bool[] sequenceOfBooleanValue)

This method will take the values of boolean and will do an || (OR) operation on those values and return the result to the caller.

DoSequenceOfXOr(params bool[] sequenceOfBooleanValue)

This method will take the values of boolean and do an ^ (XOR) operation on those values and return the result to the caller.

DoSequenceOfPlus<T>(params T[] sequenceOfTValues)

This generic method will accept a sequence of values (in here, T should only define numeric types from the value types)
and will do an addition operation and return the result.

DoSequenceOfMultiply<T>(params T[] sequenceOfTValues)

This generic method will accept a sequence of values (in here, T should only define numeric types from the value types)
and will do a multiplication operation and return the result.

Here is the code for the above methods:

public static class Operation
{
    public static bool DoSequenceOfAnd(params bool[] sequenceOfBooleanValue)
    {
        bool andResult = true;
        Array.ForEach(sequenceOfBooleanValue,
            (item) =>
            {
                andResult &= item;
            });
        return andResult;
    }
    public static bool DoSequenceOfOr(params bool[] sequenceOfBooleanValue)
    {
        bool orredResult = default(bool);
        Array.ForEach(sequenceOfBooleanValue,
            (item) =>
            {
                orredResult |= item;
            });
        return orredResult;
    }
    public static bool DoSequenceOfXOr(params bool[] sequenceOfBooleanValue)
    {
        bool xoredResult = default(bool);
        Array.ForEach(sequenceOfBooleanValue,
            (item) =>
            {
                xoredResult ^= item;
            });
        return xoredResult;
    }
    public static T DoSequenceOfPlus<T>(params T[] sequenceOfTValues)
        where T : struct
    {
        T additionResult = default(T);
        Func<T, T, T> adder = ExpressionGenerator.AdditionExpression<T>();
        Array.ForEach(sequenceOfBooleanValue,
            (item) =>
            {
                additionResult = adder(item, additionResult);
            });
        return additionResult;
    }
    public static T DoSequenceOfMultiply<T>(params T[] sequenceOfTValues)
        where T : struct
    {
        dynamic multiplicationResult = (DoSequenceOfPlus<int>(default(int), 1));
        Func<T, T, T> multiplier = ExpressionGenerator.MultiplicationExpression<T>();
        Array.ForEach(sequenceOfBooleanValue,
            (item) =>
            {
                multiplicationResult = multiplier(item, multiplicationResult);
            });
        return multiplicationResult;
    }
}

Here is the usage of the above methods:

static void Main(string[] args)
{
    bool booleanResult = MethodOne() && MethodTwo() && MethodThree();
    bool resultAnd = Operation.DoSequenceOfAnd(MethodOne(), MethodTwo(), MethodThree());
    bool resultOr = Operation.DoSequenceOfOr(MethodOne(), MethodTwo(), MethodThree());
    bool resultXOr = Operation.DoSequenceOfXOr(MethodOne(), MethodTwo(), MethodThree());
    int resultOfIntAddition = Operation.DoSequenceOfPlus<int>(3, 3, 5, 5);
    int resultOfIntMultiplication = Operation.DoSequenceOfMultiply<int>(3, 3, 3, 1);
    long resultOfLongMultiplication = Operation.DoSequenceOfMultiply<long>(1, 3, 1, 1);
}

and related code used by the DoSequenceOfPlus and DoSequenceOfMultiply methods:

public static class ExpressionGenerator
{
    public static Func<T, T, T> AdditionExpression<T>()
    {
        ParameterExpression
           parameterA = Expression.Parameter(typeof(T), "a"),
           parameterB = Expression.Parameter(typeof(T), "b");
        BinaryExpression body = Expression.Add(parameterA, parameterB);
        return Expression.Lambda<Func<T, T, T>>(body, parameterA, parameterB).Compile();
    }
    public static Func<T, T, T> MultiplicationExpression<T>()
    {
        ParameterExpression
            parameterA = Expression.Parameter(typeof(T), "a"),
            parameterB = Expression.Parameter(typeof(T), "b");
        BinaryExpression body = Expression.Multiply(parameterA, parameterB);
        return Expression.Lambda<Func<T, T, T>>(body, parameterA, parameterB).Compile();
    }
}

To browse or download the source code, please visit here[^].

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
GeneralYou have worked hard for this, showed good examples but I ag... PinmemberAll Time Programming4-Jul-11 21:41 
GeneralAnother problem with your solution is that every time a new ... PinmemberPhilippe Mori28-Jun-11 16:44 
GeneralReason for my vote of 4 The question in itself is not so bad... PinmemberPhilippe Mori28-Jun-11 16:32 
GeneralIt would be interesting to see the performance comparison of... PinmemberPhilippe Mori28-Jun-11 16:07 
GeneralTried to separate the logical operation code into a dedicate... PinmemberMohammad A Rahman28-Jun-11 3:08 
GeneralI don't think I understand what the problem is that you are ... Pinmembertallies28-Jun-11 2:36 
GeneralThis might be good for academic purpose but not of any pract... PinmemberRakeshMeena26-Jun-11 20:22 
GeneralReason for my vote of 1 This seems like a pretty bad way of ... PinmemberEdMan19626-Jun-11 1:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 26 Jun 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid