001: // example of bitwise status flag
002: #include<stdio.h>
003: #include<stdlib.h>
004: 
005: // we want to "pack" in a single "flag" your status
006: // as pet owner
007: 
008: #define HAS_NONE              0x000
009: #define HAS_CAT               0x001
010: #define HAS_DOG               0x002
011: #define HAS_GUINEA            0x004
012: #define HAS_PIGEON            0x008
013: #define HAS_FERRET            0x010
014: #define HAS_GOLDFISH          0x020
015: #define HAS_CANARY            0x040
016: #define HAS_HEDGEHOG          0x080
017: #define HAS_RABBIT            0x100
018: // ...
019: 
020: void stampabinario(unsigned short);
021: 
022: int main(int argc, char **argv){
023: 
024:   // just init your config flag
025:   unsigned short s = 0;
026:   char answer;
027: 
028:   printf("Do you own a cat? (y/n) ");
029:   scanf(" %c", &answer);
030:   if(answer == 'y' || answer == 'Y')
031:     s = s | HAS_CAT;
032: 
033:   printf("Do you own a dog? (y/n) ");
034:   scanf(" %c", &answer);
035:   if(answer == 'y' || answer == 'Y')
036:     s = s | HAS_DOG;
037: 
038: 
039:   printf("Do you own a guinea pig? (y/n) ");
040:   scanf(" %c", &answer);
041:   if(answer == 'y' || answer == 'Y')
042:     s = s | HAS_GUINEA;
043: 
044: 
045:   printf("Do you own a pigeon? (y/n) ");
046:   scanf(" %c", &answer);
047:   if(answer == 'y' || answer == 'Y')
048:     s = s | HAS_PIGEON;
049: 
050: 
051:   printf("Do you own a ferret? (y/n) ");
052:   scanf(" %c", &answer);
053:   if(answer == 'y' || answer == 'Y')
054:     s = s | HAS_FERRET;
055: 
056: 
057:   printf("Do you own a goldfish? (y/n) ");
058:   scanf(" %c", &answer);
059:   if(answer == 'y' || answer == 'Y')
060:     s = s | HAS_GOLDFISH;
061: 
062: 
063:   printf("Do you own a canary? (y/n) ");
064:   scanf(" %c", &answer);
065:   if(answer == 'y' || answer == 'Y')
066:     s = s | HAS_CANARY;
067: 
068: 
069:   printf("Do you own a hedgedog? (y/n) ");
070:   scanf(" %c", &answer);
071:   if(answer == 'y' || answer == 'Y')
072:     s = s | HAS_HEDGEHOG;
073: 
074:   printf("Do you own a rabbit? (y/n) ");
075:   scanf(" %c", &answer);
076:   if(answer == 'y' || answer == 'Y')
077:     s = s | HAS_RABBIT;
078: 
079:   printf("Your status has pet owner is %u ", s);
080:   stampabinario(s);
081:   printf("\n");
082: 
083: 
084:   return 0;
085: }
086: 
087: void stampabinario(unsigned short x)
088: {
089:   // questa funzione stampa i 16 bit di uno short
090:   // non conosciamo ancora il controllo di flusso, quindi e' scritta
091:   // in maniera non particolarmente efficiente
092: 
093:   // per stampare estraggo i singoli bit portandoli nella posizione meno significativa
094:   // mediante shift a destra e poi li isolo con un and bit a bit
095: 
096:   printf("%d", x >> 15 & 1);
097:   printf("%d", x >> 14 & 1);
098:   printf("%d", x >> 13 & 1);
099:   printf("%d", x >> 12 & 1);
100:   printf("%d", x >> 11 & 1);
101:   printf("%d", x >> 10 & 1);
102:   printf("%d", x >>  9 & 1);
103:   printf("%d", x >>  8 & 1);
104:   printf(" ");
105:   printf("%d", x >>  7 & 1);
106:   printf("%d", x >>  6 & 1);
107:   printf("%d", x >>  5 & 1);
108:   printf("%d", x >>  4 & 1);
109:   printf("%d", x >>  3 & 1);
110:   printf("%d", x >>  2 & 1);
111:   printf("%d", x >>  1 & 1);
112:   printf("%d", x       & 1);
113: }
114: 
115: 


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