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