An enhanced version of 'logical AND' function 'bitand'. The new one can accept negative number. [usage] result = vb_bitand_ex(num1, num2, bit_len) [input] num1 : number #1 [1 x N] num2 : number #2 [1 x N] or [x1] bit_len : length of bit [output] result : result of logical AND (base-10) [note] convert negative to positive and use 'bitand' [history] 2007-01-09 (Sako) initial version Copyright (C) 2011, ATR All Rights Reserved. License : New BSD License(see VBMEG_LICENSE.txt)
0001 function result = vb_bitand_ex(num1, num2, bit_len) 0002 % An enhanced version of 'logical AND' function 'bitand'. 0003 % The new one can accept negative number. 0004 % [usage] 0005 % result = vb_bitand_ex(num1, num2, bit_len) 0006 % [input] 0007 % num1 : number #1 [1 x N] 0008 % num2 : number #2 [1 x N] or [x1] 0009 % bit_len : length of bit 0010 % [output] 0011 % result : result of logical AND (base-10) 0012 % [note] 0013 % convert negative to positive and use 'bitand' 0014 % [history] 0015 % 2007-01-09 (Sako) initial version 0016 % 0017 % Copyright (C) 2011, ATR All Rights Reserved. 0018 % License : New BSD License(see VBMEG_LICENSE.txt) 0019 0020 if any(num1 < 0) 0021 num1 = inner_convert_negative2positive(num1, bit_len); 0022 end 0023 0024 if any(num2 < 0) 0025 num2 = inner_convert_negative2positive(num2, bit_len); 0026 end 0027 0028 result = bitand(num1,num2); 0029 0030 % --- INNER FUNCTIONS -------------------------------------------------------- % 0031 % 0032 % --- inner_convert_negative2positive() 0033 % 0034 function p_num = inner_convert_negative2positive(n_num, bit_len) 0035 0036 len_num = length(n_num); 0037 p_num = zeros(1,len_num); 0038 for i_num = 1:len_num 0039 if n_num(i_num) >= 0 0040 p_num(i_num) = n_num(i_num); 0041 else 0042 p_num(i_num) = n_num(i_num) + 2^bit_len; 0043 end 0044 end 0045 0046 % p_num = n_num + 2^bit_len; 0047 % 0048 % --- end of inner_convert_negative2positive() 0049 0050 %%% END OF FILE %%%