Solved

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

Posted on 2008-06-11
8
436 Views
Last Modified: 2013-12-17
Java has shift operators >>, << and  >>> but C-sharp only has >> and <<.
How would I translate >>> in Java to C-sharp?
0
Comment
Question by:ludy
  • 3
  • 2
8 Comments
 
LVL 2

Expert Comment

by:jml948
Comment Utility
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
 

Author Comment

by:ludy
Comment Utility
ok i will try this first...
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
you can achieve similar efect by casting the variable to unsigned and using the >> operator.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
by example:
unsigned int result = ((unsigned int)yourVariable) >> yourShift;

0
 

Author Comment

by:ludy
Comment Utility
how about n >>>= 1? it's also used in java.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 50 total points
Comment Utility
it is similar to:

n = ((unsigned int)n) >> 1;
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now