You can check if the most significant bit is set by using the mask ~(~0U >> 1). Here's some code adapted from yours:
#include <stdio.h>
int main() {
int addr=0x0fffffff;
unsigned int mask = ~(~0U >> 1);
if (addr & mask) {
printf("Bit is set");
}
else
printf("Bit is not set");
return 0;
}
Since you're new to C, the mask might look a little weird, so let's see what's happening here:
~0U is an unsigned number with every bit set to 1: the unary ~ operator negates every bit on 0, thus making every bit 1. ~0U >> 1 shifts it all by one position to the right, so now you have (on 32 bits machines):
01111111111111111111111111111111
Negating it all again, which is ~(~0U >> 1), yields:
10000000000000000000000000000000
So now you have a number with only the most significant bit set, this is used as a mask to test other numbers.
Using this kind of construct is portable because you're not relying on the size of a number. ~(~0U >> 1) will always work no matter how many bits there are in an int.
The number is declared to be unsigned because the right shift operator can cause sign extension in regular ints, and we don't want this. With unsigned numbers, right shift always inserts leading 0's.