Solved

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

Posted on 2008-06-11
8
437 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
ID: 21766553
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
ID: 21766972
ok i will try this first...
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21775577
you can achieve similar efect by casting the variable to unsigned and using the >> operator.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21775621
by example:
unsigned int result = ((unsigned int)yourVariable) >> yourShift;

0
 

Author Comment

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

Accepted Solution

by:
Jaime Olivares earned 50 total points
ID: 21776063
it is similar to:

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

895 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

21 Experts available now in Live!

Get 1:1 Help Now