Skip to content

Arduino Bitwise Operators and advanced tricks

Here is the detailed code :

 

//ENCODE STATUS
unsigned char NewStatus(unsigned char table, unsigned char pos, unsigned cards){
  table = table-1; //convert table 1,2,3,4,5,6  starting from 0 (0,1,2,3,4,5)
  table = table & B00000111;
  table = table<<5;

  pos = pos-1; //same as table
  pos = pos & B00000011;
  pos = pos<<3;

  cards = cards-1;//same as table
  cards = cards & B00000111;

  return table+pos+cards;
}

//DECODE STATUS
unsigned char GetTable(unsigned char &PlayerStatus){
  unsigned char table;
  table=PlayerStatus;
  table=table & B11100000;
  table=table>>5;
  table=table+1; //convert from 0,1,2... to 1,2,3...
  return table;
}

unsigned char GetPosition(unsigned char &PlayerStatus){
  unsigned char pos;
  pos=PlayerStatus;
  pos=pos & B00011000;
  pos=pos>>3;
  pos=pos+1; //same as table...
  return pos;
}

unsigned char GetCards(unsigned char &PlayerStatus){  
  unsigned char cards;
  cards=PlayerStatus;
  cards=cards & B00000111;
  cards=cards+1; //same as table...
  return cards;
}

void PrintStatus(unsigned char &PlayerStatus){
  unsigned char table;
  table=GetTable(PlayerStatus);

  unsigned char pos;
  pos=GetPosition(PlayerStatus);

  unsigned cards;
  cards=GetCards(PlayerStatus);

  Serial.print("Table : ");
  Serial.print(table,DEC);
  Serial.print(" ,Position : ");
  Serial.print(pos,DEC);
  Serial.print(" ,Cards : ");
  Serial.println(cards,DEC);
}

void setup()                      // the set up part (runs only once)
{
  Serial.begin(9600);             // set up Serial library at 9600 bps  

  //Assign status at john, mary and bob
  unsigned char JohnBlackjackStatus = NewStatus(2,3,1);
  //John sits at 2nd table (1,2,3,4,5,6 tables) at 3rd position (1,2,3,4)
  //with 1 card in hand (1,2,3,4,5,6,7,8)
  unsigned char MaryBlackjackStatus = NewStatus(1,2,3);
  //Mary sits at 1st table (1,2,3,4,5,6 tables) at 2nd position (1,2,3,4)
  //with 3 cards in hand (1,2,3,4,5,6,7,8)
  unsigned char BobBlackjackStatus = NewStatus(5,3,5);
  //Bob sits at 5th table (1,2,3,4,5,6 tables) at 3rd position (1,2,3,4)
  //with 5 cards in hand (1,2,3,4,5,6,7,8)

  //Print status
  Serial.println("John's status : ");
  PrintStatus(JohnBlackjackStatus);
  Serial.println("Mary's status : ");
  PrintStatus(MaryBlackjackStatus);
  Serial.println("Bob's status : ");
  PrintStatus(BobBlackjackStatus);

  Serial.println("It is Bob's turn. Bob thinks ...");
  delay(3000);

  Serial.println("Bob decides to hit (take a card)");
  BobBlackjackStatus = NewStatus( GetTable(BobBlackjackStatus),
                                  GetPosition(BobBlackjackStatus),
                                  GetCards(BobBlackjackStatus) + 1 ); 

  Serial.println("Bob's new status : ");
  PrintStatus(BobBlackjackStatus);

}

void loop()                       // The main loop (runs over and over again)
{
                                  // do nothing...
}

 

 

Here you can download the source code for this demo : BitwiseExample

I know that some of you might find this article a bit difficult but if you reread it from the beginning i am pretty sure you should understand everything.

You can see below some reference links :
http://arduino.cc/en/Reference/BitwiseAnd

http://www.arduino.cc/playground/Code/BitMath

http://graphics.stanford.edu/~seander/bithacks.html