473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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#.
Nov 15 '05 #1
3 8107
"Brians256" <Br*******@hotmail.com> wrote in message
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/
Nov 15 '05 #2
<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.
-----Original Message-----
// 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? Postingsome code would be
helpful.


I pasted foo2() into my C# code and it failed just as my
prior code example did. The reason I have not posted

thecode 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 getare truly odd. The error reporting is malformed and mostof 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\sample\kmd5.cs(114,13): error CS0650: Syntax
error, bad array declarator. To declare a managed array
the rank specifier precedes the variable's identifier
c:\programs\sample\kmd5.cs(134,41): error CS1031: Type
expected
c:\programs\sample\kmd5.cs(134,50): error CS1002: ;
expected
c:\programs\sample\kmd5.cs(134,50): error CS1525: Invalidexpression term ')'

Oh, and I already know that .NET has a built-in MD5 hash
function. This was just part of an existing library thatI'm porting into .Net (I hope).

I'm using VS.2003, if that's any help.
.

Nov 15 '05 #3
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.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ivar | last post by:
Hi, For my supprise I found that functions have 32 parameter limit. Where to find more info about this limitation or similar limitations ? I need at least 50, 100 would be ok. Real life...
5
by: Ian Rutherford | last post by:
Heya guys, It seems VB .net no longer supports the awesome ability of VB 6 to declare something as a string and specify how long the string would be all in one line: Public myString as String *...
6
by: barcaroller | last post by:
I couldn't find a message-digest newsgroup, so I posted here. I have a C function that converts a string of arbitrary length to a 32-bit hash value. I realize this is overkill but I used...
9
by: beni.cherniavsky | last post by:
Python seems to be missing a UCS-32 codec, even in wide builds (not that it the build should matter). Is there some deep reason or should I just contribute a patch? If it's just a bug, should I...
16
by: chandanlinster | last post by:
As far as I know floating point variables, that are declared as float follow IEEE format representation (which is 32-bit in size). But chapter1-page no 9 of the book "The C programming language"...
10
by: lesperancer | last post by:
you start with a small application in access97, then you have more modules and more... and you reach the point where tables like 'item' and 'employee' reach the limit and you know there's more...
3
by: Barry | last post by:
void func(int array) { for (int i = 0; i < 32; ++i) cout << array << ' '; } as the code above shows, I know int array here will decay into int *, but I still find the number 32 useful for...
2
by: Jeffrey Walton | last post by:
Hi All, BMP Strings are a subset of Universal Strings.The BMP string uses approximately 65,000 code points from Universal String encoding. BMP Strings: ISO/IEC 10646, 2-octet canonical form,...
6
by: docbook.xml | last post by:
I have the following in the XHTML 1.0 Strict page: <meta http-equiv="Content-Type" content="text/html;charset=utf-32" /> However W3 validator complains that "The character encoding specified in...
18
by: lak | last post by:
I am studying the Advanced programming in the unix environment. There they says that we can register upto 32 functions with atexit(). Why that is limited to 32 functions? can any one tell the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.