C Programming Code Examples C > Bitwise Operators Code Examples C program to get highest order set bit of a number C program to get highest order set bit of a number Write a C program to input any number from user and find highest order set bit of given number using bitwise operator. How to find the highest order of the set bit of a given number using bitwise operator in C programming. Highest order set bit Highest position of a set bit from left to right in a number is said to be highest order set bit of that number. Highest order set bit of any negative integers is 31 (for 32 bit signed integer). Since, highest order set bit of any negative number is its Most Significant Bit (MSB). Input number from user. Store it in some variable say, j. Find total bits required to store an integer in memory say, INT_SIZE = sizeof(int) * 8. Run a loop from 0 to INT_SIZE. The loop structure should look like for(i=0; i<INT_SIZE; i++). Initialize a variable to store highest order, say order = 0;. Inside the loop if ith bit is set then update order to i i.e. order = i;. #include <stdio.h> #define INT_SIZE sizeof(int) * 8 /* Integer size in bits */ int main() { int j, order=0, i; /* Input number from user */ printf("Enter any number: "); scanf("%d", &j); /* Iterate over each bit of integer */ for(i=0; i<INT_SIZE; i++) { /* If current bit is set */ if((j>>i) & 1) order = i; } printf("Highest order set bit in %d is %d", j, order); return 0; }