Search code examples
c#c++bit-shiftulong

Shift ulong from c++ to c#


I am converting application from C to C# now I met error in convert a shift with ulong value.

The original code in c++ is:

unsigned long long dat = (d >> dat_shift) & (0xffffffffffffffff >> (64 - data_bits_first_frame));

In C#, i think, it should be something like this:

long d = 1158552107947655168;
int dat_shift  = 8;
int data_bits_first_frame = 0;
ulong dat = (d >> dat_shift) & (0xffffffffffffffff >> (64 - data_bits_first_frame));

In runtime I get the error CS0019 Operator '&' cannot be applied to operands of type 'ulong' and 'ulong'.

The reason is 0xffffffffffffffff. Can you please help me to understand and solve the problem?

Thank you very much.


Solution

  • The error is:

    error CS0019: Operator '&' cannot be applied to operands of type 'long' and 'ulong'.

    not ulong and ulong

    To make it work d should be of type ulong:

    ulong d = 1158552107947655168;