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?

Improve company productivity with a Business Account.Sign Up

x
 
Jaime OlivaresConnect With a Mentor Software 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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.