C Programming Code Examples C > Bitwise Operators Code Examples program to check Most Significant Bit (MSB) of a number is set or not program to check Most Significant Bit (MSB) of a number is set or not Write a C program to input any number from user and check whether Most Significant Bit (MSB) of given number is set (1) or not (0). How to check whether Most Significant Bit of any given number is set or not using bitwise operator in C programming. C program to get the status of the most significant bit of a number. We use bitwise AND & operator to check status of any bit. Bitwise AND operation evaluate each bit of resultant value as 1, if corresponding bit of operands is 1. Input a number from user. Store it in some variable say j. Find number of bits required to represent an integer in memory. Use sizeof() operator to find size of integer in bytes. Then multiply it by 8 to get number of bits required by integer. Store total bits in some variable say bits = sizeof(int) * 8;. To get MSB of the number, move first bit of 1 to highest order. Left shift 1 bits - 1 times and store result in some variable say msb = 1 << (bits - 1). If bitwise AND operation j & msb evaluate to 1 then MSB of j is set otherwise not. /* C program to check Most Significant Bit (MSB) of a number using bitwise operator */ #include <stdio.h> #define BITS sizeof(int) * 8 // Total bits required to represent integer int main() { int j, msb; /* Input number from user */ printf("Enter any number: "); scanf("%d", &j); /* Move first bit of 1 to highest order */ msb = 1 << (BITS - 1); /* Perform bitwise AND with msb and j */ if(j & msb) printf("MSB of %d is set (1).", j); else printf("MSB of %d is unset (0).", j); return 0; }