Although I know my powers of two up to 262144, I prefer to use hexadecimal to represent bit masks in something like the above:
Code:
printf("Engine is %s\n", (val & 0x08) ? "on" : "off");
printf("Navigation system is %s\n", (val & 0x10) ? "on" : "off");
printf("Thrusters are %s\n", (val & 0x20) ? "on" : "off");
A nicer way that enables you to somewhat number the bits is to use a rightshift:
Code:
printf("Communication systems are %s\n", ((val >> 6) & 1) ? "on" : "off");
printf("Weapons are %s\n", ((val >> 7) & 1) ? "on" : "off");
printf("World domination is %s\n", ((val >> 8) & 1) ? "on" : "off");
(Note the 6, 7, and 8, the values from 0 to 15 correspond to the various bits starting from 0=rightmost)