1

I have a DataColumn implementation to evaluate expressions at runtime. For my surprise, DataColumn operations seens not to support bitwise operators.

In need to check for individual bits set or groups of bits set. Examples:

(Value & 0x0002) to check for the 2nd bit set. Shall return true (1).

((Value & 0x000F) == 0x03) to check if the first 4 bits of Value has a value of 3.

Is there any way to support these bitwise operations using DataColumn class ?

Is there an alternative to DataColumn that does support bitwise operations ?

Suggestions on how to do it using common math operators ?

[EDIT - Complete function code]

public static bool EvaluateExpression(string expression, object value, out object returnValue, out string errorMessge)
{
    try
    {
        errorMessge = String.Empty;

        ///
        /// First substitute the references of object with its value
        /// 
        string evalExpression = expression.Replace("[this]", Convert.ToString(value));

        ///
        /// Now evaluate the expression
        /// 
        DataTable loDataTable = new DataTable();

        double dummyResult;
        DataColumn loDataColumn;

        if (Double.TryParse(evalExpression, out dummyResult))
            loDataColumn = new DataColumn("Eval", typeof(double), evalExpression);
        else
            loDataColumn = new DataColumn("Eval", typeof(string), evalExpression);

        loDataTable.Columns.Add(loDataColumn);
        loDataTable.Rows.Add(0);

        returnValue = (object) loDataTable.Rows[0]["Eval"];

        return true;
    }
    catch (Exception e)
    {
        errorMessge = e.Message.ToString();
        returnValue = 0;
        return false;
    }
}

Calling the function in a example...

.
.
.
.
object val = (object) 12538;

string errorMsg = String.Empty;

object result;

string expr = "[this] & 0x02";

if (!EvaluateExpression (expr, val, result, errorMsg))
{
    Console.WriteLine("Error on expression. Error = " + errorMsg);
}
else
{
    Console.WriteLine("The resulting valur is " + val.ToString();
}
Mendes
  • 17,489
  • 35
  • 150
  • 263
  • 3
    Can we see some more code? – geedubb Jan 03 '14 at 13:46
  • How do you set `Value`? What is `Value`? – Mike Perrenoud Jan 03 '14 at 13:49
  • Posted code. The function works fine for some time for all common math operations, except for bitwise operators that I need to support now. [this] is changed to given value. Value was just to illustrate some value. – Mendes Jan 03 '14 at 13:55
  • Come on, the posted code does not even attempt a bitwise op. – H H Jan 03 '14 at 14:00
  • Sorry for your incomprehension. The bitwise operation is ***built at runtime***. Like: EvaluateExpression ("[this] & 0x03", (object) 12, our result, out error)... That´s why the original question does not contain code... – Mendes Jan 03 '14 at 14:05

2 Answers2

2

http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression%28v=vs.110%29.aspx These expressions can't handle bitwise ops afaik.

sdf
  • 189
  • 3
  • That´s what I found out now. So, I need to change it either using math operators or a different method. Any suggestion ? – Mendes Jan 03 '14 at 14:11
  • Why are you using a DataColumn class for this anyway? Maybe you should try something like this:http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments – sdf Jan 03 '14 at 14:26
  • The reason is that is a very simple code with not extra libraries. C# parser is much more complex and I really need to support only simple operations, not a language parser. We did that some time ago and it´s working very fine. The problem is that now I need to support bitwise and it does not support... I need a easy way to go... – Mendes Jan 03 '14 at 14:46
1

As per @sdf, they do not support bitwise operations, but some bitwise operations can be simulated with the operators available. For example:

  • [this] & 0x02 is equivalent to IIF(Convert([this]/2, 'System.Int32')%2 = 0, 0, 2)
  • [this] | 0x02 is equivalent to IIF(Convert([this]/2, 'System.Int32')%2 = 0, [this] + 2, [this])
  • [this] >> 2 is equivalent to Convert([this]/4, 'System.Int32')
  • [this] << 2 is equivalent to Convert([this]*4, 'System.Int32')
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • +This but OP would have to parse the string before stuffing it in the DataColumn and get which operation he's gonna use and substitute these. At the point where he parsed the string to get the operation symbol it would be probably simple to get the args as well as pass this to a native code function that does this. Maybe this is what he shuold've been doing from the start? – sdf Jan 03 '14 at 15:05
  • Yes, unless, of course, the original string can be modified. He didn't say where it is coming from... – Dark Falcon Jan 03 '14 at 15:16
  • Acceptable to solve my problem now, but I forsee I need to design a new parser that supports bitwise. I´m checking posts like [link](http://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net/392355#392355) and [link](http://www.codeproject.com/Articles/18880/State-of-the-Art-Expression-Evaluation). The last one seens complete and customizable, but will take some time. About the values, the expression is byuser configurable, and the value comes from DB data. I don´t know about it previously as each user will have diff expressions, running over realtime database data... – Mendes Jan 03 '14 at 15:24
  • Not working. When we do [this]/2 it turns it into double, that can´t be moduled to 2. So it raises an exception... – Mendes Jan 03 '14 at 15:43
  • Have you tried conversions, as shown above? A parser is not that hard and definitely the correct solution, however. – Dark Falcon Jan 03 '14 at 17:26
  • I took a javascript parser that has a clean syntax from [link](http://stackoverflow.com/questions/1437964/best-and-shortest-way-to-evaluate-mathematical-expressions). It uses an old VBA syntax that I´m trying to convert. I will also have to check/convert all by db expressions for the new format - but looks like will accept same syntax. It accepts bitwise operations! I´m happy with javascript syntax. Thanks all for the help. – Mendes Jan 03 '14 at 17:41