Solved

# bitwise unsigned right shift operator

Posted on 2006-03-30
1,088 Views

Whats the C# equivalent of >>> ?
0
Question by:williamcampbell

LVL 12

Author Comment

>>> from Flash ActionScript / JScript
0

LVL 52

Expert Comment

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

LVL 12

Author Comment

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

LVL 12

Author Comment

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

LVL 4

Expert Comment

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

LVL 4

Expert Comment

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

LVL 12

Author Comment

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

LVL 4

Accepted Solution

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

LVL 12

Author Comment

Nice one C_m thanks a bunch.
0

## Featured Post

### Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…