Vindictive Vindictive - 1 month ago 6
C++ Question

Is there a way to initialize a char using bits?

I'm trying to represent the 52 cards in a deck of playing cards.
I need a total of 6 bits; 2 for the suit and 4 for the rank.
I thought I would use a char and have the first 2 bits be zero since I don't need them. The problem is I don't know if there's a way to initialize a char using bits.

For example, I'd like to do is:

char aceOfSpades = 00000000;

char queenOfHearts = 00011101;

I know once I've initialized char I can manipulate the bits but it would be easier if I could initialize it from the beginning as shown in my example. Thanks in advance!

Answer

Yes you can:

example,

  char aceOfSpades = 0b00000000;
  char queenOfHearts = 0b00011101;
Comments