Question: I am new to C and learning how bit-wise operator works in C.I wanted to check if most significant of an address is set or not

```
  int main()
  {

   int addr=0x0fffffff;

   if(addr&0x80)
   {
      Printf("Bit is set");
   }
    else
     printf("Bit is not set");
     return 0;
   }

```

EDIT: I Would like 0x80 to be represented as 1000 0000 0000 0000 0000 0000 0000 0000 ,I have seen such implementation in past where no need of writing all the bits using some macros but not able to recall it where?

Answer: 
Your code
 int addr=0x0fffffff;
and
 if(addr&0x80)
causes this condition always true.

```
0x0fffffff  => 0000 1111 1111 1111 .... 1111 1111
where 0x80  =>                          1000 0000
-------------------------------------------------
bitwise AND                             1000 0000 -> non zero means true!

```
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.