I need to shift an unsigned int to the right more than 32 times and still get a proper answer of zero instead of the random or original number. E.g 8 >> 40 should = 0 but it returns a random number.
I understand a loop that shifts one place right at a time would solve this problem as it would fill in zeros as it went. However my current code for this doesn’t work for some reason. What am I doing wrong?
unsigned int shiftR(unsigned int a, unsigned int b) {
unsigned int i=0;
while (i < b) {
a >> 1;
i++;
}
return a;
}
This gives me a compile warning that it has no effect ( a >> 1;). How come?
Thanks!
You want to use
a >>= 1;ora = a >> 1;this is becausea >> 1shifts a to the right once and returns the result. It doesn’t assign the result toa