Skip to content

Arduino Bitwise Operators and advanced tricks

From the article Binary, decimal and hex numbers we have seen that

baselength -1 = maximum number in base 10.

So in binary case we get 2length-1=max10

So with 1bit we can reach 21-1=110 (so we have at max 2 elements 0 and 1)
with 2bits we can reach 22-1=310 (so we have at max 4 elements 0,1,2,3)

with 3bits we can reach 23-1=710 (so we have at max 8 elements 0,1,2,3,4,5,6,7)

with 4bits we can reach 24-1=1510 (so we have at max 16 elements 0,1,2...14,15)

with 5bits we can reach 25-1=3110 (so we have at max 32 elements 0,1,2...30,31)

with 6bits we can reach 26-1=6310 (so we have at max 64 elements 0,1,2...62,63)

with 7bits we can reach 27-1=12710 (so we have at max 128 elements 0,1,2...126,127)

with 8bits we can reach 28-1=25510 (so we have at max 256 elements 0,1,2...254,255)

and so on...

Let's see better our specs. Tables must be at most 6. The positions are 4 at most and the cards must be at most 8.
6 tables can be covered with 3 bits
4 positions can be covered by 2 bits
8 cards can be covered by 3 bits

So in total we need 3+2+3=8bits = 1 byte .

Only a byte ? How can we store three different values in a single byte ?
The asnwer is using the bitwise operators !

A BlackjackPlayerStatus can be an unsigned char (a single byte) with this internal format

XXX YY ZZZ


Where XXX are the 3 bits needed for the table, YY the two bits needed for the position and  ZZZ the three bits needed for the number of cards.

In order to write and read our data we must construct some encode / decode functions.
Let's start with the encoding (writing a new byte from 3 single bytes table, position and cards)

The table byte has only the last 3 bits significant. The other ones should be zero (00000XXX).
With this in mind we have to do a masking operation. The masking is done with the bitwise and.
So XXXXXXXX & 00000111 = 00000XXX.
After this step we have to push at the top the three X bits. This can be done using the << operator
So 00000XXX << 5 = XXX00000

The position need two bits so we do a 2 bit masking
So YYYYYYYY & 00000011 = 000000YY.
The YY are located at the 4rth and 5th place so we need to move left 3 places
So 000000YY << 3 = 000YY000

The card need three bits so we do a masking using the bitwise and.
So ZZZZZZZZ & 00000111 = 00000ZZZ.
As you can see this time the bits are located to the correct positions

Now by adding these three results we get :  XXX00000 + 000YY000 + 00000ZZZ = XXXYYZZZ

The decoding is the same.
Lets decode the table from XXXYYZZZ
The first step is to mask the bits we want so we do XXXYYZZZ & 11100000 and we get XXX00000
Now we move these three bits in the correct right position XXX00000 >> 5 = 00000XXX
As you can see the byte 00000XXX contains the information of the table

Lets decode the position
With the mask method we do XXXYYZZZ & 00011000 and we get 000YY000
Then 000YY000 >> 3 = 000000YY

Lastly we decode the cards
XXXYYZZZ & 00000111 = 00000ZZZ
As we can see we get the cards byte with only one step this time !

In the next page we will see the detailed code ...