Reputation: 85
I have a bitwise operations in VBA and now I want to convert that to C#.
I tried converting using the C# bitwise operators but I didn't get the result.
VBA Bitwise Operations
Bit1 = (Var1 And 2 ^ 14) Or (Var1 And 2 ^ 15)
C# Bitwise Operations
var Bit1 = (bitvalue >> 14) | (bitvalue >> 15);
Since VBA doesn't have bit shift operators we use 2^places for right shift and in C# we can simply use >> for right shift operator, but it's not giving me the result which I am getting it in VBA. What could be the reason?
This is my full C# function and it's similar in VBA:
public void bitcalc()
{
var Bit1 = (bitvalue >> 14) | (bitvalue >> 15);
if (Bit1 == 16384) getISO15031BitsLetter = "C";
else if (Bit1 == 32768) getISO15031BitsLetter = "B";
else if (Bit1 == 49152) getISO15031BitsLetter = "U";
else getISO15031BitsLetter = "P" ;
}
Upvotes: 0
Views: 154
Reputation: 32627
Your original code does not shift the input. It only masks it. A direct translation could be:
var Bit1 = (Var1 & (1 << 14)) | (Var1 & (1 << 15));
There are multiple variations to this. E.g., you could mask both bits at the same time:
var Bit1 = (Var1 & (3 << 14)); // decimal 3 is binary 11
Upvotes: 2