% index_to_binary.m % Takes a list of index values and converts them to [length, value] lists. % usage: % index = bitstring_to_index( [0 0 0 0 1] ); % [bitlength, value] = index_to_binary(index) % returns % length = 5 % value = 1 % % index_to_binary() and binary_to_index() convert % back and forth freely, like % bitstring_to_index() and index_to_bitstring(). For example, % [bitlength, value] = index_to_binary( 1:10 ); % index = binary_to_index( bit_length, value ) % returns % index = [ 1 2 3 4 5 6 7 8 9 10 ] % % See also BINARY_TO_INDEX, % INDEX_TO_BITSTRING, PRINT_BITSTRINGS, BITSTRING_TO_INDEX. % Change log: % 1999-06-30:DAV: David Cary started. function [length,value] = index_to_binary(index) % IEEE754 double precision can handle integers exactly % up to (but not including) 2^53+1. See BITMAX. if( 2^53 < max(index(:)) ) error('Sorry, this is too big for me to handle.') end; [dummy, lengthp1] = log2( index+1 ); length = lengthp1 - 1; value = (index - 2.^length) + 1; % must be done in this order to do 2^53 correctly. % end index_to_binary.m