Bit order using PortIn? Mystery!

16 Nov 2016

Weird! Using

PortIn(Port0,0x7878000)

, which is

 (p18,p17,p16,p15,p11,p12,p14,p13)

, I read the portIn value. I am extremely confused about the bit order of the received byte. Using this code to print every bit:

pc.printf(..., s,s&0x80,s&0x40,s&0x20,s&0x10,s&0x8,s&0x4,s&0x2,s&0x1)

, I see that, when pin input is HIGH, the value of the bits is:

 (8,4,2,1,256,512,1024,2048)

What is the correct order to read one byte from PortIn?

PS I' ve found a function to read a byte from PortIn, but seems not to work and is even more confusing:

 int top = s >> 19;         // Isolate the top nibble
        int middle = s >> 2;       // Isolate bits 2 & 3
        s = s & 0x00000003;   // Isolate bits 0 & 1      
        s += middle;
        s += top;

So what the hell is going on here? THANKS everybody!!