Write a C program to input any number from user and find lowest order set bit of given number using bitwise operator. How to find first set bit in a given number using bitwise operator in C programming. Logic to get first set bit of a number using C program.

**Example**

**Input**

Input any number: 22

**Output**

First set bit: 1

## Required knowledge

Bitwise operators, Data types, Variables and Expressions, Basic input/output, If else, For loop

## Lowest order set bit

Lowest order or first set bit of any number is the first bit set starting from left to right. Lowest order set bit

of any odd number is 0 since first bit of any odd number is always set.

## Logic to get lowest order set bit

Logic to get lowest order set bit is almost similar to highest order set bit of a number.

Step by step descriptive logic to get lowest order set bit.

- Input number from user. Store it in some variable say,
`num`. - Find total bits required to store an integer in memory say,
`INT_SIZE = sizeof(int) * 8`

.Read more - How to find size of a data type using sizeof() operator.

- 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 lowest order set bit, say
`order = 0;`

. - Inside the loop if
`i`bit is set then update^{th}`order`to`i`i.e.`order = i;`

and terminate from loop.

## Program to get lowest order set bit

```
/**
* C program to get lowest order set bit in a number
*/
#include <stdio.h>
#define INT_SIZE sizeof(int) * 8 /* Integer size in bits */
int main()
{
int num, order, i;
/* Input number from user */
printf("Enter any number: ");
scanf("%d", &num);
/* Initially set the order to max size of integer */
order = INT_SIZE - 1;
/* Iterate through each bit of integer */
for(i=0; i<INT_SIZE; i++)
{
/* If current bit is set */
if((num>>i) & 1)
{
order = i;
/* Terminate the loop */
break;
}
}
printf("Lowest order set bit in %d is %d", num, order);
return 0;
}
```

Output

Enter any number: 22 Lowest order set bit in 22 is 1

Happy coding 😉

### Recommended posts

- Bitwise operator programming exercises index.
- C program to check Least Significant Bit (LSB) of a number is set or not.
- C program to check Most Significant Bit (MSB) of a number is set or not.
- C program to flip bits of a binary number using bitwise operator.
- C program to count trailing zeros in a binary number.
- C program to count leading zeros in a binary number.

doubt, write here. I willhelp my best.<pre><code> ----Your Source Code---- </code></pre>