Example 97: Pack Information in Bits

Learn how to determine the species of an animal.

We'll cover the following

Problem

An animal could be a canine (dog, wolf, fox, etc.), a feline (cat, lynx, jaguar, etc.), a cetacean (whale, narwhal, etc.), or a marsupial (koala, wombat, etc.).

When a particular animal is canine, feline, cetacean, or marsupial, the information is stored in bit numbers 0, 1, 2, and 3, respectively. The information is of an integer variable called type. Bit number 4 of the variable type stores the information about the animal being either a carnivore or herbivore.

For the following animal, complete the program to determine whether the animal is a herbivore or a carnivore. Also, determine whether the animal is a canine, feline, cetacean, or marsupial.

Try it yourself

Try to solve this question on your own in the code widget below. If you get stuck, you can always refer to the solution provided.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.