How to execute a bitwise logical expression which is a string and the operator isn't known before hand

Refresh

February 2019

Views

27 time

0

I have to execute a logical expression which is a string.

For example :

string s = "2 << 1"

How could I execute above "s" with the bitwise operator unknown at the time of executing.

1 answers

0

Вы можете попробовать следующее:

string s = "2 << 1";
string operator_ = (new string(s.Where(c => !char.IsDigit(c)).ToArray())).Trim();
int operand1 = Convert.ToInt32(s.Substring(0, s.IndexOf(operator_)).Trim());
int operand2 = Convert.ToInt32(s.Substring(s.IndexOf(operator_) + operator_.Length).Trim());

int result = 0;
switch (operator_)
{
    case "<<":
         result = operand1 << operand2;
         break;
    case ">>":
         result = operand1 >> operand2;
         break;
}
Console.WriteLine(string.Format("{0} {1} {2} = {3}", operand1, operator_, operand2, result));