32-bit unsigned math with C#?

B

Brians256

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#.
 
P

Pieter Philippaerts

Brians256 said:
Why is it not possible to use bit-wise operators on 32-
bit unsigned integers?

It is possible.
Consider this C code:

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.
Hopefully I am missing
something simple, so I can port my hashing function to C#.

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

Regards,
Pieter Philippaerts
http://www.mentalis.org/
 
B

Brians256

<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.
 
B

Brians256

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top