-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion11.c
32 lines (25 loc) · 841 Bytes
/
question11.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
/*
1. Write a C function to find whether a positive integer is a power of two, using
a function "void is_power_of_two (int number)".
Hint: use the shift operator, and iterate over each bit. DO NOT COMPARE WITH SUCCESIVE
POWERS OF TWO.
Input: A positive integer.
Output : "yes" if the number is power of two, else "no". [2]
http://stackoverflow.com/questions/1053582/how-does-this-bitwise-operation-check-for-a-power-of-2
*/
#include <stdio.h>
void is_power_of_two (unsigned int x){
while (((x & 1) == 0) && x > 1) /* While x is even and > 1 */
x >>= 1;
if (x == 1){
printf("the number is a power of two\n");
}else{
printf("the number is not a power of two\n");
}
}
int main(){
int num, remainder, binary = 0, i = 1;
printf("Enter a number\n");
scanf("%d", &num);
is_power_of_two(num);
}