32-bit unsigned math with C#?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brians256

    #1

    32-bit unsigned math with C#?

    Why is it not possible to use bit-wise operators on 32-
    bit unsigned integers? Isn't that a bit backwards? How
    else can you specify a full 32-bits?

    I'm trying to use some C code by translating it to C#.
    However, it appears that I must either suffer a
    performance hit by moving to 64-bit integers or stuff the
    algorithms into a DLL via another more sane language.

    Consider this C code:

    int foo(void)
    {
    int i = 0x81234567;
    int j = i >> 1;
    return (j & ~i);
    }

    You can't do that easily in C#. A System.Int32 type
    cannot contain 0x80000000 or larger value. However, a
    System.UInt32 type will not allow bitwise operators.

    Is Microsoft insane, or have they restricted the UInt32
    type for some good reason? Hopefully I am missing
    something simple, so I can port my hashing function to C#.
  • Pieter Philippaerts

    #2
    Re: 32-bit unsigned math with C#?

    "Brians256" <Brians256@hotm ail.com> wrote in message[color=blue]
    > Why is it not possible to use bit-wise operators on 32-
    > bit unsigned integers?[/color]

    It is possible.
    [color=blue]
    > Consider this C code:[/color]

    Here are two equivalent C# versions:

    // using unsigned integers
    public uint foo2() {
    uint i = 0x81234567;
    uint j = i >> 1;
    return (j & ~i);
    }
    // using signed integers
    public int foo() {
    int i = -2128394905;
    int j = i >> 1;
    return (j & ~i);
    }

    Both compile and work fine on my computer.
    [color=blue]
    > Hopefully I am missing
    > something simple, so I can port my hashing function to C#.[/color]

    What errors do you exactly get with your code? Posting some code would be
    helpful.

    Regards,
    Pieter Philippaerts
    Free software components for C#, VB.NET and other .NET languages.



    Comment

    • Brians256

      #3
      Re: 32-bit unsigned math with C#?

      <insert egg on face>

      Ooops, hold on. Wrong project I got that error log
      from. foo2() compiles correctly, now I have to figure
      out why my prior example code didn't work as expected.
      [color=blue]
      >-----Original Message-----
      >[color=green]
      >>// using unsigned integers
      >>public uint foo2() {
      >> uint i = 0x81234567;
      >> uint j = i >> 1;
      >> return (j & ~i);
      >>}
      >>// using signed integers
      >>public int foo() {
      >> int i = -2128394905;
      >> int j = i >> 1;
      >> return (j & ~i);
      >>}
      >>
      >>Both compile and work fine on my computer.
      >>What errors do you exactly get with your code? Posting[/color]
      >some code would be[color=green]
      >>helpful.[/color]
      >
      >I pasted foo2() into my C# code and it failed just as my
      >prior code example did. The reason I have not posted[/color]
      the[color=blue]
      >code is that the code is proprietary, but the problem is
      >so easily reproduced that I believed my problem to be
      >well known and understood. Also, the warnings that I[/color]
      get[color=blue]
      >are truly odd. The error reporting is malformed and[/color]
      most[color=blue]
      >of the errors have nothing to do with the actual error.
      >
      >This is what I actually get when I pasted foo2() into an
      >existing and compiling C# file. Note that none of the
      >line number actually associate with the foo2() function,
      >which ranges from line 105 to line 110 in this file
      >kmd5.cs.
      >
      >c:\programs\sa mple\kmd5.cs(11 4,13): error CS0650: Syntax
      >error, bad array declarator. To declare a managed array
      >the rank specifier precedes the variable's identifier
      >c:\programs\sa mple\kmd5.cs(13 4,41): error CS1031: Type
      >expected
      >c:\programs\sa mple\kmd5.cs(13 4,50): error CS1002: ;
      >expected
      >c:\programs\sa mple\kmd5.cs(13 4,50): error CS1525:[/color]
      Invalid[color=blue]
      >expression term ')'
      >
      >Oh, and I already know that .NET has a built-in MD5 hash
      >function. This was just part of an existing library[/color]
      that[color=blue]
      >I'm porting into .Net (I hope).
      >
      >I'm using VS.2003, if that's any help.
      >.
      >[/color]

      Comment

      • Brians256

        #4
        Re: 32-bit unsigned math with C#?

        It was all a figment of my imagenation, I suppose.
        Another error masqueraded quite successfully to convince
        me that you cannot use the uint type with the shift
        operator. Believe it or not, but the task list was not
        as accurate as the build output window, and I got red-
        herrings from bad line numbers.

        Thanks for the patience.

        Comment

        Working...