-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexp-2-9.c
More file actions
52 lines (43 loc) · 984 Bytes
/
exp-2-9.c
File metadata and controls
52 lines (43 loc) · 984 Bytes
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
47
48
49
50
51
52
/*************************************************************************
> File Name: exp-2-9.c
> Author: xiaoxiaoh
> Mail: xiaoxxhao@gmail.com
> Created Time: Wed Jun 28 15:40:22 2017
************************************************************************/
/*
* Write a C program to count trailing zeros in a binary number.
*
* Example:
*
* Input any number: 48
*
* Output trailing zeros: 4
*
*/
#include <stdio.h>
#define INT_SIZE sizeof(int) * 8 //Integer size in bits
int main()
{
int num, i, count=0;
/* Read a number from user */
printf("Enter a number: ");
scanf("%d", &num);
/* Iterate over each bit of the number */
for(i=0; i<INT_SIZE; i++)
{
//if a set is found, then break
if((num >> i) & 1)
break; //no need to run further
count++;
}
/*
* method 2:
* while(!(num & 1))
* {
* num = num >> 1;
* count++;
* }
*/
printf("The count trainling zeros of bits in %d is %d.\n", num, count);
return 0;
}