4

I am not familiar with bitwise operator. I have these code:

var value= -2145643504;
value = (value << 1) | (value >> 27);
//result: -16

Both C# and JavaScript result the same -16 But in JavaScript there is another operator >>> which C# has not. Code in JavaScript:

 var value= -2145643504;
 value = (value << 1) | (value >>> 27);
 //result: 3680304    //wanted result in C#

Any solutions to get it in C#?

2

2 Answers 2

4

In JavaScript you are doing a Unsigned right shift assignment with >>>.


To duplicate this in C# you will need to use >> but you must first cast the int.

int x = -100;
int y = (int)((uint)x >> 2);
Console.WriteLine(y);
Sign up to request clarification or add additional context in comments.

1 Comment

I'm converting kindof hashing mechanism from JavaScript to C# with zero experience of C# and this helped me so damn much! Thank you!
1
            var value = -2145643504;
            value = (value << 1) | rightMove(value , 27);
            //value = 3680304

        int rightMove(int value, int pos)
        {
            if (pos != 0)
            {
                int mask = 0x7fffffff;
                value >>= 1;
                value &= mask;
                value >>= pos - 1;
            }
            return value;
        }

Comments

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.