cmn_dec2bin.m 1.46 KB
Newer Older
Ben Wojtowicz's avatar
Ben Wojtowicz committed
1
%
2
% Copyright 2011-2012 Ben Wojtowicz
Ben Wojtowicz's avatar
Ben Wojtowicz committed
3 4
%
%    This program is free software: you can redistribute it and/or modify
5
%    it under the terms of the GNU Affero General Public License as published by
Ben Wojtowicz's avatar
Ben Wojtowicz committed
6 7 8 9 10 11
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
%    GNU Affero General Public License for more details.
Ben Wojtowicz's avatar
Ben Wojtowicz committed
13
%
14
%    You should have received a copy of the GNU Affero General Public License
Ben Wojtowicz's avatar
Ben Wojtowicz committed
15 16 17 18 19 20 21 22 23 24 25 26
%    along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
% Function:    cmn_dec2bin
% Description: Converts an array of decimal numbers
%              to an array of binary arrays
% Inputs:      dec      - Array of decimal numbers
%              num_bits - Number of bits per decimal
%                         number
% Outputs:     array    - Array of binary arrays
% Spec:        N/A
% Notes:       None
% Rev History: Ben Wojtowicz 11/22/2011 Created
27
%              Ben Wojtowicz 01/29/2012 Fixed license statement
Ben Wojtowicz's avatar
Ben Wojtowicz committed
28 29 30 31 32 33 34 35 36 37 38 39
%
function [array] = cmn_dec2bin(dec, num_bits)
    [junk, num_dec] = size(dec);

    for(n=1:num_dec)
        tmp = dec(n);
        for(m=num_bits-1:-1:0)
            array(n,num_bits-m) = floor(tmp/2^m);
            tmp                 = tmp - floor(tmp/2^m)*2^m;
        endfor
    endfor
endfunction