bitwise unsigned right shift operator


  Whats the C# equivalent of >>> ?
LVL 12
williamcampbellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

williamcampbellAuthor Commented:
>>> from Flash ActionScript / JScript
0
Carl TawnSystems and Integration DeveloperCommented:
The right shift operator in C# is >>.

Theres a more complete overview of binary operators here:

    http://www.c-sharpcorner.com/Language/BitWiserOpsInCSCH001.asp
0
williamcampbellAuthor Commented:
The problem is C#'s >> right shifts but does not preserve the sign ...

  so if the number is negative it shifts a zero in making the number positive

  0111  7  
  >> 1
  0011  3

  1111  -7
  >> 1
  0111  3  (not -3)

  In Java  >>>
  1111 -7
  >>> 1
  1011 -3

  Hope this clears up what I want

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

williamcampbellAuthor Commented:

My bad I got it the wrong way around

  C# >> does preserve the sign bit

  Java >>> clears the sign bit

  Any bit pushers out there give me some C# code that does >>>?

Thanks
0
c_myersCommented:
You can cast/convert the int's to uint's and then >> and << will be unsigned (zero-fill).

If you use int or long, >> will be signed.

If you use uint or ulong, >> will be unsigned.

The reason Javascript/JScript/JScript.NET has >>> is because it's not strongly typed, and therefore there is no difference between signed and unsigned integer, thus they need an explicit operator for unsigned bitwise operations.
0
c_myersCommented:
FYI, the first paragraph in this article talks about what I just said. Perhaps they can explain it better than I :) ---

http://www.ondotnet.com/pub/a/dotnet/2001/06/07/csharp_java.html?page=3
0
williamcampbellAuthor Commented:

 Ack I still dont get it

 perhaps a better question is

 function bit_rol(num,cnt)
{
    return (num << cnt) | (num >>> (32-cnt) );
}

Conver this to C#

 public Int32 bit_rol(Int32 num,Int32 cnt)
{
   ???
}
0
c_myersCommented:
That worked for me, for most of the tests I did at least.

private static int bit_rol( int num, int cnt )
{
        // First, get the bits of the int and stuff them into a uint
      uint unum = BitConverter.ToUInt32( BitConverter.GetBytes(num), 0 );            
 
        // Do our dirty work
      uint ures = ( (unum << cnt) | (unum >> (32-cnt) ) );            

        // Then turn the bits back into an int and return
      return BitConverter.ToInt32( BitConverter.GetBytes(ures), 0);            
}



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
williamcampbellAuthor Commented:
Nice one C_m thanks a bunch.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

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.