the purpose of my code is to create a mask of n zeroes to the right of my int (32 bits) representation. My approach was first to have the negative one stored in a variable and then shift it left n spaces to have n zeroes to the right. The code is the following:
int mask(int n){
int neg1=(1<<31)>>31;
int mask=neg1<<n;
return mask;
}
Nonetheless when n is 32 I would like to get the value 0x0, but instead I am getting 0xffffffff (neg1). That happens when I do the shift to the variable. But when I do the shift to the constant itself, it works like a charm. The new code would be:
mask=0xffffffff<<n;
Nonetheless, I'm not allowed to use constants of more than 8 bits. So I need the value stored in another variable. Can somebody tell me why is this happening and how can I solve it?
Thank you so much!