MATLAB: How does fread read the ubit*N source

freadfread orderfread sourceMATLAB

As I understand, characters from a file must be read in multiples of 8 bits (one byte). The following code:
a = fread(fd_rb,1,'ubit4');
b = fread(fd_rb,1,'ubit4');
Claims to read 4 bits and for bits. Of course, I expect it to read a whole byte into a buffer then assign the upper 4 bits to a and the lower 4 bits to b. Oddly enough, the following code yields the same result:
tmp = fread(fd_rb,1,'uchar'); %Read the whole byte
a = bitand(tmp, hex2dec('f')); %Keep 4 lower bits
b = bitshift(tmp, -4); %Keep 4 higher bits
So apparently, the high bits end up in b and the low ones in a. Why is it like this?

Best Answer

Nevermind, I just found out the answer. As the documentation specifies, the order for reading bits when using the (u)bit*N source types depends on the endianness of the host system. Being little endian, the first read reads bits 0:3, corresponding to the low values and the second one 4:7, corresponding to the high values, as opposed as one would expect if it read from a bit array where [byte0, byte1] expands as [bit0(7-1), bit1(7-1)].