It is very simple to determine the number of bits of an integer number:
Algorithm:
1. take input x, initialize countbit : = 0
2. check x is zero, yes then go to step 5
3. x: = Shift one bit left of x,countbit:=countbit+1;
4. go to step 2.
5. End
Algorithm:
1. take input x, initialize countbit : = 0
2. check x is zero, yes then go to step 5
3. x: = Shift one bit left of x,countbit:=countbit+1;
4. go to step 2.
5. End
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> int main() { int n,tn,cb=0,r=0; scanf ("%d",&n); while(n!=0){ cb++; n = n>>1; } printf("Length: %d\n",cb); return 0; } |
Sample Output: 12 Length: 4
No comments:
Post a Comment