01: // quick check for power of 2 using bitwise operators
02: #include<stdio.h>
03: #include<stdlib.h>
04: 
05: void stampabinario(int x);
06: 
07: int main(int argc, char **argv){
08: 
09:   int x;
10:   printf("Enter a number: ");
11:   scanf("%d", &x);
12: 
13:   printf("The number you entered in binary format is ");
14:   stampabinario(x);
15:   printf("\n");
16: 
17:   // the approach exploits the property of powers of 2. In binary format they 
18:   // have only one bit set to 1. If you subract them 1 you need a borrow, namely
19:   // the result has all bits 1 up to the original 1 as example
20:   // 1024     in binary format is 10000000000
21:   // 1024 - 1 in binary format is 01111111111
22:   // obviously this is true for positive numbers only! 
23:   if(x > 0 && !(x & (x - 1)))
24:     printf("The number you entered is a power of 2\n");
25:   else
26:     printf("The number you entered is NOT a power of 2\n");
27: 
28:   // try some shift
29:   for(int i = 0; i < 5; ++i)
30:   {
31:     printf("%d left  shifted by %d positions is %8d (", x, i , x << i);
32:     stampabinario(x << i);
33:     printf(")\n");
34:   }
35: 
36:   printf("********\n");
37: 
38:   for(int i = 0; i < 5; ++i)
39:   {
40:     printf("%d right shifted by %d positions is %8d (", x, i , x >> i);
41:     stampabinario(x >> i);
42:     printf(")\n");
43:   }
44: 
45:   return 0;
46: }
47: 
48: void stampabinario(int x)
49: {
50:   // questa funzione stampa i 32 bit di un int
51:   // non conosciamo ancora il controllo di flusso, quindi e' scritta
52:   // in maniera non particolarmente efficiente
53: 
54:   // per stampare estraggo i singoli bit portandoli nella posizione meno significativa
55:   // mediante shift a destra e poi li isolo con un and bit a bit
56: 
57:   printf("%d", x >> 31 & 1);
58:   printf("%d", x >> 30 & 1);
59:   printf("%d", x >> 29 & 1);
60:   printf("%d", x >> 28 & 1);
61:   printf("%d", x >> 27 & 1);
62:   printf("%d", x >> 26 & 1);
63:   printf("%d", x >> 25 & 1);
64:   printf("%d", x >> 24 & 1);
65:   printf(" ");
66:   printf("%d", x >> 23 & 1);
67:   printf("%d", x >> 22 & 1);
68:   printf("%d", x >> 21 & 1);
69:   printf("%d", x >> 20 & 1);
70:   printf("%d", x >> 19 & 1);
71:   printf("%d", x >> 18 & 1);
72:   printf("%d", x >> 17 & 1);
73:   printf("%d", x >> 16 & 1);
74:   printf(" ");
75:   printf("%d", x >> 15 & 1);
76:   printf("%d", x >> 14 & 1);
77:   printf("%d", x >> 13 & 1);
78:   printf("%d", x >> 12 & 1);
79:   printf("%d", x >> 11 & 1);
80:   printf("%d", x >> 10 & 1);
81:   printf("%d", x >>  9 & 1);
82:   printf("%d", x >>  8 & 1);
83:   printf(" ");
84:   printf("%d", x >>  7 & 1);
85:   printf("%d", x >>  6 & 1);
86:   printf("%d", x >>  5 & 1);
87:   printf("%d", x >>  4 & 1);
88:   printf("%d", x >>  3 & 1);
89:   printf("%d", x >>  2 & 1);
90:   printf("%d", x >>  1 & 1);
91:   printf("%d", x       & 1);
92: }
93: 
94: 
95: 


Se avete commenti o osservaƶioni su questa pagina
mandate un messaggio di posta elettronica a bertoƶƶi@ce.unipr.it