1

This SQL statement below, when used is returned multiple rows.

Now, how exactly can I do use bitwise calculation in C# instead of the database? I'm working on building C# version and not have to rely on using the database too much.

How exactly does C# bitwise operator works?

SELECT CustomerMsg 
    CustomerMsg 
FROM BookItOut_DataReloading_DealerSalesErrorCode 
WHERE ((BitwiseErrorCode & '17314086912') = BitwiseErrorCode) 
0

2 Answers 2

1

In C# bitwise operators work the same way they work in SQL. With LINQ you can do something like this:

// note that 17314086912 does not fit to int
BookItOut_DataReloading_DealerSalesErrorCode
   .Where(x => (x.BitwiseErrorCode & 17314086912) == x.BitwiseErrorCode)
   .Select(x => x.CustomerMsg);

From & Operator (C# Reference):

Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.

Example:

int a = 3;
int b = 6;
int c = a & b; // c will be set to 2
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it with the same operators in Integers types.

var mask = 2 << 3;
var value = 16;
Console.WriteLine((value & mask) == mask); //true

Bitwise Operators can be used with Enums type marked with FlagsAttribute

[Flags]
enum MyEnum
{
    A = 1 << 0,   //1
    B = 1 << 1,   //2
    C = 1 << 2,   //4
    D = 1 << 3,   //8
    E = A | B | C //7
}

See the MSDN Docs

1 Comment

note that in Console.WriteLine(value & mask == mask) operator == will precede &.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.