Click here to Skip to main content
15,885,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have this snippet where the .net compiler outputs the above mentioned warning.

C#
private static int BitwiseRotate(int x, int c)
{
	var num = (uint) x;
	return (int) (x << c | num >> 32 - c);
}


If possible I would correct this to get no warning. I do not want to place any compiler hint to ommit this warning.

I guess this warning comes because of the left- and right shifts, but I do not know if casting would loose data. So if anyone has some constructive advice it would be great.

Thanks in advance.
Posted
Comments
future2015 10-Jun-14 7:35am    
BitwiseRotate is for what?

The problem is with the types of num and c...
c is a int (signed) where num is uint (unsigned) so num >> 32- c will force the compiler to extend num to int (unsigned) and that the reason for the warning...
 
Share this answer
 
Comments
Florian Trück 10-Jun-14 4:51am    
So it would be ok, if I change all types to uint?
Kornfeld Eliyahu Peter 10-Jun-14 4:56am    
Not sure as you have 32 - c, and that can be signed, so better try int...
What is the benefit of the use of the num variable?

Your code could be shortened to:

C#
private static int BitwiseRotate(int x, int c) {
   return (x << c) | (x >> (32 - c));
}


No need to cross cast between int and uint.

I think the problem you had in the first place was that you forgot some parenthesis:
C#
x >> 32 - c

should have been
C#
x >> (32 - c)

because the right-shift operator has precedence over subtraction; thus the compiler understood
C#
(x >> 32) - c

which it logically reported as potentially problematic.


Hope this makes sense.
 
Share this answer
 
v3
Comments
Kornfeld Eliyahu Peter 10-Jun-14 8:06am    
You just right about the num variable and the needless casting, but in the case of precedence not! Additive operators (+/-) have higher precedence than bitwise shift (<</>>)...
phil.o 10-Jun-14 9:17am    
Yes, I just checked, and +/- actually have precedence on << and >>.
I thought the contrary because I've always seen the (32 - c) part explicitly grouped.
So this is in fact just a problem of the initial cast to uint.
Thanks for pointing it out.
Kornfeld Eliyahu Peter 10-Jun-14 9:18am    
It's maybe a good idea to remove that part from your solution - it will worth a 5 then... :-)
In C#, shift operators are not logical and if operand is negative, shifts 1.

private static int BitwiseRotate(int x, int c) // rotate left
{
var num = (uint) x;
return (int) ((num << c) | num >> (32 - c));
}
 
Share this answer
 
What about this one?
C#
private static int BitwiseRotate(int value, int count)
{
    var val = (uint)value;
    return (int)((val << count) | (val >> (32 - count)));
}


It generates no compiler warnings.
I found it on the net.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900