How to check 4 inputs and set flag in Microcontroller
I would consider using a pre-initialised table, like this:

static uint8 table[] = { 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4 };

port_val = Read_Port() &amp; 0xf;
flag = table[port_val];


I'm more of an AVR hacker myself, and don't know what the PIC equivalent would be of using PROGMEM to put the table in ROM.

And for 8 bits instead of 4:

uint8 port_val = Read_Port() &amp; 0xff;
flag = table[port_val &amp; 0xf];
if (flag == 4) flag += table[port_val &gt;&gt; 4];