function [new_bitstream, dict] = apply_dictionary(bitstream, huf_dict)

len           = length(bitstream);
[num_symb, ~] = size(huf_dict);

[symbs] = huf_dict(:,1);
symbs   = cell2mat(symbs);

bits_per_word = ceil(log2(max(symbs)));

for ith_symb = 1 : num_symb
    huf_dict{ith_symb,1} = dec2bin(symbs(ith_symb), bits_per_word);
end

new_bitstream = [];
for idx = 1 : bits_per_word : len
    cur_word = bitstream(idx:idx+bits_per_word-1);
    
    found    = false;
    ith_symb = 1;
    while found == false && ith_symb <= num_symb
        dict_word = huf_dict{ith_symb,1};
        
        if sum(xor(cur_word, dict_word)) == 0
            new_bitstream = [ new_bitstream, huf_dict{ith_symb,2} ];
            found = true;
        end
        
        ith_symb = ith_symb + 1;
    end
end

dict = huf_dict;

end
