-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path18_pos_of_first_1_in_bin.c
46 lines (39 loc) · 1011 Bytes
/
18_pos_of_first_1_in_bin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// // C program to find Position of first 1 when searching from LSB to MSB.
// // Header Files
#include <stdio.h>
#include <conio.h>
// // Main Function Start
int main()
{
int num, position = 1;
printf("\nEnter An Integer => ");
scanf("%d", &num);
int copyNum = num;
// // // // 1st Approach
// // if (copyNum)
// // {
// // while (!(num & 1))
// // {
// // num >>= 1;
// // position++;
// // }
// // printf("\nPostion of First 1 In the Binary of %d => %d\n", copyNum, position);
// // }
// // else
// // printf("\nThere is No 1 in the Binary of 0\n");
// // // // 2nd Approach
while (num)
{
if (num & 1)
break;
num >>= 1;
position++;
}
if (copyNum)
printf("\nPostion of First 1 In the Binary of %d => %d\n", copyNum, position);
else
printf("\nThere is No 1 in the Binary of 0\n");
getch();
return 0;
}
// // Main Function End