function y = bit2byte(x)
% y = bit2byte(x)
% Transform a number of bits into a number of bytes.

x = double(x);

x_n = numel(x);
if rem(x_n,8)
    x = [x zeros(1,8-rem(x_n,8))];
end
y = zeros(1,ceil(x_n/8));
y_index = 1;
v = 2.^(0:7);
for i=1:8:x_n
    y(y_index) = sum(x(i+7:-1:i).*v);
    y_index = y_index + 1;
end
y = uint8(y);
