Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1135
  • Last Modified:

bitwise unsigned right shift operator


  Whats the C# equivalent of >>> ?
0
williamcampbell
Asked:
williamcampbell
  • 5
  • 3
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
williamcampbellAuthor Commented:
Nice one C_m thanks a bunch.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now