2

I'm looking to convert this line of C code to C#:

const u64 a = 1, b = -a;

So my understanding is that both constants are unsigned 64-bit integers. If so, what is the result going to look like?

Or is the second constant actually promoted and therefore defined as a signed integer?

4
  • Are you asking what the result of running this will be in C, or what the equivalent C# should be? Feb 8, 2012 at 2:26
  • Why do you want to negate an unsigned int? Doesn't that defeat the purpose?
    – jb.
    Feb 8, 2012 at 2:27
  • @Chris: I'm asking both.
    – HTTP 410
    Feb 8, 2012 at 2:28
  • @jb: This is a line of code from somebody else's C program - I'm looking to convert the line to C#.
    – HTTP 410
    Feb 8, 2012 at 2:28

4 Answers 4

4

This would be the equivalent C#:

const ulong a = 1, b = unchecked((ulong)-1);

Or more simply:

const ulong a = 1, b = 18446744073709551615;
4
  • Aha - many thanks! So the C code actually results in an overflow to (in C#) UInt64.MaxValue?
    – HTTP 410
    Feb 8, 2012 at 2:33
  • If you want the max value const ulong b = ~(ulong)0; also works
    – PostMan
    Feb 8, 2012 at 2:36
  • @RoadWarrior - the result isn't due to an overflow, just the conversion to an unsigned 64 bit int. See my answer for details. Feb 8, 2012 at 2:37
  • @TimothyJones: The value already has type u64 when the negation operator is applied, so the huge value is due to reduction of arithmetic results modulo 2^64, not anything about conversion. Feb 8, 2012 at 4:34
3

The C# compiler will try to protect you from accidentally negiting an unsigned integer, but you can force it like this:

ulong a = 1;
ulong b = (ulong)-(long)a;

The result will be exactly the same bunch of bits as when negating a signed integer (i.e. two's complement), the only difference is how these bits are interpreted.

1
  • Thanks, that answers another question I had.
    – HTTP 410
    Feb 8, 2012 at 2:53
1

Due to the behaviour of negating unsigned integers, the representation of -(u64)1 is all 1s. So, after the following:

const u64 a = 1, b = -a;

// a is now 0x0000000000000001
// b is now 0xffffffffffffffff

Of course, 0xffffffffffffffff is also (2^64) -1, which is 18446744073709551615.

In my opinion, it would have been clearer for the original programmer to instead write:

const u64 a = 1, b = ~(u64)0;

I'm not a C# programmer, but I suspect the following will work for you:

const ulong b = ~(ulong)0;
4
  • I think ~0 depends on the same undefined behavior as -1 (casting int to uint64). ~(u64)0 should solve this. Feb 8, 2012 at 2:48
  • It's not UD in C. The standard clearly defines the result of conversion of signed negative integers to unsigned integers. But we don't even have that here. 1 is signed positive that is converted to unsigned (to type of a) without change. a is unsigned because it's defined as unsigned. And so is -a. See this question and answers to it Feb 8, 2012 at 2:57
  • @Alex Thanks, I'll remove the answer shortly. Can you tell me where the C standard defines the result of conversion from signed negative to unsigned? If I'm reading it correctly, in your question you say it doesn't? Feb 8, 2012 at 3:09
  • @Alex: Updated answer to remove the comments about UB. Thanks for the feedback! Feb 8, 2012 at 3:22
0

You get an ambiguous invocation error because -1 is not possible as an unsigned type (see comment below). You would have to use Int64 as type (which equals long).

When using long as type then the result will be a = 1, b = -1 and both longs.

2
  • How can b be -1 if it's unsigned? Sorry, can't check at home as I only have the C# version of Visual Studio Express.
    – HTTP 410
    Feb 8, 2012 at 2:27
  • If that's about C, there's no error here, even though mathematically -1 cannot be non-negative. The only error can be that the programmer doesn't know what's he doing. Feb 8, 2012 at 3:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.