How do I convert the Java bitwise shift operator '>>>' to C Sharp?

Java has shift operators >>, << and  >>> but C-sharp only has >> and <<.
How would I translate >>> in Java to C-sharp?
ludyAsked:
Who is Participating?
 
Jaime OlivaresSoftware ArchitectCommented:
it is similar to:

n = ((unsigned int)n) >> 1;
0
 
jml948Commented:
The details behind the ">>>" operator seem to be explained as follows:

n >>> s

if n is positive then its the same as n >> s

but if n is negative there are two cases:
   if n is an int and negative then:
       n >>> s = (n>>s)+(2<<~s)
   if n is a long and negative then:
       n >>> s = (n>>s)+(2L<<~s)

So I would try the following:

    static int UnsignedRightShift(int n, int s) //n is an integer
    {
          if(n > 0){
               return n>>s;
          }else{
               return (n>>s)+(2<<~s);
          }
    }
    static long UnsignedRightShift(long n, int s)  //Overloaded function where n is a long
    {
          if(n > 0){
               return n>>s;
          }else{
               return (n>>s)+(((long)2)<<~s);
          }
    }

0
 
ludyAuthor Commented:
ok i will try this first...
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Jaime OlivaresSoftware ArchitectCommented:
you can achieve similar efect by casting the variable to unsigned and using the >> operator.
0
 
Jaime OlivaresSoftware ArchitectCommented:
by example:
unsigned int result = ((unsigned int)yourVariable) >> yourShift;

0
 
ludyAuthor Commented:
how about n >>>= 1? it's also used in java.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.