You're storing your bitstream as a logical array. In that case, it's a logical operation that you want to perform, not a bitwise xor. Therefore use xor instead of bitxor.
Compare
n1 = 8; n2=32
n1logicalbit = dec2bin(n1, 8) - '0';
n2logicalbit = dec2bin(n2, 8) - '0';
logicalxor = xor(n1logicalbit, n2logicalbit);
binaryxor = bitxor(n1, n2);
isequal(logicalxor, dec2bin(binaryxor, 8)-'0')
Otherwise your code is fine. There's a few unnecessary transpositions, you could simply do:
bytelist = fread(fid, '*uint8');
bitexpanded = dec2bin(bytelist, 8) -'0';
I would also move the fclose to just after you've done the fread. This way if your code stops further down because of an error or the user interrupting it, the file won't be left open.
Best Answer