# port masking and ports in pic

i have data at portb on microprocessor (pic), i need to compress three bits rb3,rb4, and rb5, the other pins are various IO on port b, i wish to compress that info held in those three pins on port b, ignoring all others on port b, and present it as two bits on port A, pins RA3, RA4.

I just dont know how to apply bitmasks etc in this problem, i trust that is even the correct approach?

• So you need to compress 3 bits to 2 bits? You mean you want to represent 7 different combinations in two bits? I guess you mean you have a truth table somewhere, or a logical relationship? Mar 13, 2015 at 8:04

If you want to get a byte with RB3, RB4 and RB5 in the lower 3 bits (i.e. RB5 is the LSB), do this:

unsigned char uByte = ((RB3 << 2) | (RB4 << 1) | RB5);


Then do your compression, so assuming the lower two bits of uByte need to go to RA3 and RA4 (i.e. RA4 is the LSB), and set the PORTA bits like this:

RA3 = ((uByte >> 1) & 0x1);
RA4 = (uByte & 0x1);

• thanks Roger, will have to study whats going on there, i will try to implement that. Mar 13, 2015 at 8:20
• think i need to make RB5 the MSB? that would be normal convention,,, or am i missing a clever programming trick here, ? Mar 13, 2015 at 8:24
• @StevieB There's no clever trick, I'm just shifting the bits to some required order. Just replace RB3/4/5 in the order you need (MSB to LSB in the lower 3 bits of the byte). If you want to shift all the way to bit 7 of uByte, adjust the shift amounts - e.g. ((RB5 << 7) | (RB4 << 6) | (RB3 << 5)). Mar 13, 2015 at 8:25
• @Downvoter - please explain what you think is wrong with my answer so I can improve it. Mar 24, 2015 at 16:10