williamcampbell
asked on
bitwise unsigned right shift operator
Whats the C# equivalent of >>> ?
The right shift operator in C# is >>.
Theres a more complete overview of binary operators here:
http://www.c-sharpcorner.com/Language/BitWiserOpsInCSCH001.asp
Theres a more complete overview of binary operators here:
http://www.c-sharpcorner.com/Language/BitWiserOpsInCSCH001.asp
ASKER
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
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
ASKER
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
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.
If you use int or long, >> will be signed.
If you use uint or ulong, >> will be unsigned.
The reason Javascript/JScript/JScript
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
http://www.ondotnet.com/pub/a/dotnet/2001/06/07/csharp_java.html?page=3
ASKER
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)
{
???
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nice one C_m thanks a bunch.
ASKER