Originally Posted by
ihatec++
edit: I also think i should get rid of the ++ in my username :P
No.
I'm not sure why you want to use hex values with the bit masking here, but go ahead. Here's some ideas (using decimal numbers, you can convert them yourself if you get a kick out of hex):
Code:
#include <stdio.h>
#include <string.h>
void getsuit(int n, char *suit) {
switch (n) {
case (0):
strcpy(suit,"diamonds");
break;
case (1):
strcpy(suit,"spades");
break;
case (2):
strcpy(suit,"clubs");
break;
case (3):
strcpy(suit,"hearts");
break;
default: break;
}
}
int main() {
unsigned bitflags = 0,
suitmask, valuemask, colorbit = 1<<6;
char suit[16];
suitmask = ~(~0U<<2);
valuemask = ~((~0U<<6)+3);
/* set hearts */
bitflags ^= 3;
bitflags ^= (1<<6);
/* set jack */
bitflags ^= (11<<2);
/* show */
getsuit(bitflags&suitmask,suit);
printf ("Suit: %s\nValue: %d\nColor: %s\n",
suit, (bitflags&valuemask)>>2, bitflags&colorbit ? "red" : "black");
return 0;
}
I'm presuming you understand bitshifting which is why you want to do this. Anyway, ~ is "complement of", 0U is all zero, so ~0U is all ones...