?
Solved

Converting Zero-Fill Right Shift to .NET

Posted on 2004-09-16
8
Medium Priority
?
1,456 Views
Last Modified: 2007-12-19
In Javascript, if I evaluate the following like so:

var i=-306674912;
alert(i>>>=1);

The zero-fill right-shift assignment operator (and thus the alert box) produces:

1994146192

How can I get this behavior in .NET? It doesn't have a ">>>+" operator or a ">>>" operator.

Thanks!

0
Comment
Question by:electricstory
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 4

Expert Comment

by:vigrid
ID: 12079880
Use the >> operator for zero-fill right shift and << operator for zero-fill left shift. C# code:

long i = 1;
for(int j = 0; j < 64; j++)
{
      Console.Write("i={0},", i);
      Console.WriteLine("\ti << {0} = {1}", j, i << j);
}

HTH
0
 

Author Comment

by:electricstory
ID: 12079898
Well, maybe I'm giving it the wrong name. But the ">>" operator isn't the same as the ">>>" operator. Try it. If you can get the same result as the javascript example above in .NET, you get the points.
0
 
LVL 4

Accepted Solution

by:
vigrid earned 2000 total points
ID: 12080152
static void Main(string[] args)
{
  int i = -306674912;  // 11111111 11111111 11111111 11111111 11101101 10111000 10000011 00100000
  int k = 1994146192; // 00000000 00000000 00000000 00000000 01110110 11011100 01000001 10010000
  int j1 = ZFRS(i, 1);   // 00000000 00000000 00000000 00000000 00110110 11011100 01000001 10010000
}

private static int ZFRS(int i, int j)
{
  bool maskIt = (i < 0);
  i = i >> j;
  if(maskIt)
    i &= 0x7FFFFFFF;
  return i;
}
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 4

Expert Comment

by:vigrid
ID: 12080160
int i = -306674912;  // 11111111 11111111 11111111 11111111 11101101 10111000 10000011 00100000
int k = 1994146192; // 00000000 00000000 00000000 00000000 01110110 11011100 01000001 10010000
int j1 = ZFRS(i, 1);   // 00000000 00000000 00000000 00000000 01110110 11011100 01000001 10010000

The comment was wrong. I left it after some tries and forgot to change it.

HTH
0
 

Author Comment

by:electricstory
ID: 12081142
You rock.
0
 

Author Comment

by:electricstory
ID: 12082071
Actually, if you're still looking in and feel so inclined, Vigrid, I can still use your help. Your mask only works for that one particular example. Positive numbers seem to be the same between >>> and the .net >>, but there's that left fill bit with negative numbers. Is there any (simple) way to perform that left fill every time a negative number comes up? I'll open a new question on it.

int i = -306674912;  // 11111111 11111111 11111111 11111111 11101101 10111000 10000011 00100000
int k = 1994146192; // 00000000 00000000 00000000 00000000 01110110 11011100 01000001 10010000
0
 
LVL 4

Expert Comment

by:vigrid
ID: 12091282
What do you expect to get from the method? Please just give me the input and needed output bits. Few examples would be great. I'm not so experienced in JavaScript, so I wasn't 100% sure if my solution will work every time.
0
 

Author Comment

by:electricstory
ID: 12092474
It looks like many operations result in an overflow error in .NET if you don't use longs. Maybe this is simpler than I think it is. Anyway, I've expanded your function, and this seems to work, though it's admitted kludgy (and I know I'm awful about naming variables). Thanks for your help; my grasp of hex was pretty shaky before this, but I think I'm getting it!

