7

I can't seem to get rid of this warning for the following line of code:

d.word[wrdIndex++] = d.GetWord(english) | (ulong)i;

the warning applies to the code after the assignment operator. the method GetWord returns a ulong. I've tried the following to no avail:

d.word[wrdIndex++] = (d.GetWord(english) | (ulong)i);
d.word[wrdIndex++] = d.GetWord(english) | ((ulong)i);
d.word[wrdIndex++] = ((ulong)d.GetWord(english)) | ((ulong)i);

Anyone have any ideas?

3 Answers 3

17

UPDATE: This question was the subject of my blog on Monday November 29th 2010.

Thanks for the great question!


You get rid of the warning by first thinking about the warning and deciding if the compiler is right to bring the problem to your attention in the first place! Are you in fact going to get incorrect results from the bitwise or should the conversion from a signed type to a larger unsigned type sign-extend the integer?

If the answer is yes then your solution is wrong. Don't eliminate the warning by tricking the compiler into still doing the wrong thing without giving you the warning. Eliminate the warning by doing the right thing: don't use a conversion that sign-extends the integer.

If the answer is no, and you want the sign extension then your solution or Hans Passant's solution is correct. However, I would put a comment to that effect in the code, because as it stands, it is hard to see that the sign extension is desirable.

3
  • The code certainly does what it should (and this is the right way to do what I want in this case I think) - I'm just surprised that the 2nd of the 3 attempts : d.GetWord(english) | ((ulong)i) still delivers the warning as the ((ulong)i) is cast to a ulong before the bitwise OR operator is applied, right? Commented Oct 30, 2010 at 18:50
  • 1
    Ah, now I get it - it's actually pretty clever of the compiler to warn me about this I guess. Is there a way to cast to ulong without sign extension (other than by masking off the top bit manually)? Commented Oct 30, 2010 at 18:52
  • 1
    cast to uint first. (ulong)(uint)i Commented Oct 31, 2010 at 11:55
8
int i=-1;                 // =         0xFFFFFFFF
ulong u1=(ulong)i;        // = 0xFFFFFFFFFFFFFFFF
ulong u1a=(ulong)(long)i; // = 0xFFFFFFFFFFFFFFFF
ulong u2=(ulong)(uint)i;  // = 0x00000000FFFFFFFF

So a cast from int to ulong corresponds to first sign-extending to (signed) long, and then casting away the sign.

1
  • this should be the accepted answer, much clearer without the verbosity, a picture is worth a thousand words
    – Alas
    Commented Oct 3, 2020 at 20:33
2

Sorry, just got rid of it thusly:

ulong w = d.GetWord(english);
ulong u = (ulong)i;
d.word[wrdIndex++] = w | u;
1
  • As pointed out above, if i is negative, your code will break (because the rest of u will contain 0xFFFFFFFF, and so the d.GetWord(english) value will be lost entirely instead of being combined with i).
    – MrCranky
    Commented May 22, 2014 at 17:33

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.