# More Glcd - Understanding The Code

#### GettinBetter

##### New Member
Hi Peeps,

I recently had issues with a GLCD which with your help I managed to get working.
Trouble is I've changed from a PIC16F877 to a PIC16LF18877 and the waveform on the analyser is all messed up.

My question is:
What is going on here.. with these two lines?
(For reference GlcdControlBus is defined as PORTB, with CS1 as 0x00, & CS2 as 0x01)

Code:
GlcdControlBus |= (1<<CS2);
GlcdControlBus &= ~(1<<CS1);
I know what the end result creates, but how? I tried to break each symbol down as I try to fathom it out, but I'm struggling. It only needs to set bits<1:0> of PORTB yet seems so complicated.

regards
GB

#### tumbleweed

##### Active Member
The expression '(1<<n)' creates a bitmask value from a bit number n=0 to 7 by shifting '1' left n times.

For example, with CS2 = 1 (0x01) it evaluates as:
Code:
(1<<1) = 2 = 0x02
So,
Code:
GlcdControlBus |= (1<<CS2);

// is the same as saying:
GlcdControlBus = GlcdControlBus | 0x02;    // set bit 1
Likewise, (1<<0) = 1 = 0x01, so
Code:
GlcdControlBus &= ~(1<<CS1);
GlcdControlBus = GlcdControlBus & ~(0x01)

// or
GlcdControlBus = GlcdControlBus & 0xFE;   // clear bit 0

#### GettinBetter

##### New Member
Thank you for that, and forgive my ignorance, why don't/can't I just use ....
Code:
GlcdControlBus = (0x01); // Select Page 0

GlcdControlBus = (0x02); //Select Page 1

#### tumbleweed

##### Active Member
If there are no other pins used on PORTB then you could.
It's much more common to have to read the current IO pin states, modify the pins you want, and then write them back to the PORT.

#### GettinBetter

##### New Member
If there are no other pins used on PORTB then you could.
It's much more common to have to read the current IO pin states, modify the pins you want, and then write them back to the PORT.
Ah, so does that mean that, that bit of code (i.e. "&= ~(1<<CS1);" ) only changes a single bit at a time?

#### tumbleweed

##### Active Member
Yes.

If you look back at post #2 again you'll see that the two expressions either set bit 1 or clear bit 0 (and leave all the other bits as they are).

#### GettinBetter

##### New Member
Cool, That's handy to know, and very useful.
Can't thank you enough for taking the time to explain.