As far as I know when you use a left shift bitwise operator in C it's guaranteed that the vacant bits will be filled with 0s. However, I've read that the right shift is implementation-dependent, meaning in some machines the vacant bits will be filled with 0s, in others they will be filled with 1s.
I am using the right shift in a program, and indeed my machine is filling the vacant bits with 1s. Problem is I would need it to fill with 0s instead.
Is there a way to force 0s to be used on right shifts?
One solution would be, after the right shift is applied, to create a mask like 011111111 and then apply a bitwise AND, which will change the leftmost 1 that was inserted to a 0.
But this is cumbersome and wastes time. If there was a way to tell my machine to fill right shifts with 1s it would be much easier.
Thanks
Cast the number to unsigned
and then shift. That will force a 0-fill.