## Refering to a bit by bit number

If we have a valueXwe can identify each bit withinXasX_{j}We could either identify each bit within a byte as:

Or as:

right to left X_{7}X_{6}X_{5}X_{4}X_{3}X_{2}X_{1}X_{0}The right to left representation is prefered however because by selecting the bit numbers of each bit within

left to right X_{0}X_{1}X_{2}X_{3}X_{4}X_{5}X_{6}X_{7}Xto start at0and increasing from right to left, each bitXhas the value_{j}2(where^{n}nis equal toj) (see a more detailed description of binary).i.e.

Xhas the value_{0}1which is the same as2^{0}

Xhas the value_{1}2which is the same as2^{1}

Xhas the value_{2}4which is the same as2^{2}

Xhas the value_{3}8which is the same as2^{3}The XCSB expression

(1 << j)has the same effect as raising2to the (integer) power ofjUsefully, the PIC microcontroller has native instructions that use the right to left representation for selecting bits within bytes.

If we assign the value

00100101toXwe can pick out specific bits asSo

X_{7}X_{6}X_{5}X_{4}X_{3}X_{2}X_{1}X_{0}00100101bitTo generate a value with a specific bit set to7ofX(shown asX) has the value_{7}0

bit5ofX(shown asX) has the value_{5}1

bit1ofX(shown asX) has the value_{1}0

bit0ofX(shown asX) has the value_{0}11, we would find the bit number of the bit we are interested in (call thisj) and calculate a mask pushing the bit left until it is in the required bit position.e.g. calculating a mask with only bit

Xset_{3}Using a traditional BASIC we could achive the above using

X_{7}X_{6}X_{5}X_{4}X_{3}X_{2}X_{1}X_{0}start 00000001shift left 0 00000001shift left 1 00000010shift left 2 00000100shift left 3 00001000finish 00001000In XCSB however we would use the left shift operatorlet X = 1 for J=1 to 3 X = X * 2 next J<<and the expression:to calculate the above mask of(1 << 3)X_{3}In general if we want to calculate a mask for bit

Xwe would use the expression:_{j}XCSB actualy trys very hard to convert bit manipulation expressions into single native PIC instructions.(1 << j)e.g. the XCSB expressionis converted into the PIC instructionX = X | (1 << 3)and the XCSB expressionbsf X, 3is converted into the PIC instructionX = X & ~(1 << 3)bcf X, 3## using a bit mask

Having calculated a bit mask we can use it toset,clearortesta bit withing a variable## set bit

To

seta bit we would use the binaryORoperatorLooking step by step at the computation we seee.g. X = 0x21 J = 2 X = X | (1 << J)For definition of

X_{7}X_{6}X_{5}X_{4}X_{3}X_{2}X_{1}X_{0}E_{7}E_{6}E_{5}E_{4}E_{3}E_{2}E_{1}E_{0}X = 0x21 00100001E = (1 << J) 00000100X = X | E 00100101xX,_{7}X,_{6}Xetc., see Refering to a bit by bit number_{5}## clear bit

To

cleara bit we would use the binaryNOTandANDoperatorsLooking step by step at the computation we seee.g. X = 0x25 J = 2 X = X & ~(1 << J)For definition of

X_{7}X_{6}X_{5}X_{4}X_{3}X_{2}X_{1}X_{0}E_{7}E_{6}E_{5}E_{4}E_{3}E_{2}E_{1}E_{0}X = 0x25 00100101E = (1 << J) 00000100E = ~E 11111011X = X & E 00100001xX,_{7}X,_{6}Xetc., see Refering to a bit by bit number_{5}## test bit

To

testa bit we would use the binaryANDandNOT EQUALoperatorsLooking step by step at the computation we seee.g. X = 0x21 J = 2 T = (X & (1 << J)) != 0

X_{7}X_{6}X_{5}X_{4}X_{3}X_{2}X_{1}X_{0}E_{7}E_{6}E_{5}E_{4}E_{3}E_{2}E_{1}E_{0}T_{7}T_{6}T_{5}T_{4}T_{3}T_{2}T_{1}T_{0}X = 0x21 00100001E = (1 << J) 00000100E = X & E x00000000T = (E != 0) xxxxxxxx00000000Looking step by step at the computation we seee.g. X = 0x25 J = 2 T = (X & (1 << J)) != 0For definition of

X_{7}X_{6}X_{5}X_{4}X_{3}X_{2}X_{1}X_{0}E_{7}E_{6}E_{5}E_{4}E_{3}E_{2}E_{1}E_{0}T_{7}T_{6}T_{5}T_{4}T_{3}T_{2}T_{1}T_{0}X = 0x25 00100101E = (1 << J) 00000100E = X & E x00000100T = (E != 0) xxxxxxxx00000001X,_{7}X,_{6}Xetc., see Refering to a bit by bit number_{5}