Private Shared Function ZFRS(ByVal l As Long, ByVal j As Integer) As Long
        Dim maskIt As Boolean = (l < 0)
        l = l >> j
        If (maskIt) Then
            Dim int As Integer = 0
            Do While Hex(l).Substring(int, 1) = "F"
                int += 1
                If int = 15 Then Exit Do
            Loop
            Dim strText As String = Hex(l)

            Dim strHex As String


            If int <> 8 Then

                Dim intZero As Integer
                Dim strHexDigit As String

                'For every four shift bits, you'll have a hex 0; add it to your shift digit
                For intZero = 0 To Math.Floor(j / 4) - 1
                    strHexDigit += "0"
                Next
                Dim strFinalDigit As String = strText.Substring(8 + intZero, 1)

                'otherwise, put in zeroes in front of the last hex digit
                Dim lFinalDigit As Long

                Dim intMod As Integer
                If j >= 4 Then
                    intMod = j Mod 4
                Else
                    'intMod = 0
                    intMod = j
                End If

                Dim strAddDigit, strOffset As String
                For intZero = 0 To intMod - 1
                    strOffset += "0"
                Next

                Select Case strfinaldigit
                    Case "1"
                        strAddDigit += "0001"
                    Case "2"
                        strAddDigit += "0010"
                    Case "3"
                        strAddDigit += "0011"
                    Case "4"
                        strAddDigit += "0100"
                    Case "5"
                        strAddDigit += "0101"
                    Case "6"
                        strAddDigit += "0110"
                    Case "7"
                        strAddDigit += "0111"
                    Case "8"
                        strAddDigit += "1000"
                    Case "9"
                        strAddDigit += "1001"
                    Case "A"
                        strAddDigit += "1010"
                    Case "B"
                        strAddDigit += "1011"
                    Case "C"
                        strAddDigit += "1100"
                    Case "D"
                        strAddDigit += "1101"
                    Case "E"
                        strAddDigit += "1110"
                    Case "F"
                        strAddDigit += "1111"
                End Select

                If Not strOffset Is Nothing Then
                    strAddDigit = strOffset + strAddDigit.Substring(strOffset.Length, (4 - strOffset.Length))
                End If

                Select Case strAddDigit
                    Case "0001"
                        strAddDigit = "1"
                    Case "0010"
                        strAddDigit = "2"
                    Case "0011"
                        strAddDigit = "3"
                    Case "0100"
                        strAddDigit = "4"
                    Case "0101"
                        strAddDigit = "5"
                    Case "0110"
                        strAddDigit = "6"
                    Case "0111"
                        strAddDigit = "7"
                    Case "1000"
                        strAddDigit = "8"
                    Case "1001"
                        strAddDigit = "9"
                    Case "1010"
                        strAddDigit = "A"
                    Case "1011"
                        strAddDigit = "B"
                    Case "1100"
                        strAddDigit = "C"
                    Case "1101"
                        strAddDigit = "D"
                    Case "1110"
                        strAddDigit = "E"
                    Case "1111"
                        strAddDigit = "F"
                End Select

                strFinalDigit = strAddDigit

                strHex = strHexDigit & strFinalDigit
                int = 9 + (strHex.Length - 1)
            Else
                strHex = "7"
                int = 9
            End If
            strHex += Hex(l).Substring(int)
            If strHex.Length = 8 And strHex.Substring(0, 1) = "F" Then
                strHex = "7" & strHex.Substring(1)
            End If
            l = Int64.Parse(strHex, Globalization.NumberStyles.HexNumber)

        ElseIf Hex(l).Length > 8 Then ' the number is positive

            Dim strText As String = Hex(l)
            Dim strHex As String
            'find out what's in the left-most 32-bit positions. You've already shifted it over, so fill in w/ zeroes

            Dim intZero As Integer
            Dim strHexDigit As String

            'For every four shift bits, you'll have a hex 0; append it to your shift digit
            For intZero = 0 To Math.Floor(j / 4) - 1
                strHexDigit += "0"
            Next
            Dim strFinalDigit As String = strText.Substring((Hex(l).Length - 8) + intZero, 1)

            'otherwise, put in zeroes in front of the last hex digit
            Dim lFinalDigit As Long

            Dim intMod As Integer
            If j >= 4 Then
                intMod = j Mod 4
            Else
                intMod = j
            End If

            Dim strAddDigit, strOffset As String
            For intZero = 0 To intMod - 1
                strOffset += "0"
            Next

            Select Case strfinaldigit
                Case "1"
                    strAddDigit += "0001"
                Case "2"
                    strAddDigit += "0010"
                Case "3"
                    strAddDigit += "0011"
                Case "4"
                    strAddDigit += "0100"
                Case "5"
                    strAddDigit += "0101"
                Case "6"
                    strAddDigit += "0110"
                Case "7"
                    strAddDigit += "0111"
                Case "8"
                    strAddDigit += "1000"
                Case "9"
                    strAddDigit += "1001"
                Case "A"
                    strAddDigit += "1010"
                Case "B"
                    strAddDigit += "1011"
                Case "C"
                    strAddDigit += "1100"
                Case "D"
                    strAddDigit += "1101"
                Case "E"
                    strAddDigit += "1110"
                Case "F"
                    strAddDigit += "1111"
            End Select

            If Not strOffset Is Nothing Then
                strAddDigit = strOffset + strAddDigit.Substring(strOffset.Length, (4 - strOffset.Length))
            End If

            Select Case strAddDigit
                Case "0001"
                    strAddDigit = "1"
                Case "0010"
                    strAddDigit = "2"
                Case "0011"
                    strAddDigit = "3"
                Case "0100"
                    strAddDigit = "4"
                Case "0101"
                    strAddDigit = "5"
                Case "0110"
                    strAddDigit = "6"
                Case "0111"
                    strAddDigit = "7"
                Case "1000"
                    strAddDigit = "8"
                Case "1001"
                    strAddDigit = "9"
                Case "1010"
                    strAddDigit = "A"
                Case "1011"
                    strAddDigit = "B"
                Case "1100"
                    strAddDigit = "C"
                Case "1101"
                    strAddDigit = "D"
                Case "1110"
                    strAddDigit = "E"
                Case "1111"
                    strAddDigit = "F"
            End Select


            strFinalDigit = strAddDigit

            'add strHexdigit to the beginning of strFinalDigit
            strHex = strHexDigit & strFinalDigit
            Dim int As Integer = Hex(l).Length
            int = int - (7 - (strHex.Length - 1))

            strHex += Hex(l).Substring(int)
            l = Int64.Parse(strHex, Globalization.NumberStyles.HexNumber)


        End If

        Return l

    End Function
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

752 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