I am working on a PIC18f4520. To do my test, I have connected a led to the pic. The following items are the steps I would like to do :

  • Save the value of the port set for the LED (which blinks according to the ISR subroutine I wrote, and can be look at in this case as a clock signal). For this I did :

    unsigned char portValue;   // initialization
    TRISBbits.TRISB5 = 0;      // LED on RB5 set as output
    portValue = PORTBbits.RB5; // operation in the main while loop  
  • Left shift the bits of portValue by one but with the insertion of the RB5 value. To illustrate :

    --> if RB5 = 1, I would have portValue = 0b0000 0001 
    --> at t+1, RB5 = 0 then portValue = 0b0000 0010
    --> at t+2, RB5 = 1 then portValue = 0b0000 0101
    --> and so on (RB5 taking 1 or 0 and no other value)

With what I have wrote so far I can only do a left shifting. But I can't find a way to "add" the RB5...

Does something like

portValue = portValue<<(&PORTBbits.RB5); 

could work ? Bit shifting operations are quite new to me so my questions can be odd.

Anyhow, thanks in advance !

  • 1
    \$\begingroup\$ On the 18F you should use LAT not PORT when writing. See: electronics.stackexchange.com/questions/28750/… \$\endgroup\$
    – David
    Jun 1, 2016 at 17:05
  • \$\begingroup\$ Fantastic guys ! Thanks a lot !! Your explanations are clear and I understand better how the bit shifting works now. Both solution work perfectly. I am going to push forward my code and I'll see which one is more efficient for my application. \$\endgroup\$
    – Daymov
    Jun 2, 2016 at 8:25

2 Answers 2


Bitshifting works like this:

result = value << amount

this shifts the bits of the variable value by amount of bits to the left. Use >> for right shifting.

So before you store your new bit, you want do shift portValue by one bit, like this:

portValue = portValue << 1;

Now the first bit will be 0 by default and you can store your new bit there like this:

portValue = portValue | PORTBbits.RB5;

This assumes that the variable PORTBbits.RB5 will only have the values 0 or 1 so every other bit except bit 0 is always 0. To make sure of this you can mask PORTBbits.RB5 with 0x01 like this:

portValue = portValue | (PORTBbits.RB5 & 0x01);

This will make sure that only the first bit will be changed.

This can now be combined in one line if you like:

portValue = (portValue << 1 ) | (PORTBbits.RB5 & 0x01);
portValue <<= 1; //left shift every time round
portValue |= (PORTBbits.RB5) ? 1 : 0; //set LSB of portValue if RB5 is true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.