luc100.c (966B)
1 /* Determine if an animal is Carnivore/Herbivore and its type (Canine, Feline, Cetacean, Marsupial) based on bits in an integer. 2 */ 3 /* Let Us C, Chap- 21 (Operations on Bits), Qn No.: A(b) */ 4 5 /* This file is auto-generated by a bot. */ 6 /* This code is not compiled; it is for reference only. */ 7 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 12 struct animal 13 { 14 char name[30]; 15 int type; 16 }; 17 18 int main() 19 { 20 struct animal a = {"OCELOT", 18}; 21 int type = a.type; 22 23 printf("Animal: %s\n", a.name); 24 25 // Check Bit 4 for Diet (Assuming 1=Carnivore, 0=Herbivore based on context) 26 if (type & (1 << 4)) 27 printf("Diet: Carnivore\n"); 28 else 29 printf("Diet: Herbivore\n"); 30 31 // Check Bits 0-3 for Family 32 printf("Family: "); 33 if (type & (1 << 0)) printf("Canine "); 34 if (type & (1 << 1)) printf("Feline "); 35 if (type & (1 << 2)) printf("Cetacean "); 36 if (type & (1 << 3)) printf("Marsupial "); 37 printf("\n"); 38 39 return 0; 40 }