📄 perform_arith_fixed.sci.svn-base
字号:
//// -Inputs :// x : a scalar/vector/matix of positives reals// n : an integer// -Output :// y : a vector of strings (positives)//// =============================================================================function y=dec2bin(x,n) rhs = argn(2); // check the number of input arguments if rhs<1 or rhs>2 then error(); end // check type and size of the input arguments if or(type(x)<>8) & (or(type(x)<>1) | or(x<0)) then error(); end if rhs==2 & ((type(n)<>8 & (type(n)<>1 | n<0)) | prod(size(n))<>1) then error(); end // empty matrix if x==[] y=string([]); return; end [nr,nc] = size(x); x=x(:); // input argument is a scalar/vector/matrix of zeros if and(x==0) if rhs==2 y = strcat(string(zeros(1:n))) + emptystr(nr,nc); else y = "0" + emptystr(nr,nc); end return end // for x=25, pow=[4 3 0], because x=2^4+2^3+2^0 // for x=23, pow=[4 2 1 0] because x=2^4+2^2+2^1+2^0 // for x=[25 23] // pow=[4 3 0 -1 // 4 2 1 0]; while find(x>0)<>[] pow(x>0,$+1) = floor(log2(double(x(x>0)))); pow(x<=0,$) = -1; x(x>0) = floor(x(x>0)-2^pow(x>0,$)); end pow = pow+1; ytemp = zeros(size(pow,1),size(pow,2)); for i=1:size(ytemp,1) ind = pow(i,pow(i,:)>=1); ytemp(i,ind) = 1; end if rhs==2 for i=1:size(ytemp,1) y(i)=strcat(string([zeros(1,round(n-size(ytemp,2))) ytemp(i,size(ytemp,2):-1:1)])); end else for i=1:size(ytemp,1) y(i)=strcat(string(ytemp(i,size(ytemp,2):-1:1))); end end y = matrix(y,nr,nc); endfunction// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab// Copyright (C) ???? - INRIA - Farid BELAHCENE// Copyright (C) 2008 - INRIA - Pierre MARECHAL// // This file must be used under the terms of the CeCILL.// This source file is licensed as described in the file COPYING, which// you should have received as part of this distribution. The terms// are also available at// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txtfunction y = bitcmp(x,n) // BITCMP function // // Given an unsigned integer x, this function returns the unsigned integer // which is the integer corresponding to the complementary of the binary // form of x // // If the bits number of the x binary representation is less than the // bitmax number (8,16 or 32) then the bits '1' are added to the // complementary in order to have bitmax number (8, 16 or 32) for the // complementary // // for example for the type uint8 (bitmax=8), the complementary of '1101' is not '0010' but '11110010' // The integer n sets the bits max number // -Inputs : // x : an unsigned integer // n : a positive integer between 1 and the bitmax of the x type // // -Output : // y : an unsigned integer // // P. Marechal, 5 Feb 2008 // - Add argument check // Check input arguments // ========================================================================= rhs = argn(2); // check number input argument rhs = argn(2); if (type(x) == 1) & (rhs == 1) then error('Problem'); elseif rhs == 0 then error('Problem'); end // check type if (type(x)==1 & (x-floor(x)<>0 | x<0)) .. | (type(x)==8 & (inttype(x)<10)) .. | (type(x)<>1 & type(x)<>8) then error('Problem'); end if (rhs == 2) & ( .. (type(n)==1 & (n-floor(n)<>0 | x<0)) .. | (type(n)==8 & (inttype(n)<10)) .. | (type(n)<>1 & type(n)<>8) .. | (size(n,"*")<>1) ) then error('Problem'); end // check n value if rhs>1 then select inttype(x) case 0 then nmax = 52; case 11 then nmax = 8; case 12 then nmax = 16; case 14 then nmax = 32; end if (n>nmax) | (n<1) then error('Problem'); end else n = nmax; end // Algorithm // ========================================================================= // empty matrix shortcut if isempty(x) then y = []; return; end // unit8, uint16 and uint32 shortcut if type(x)==8 then y = ~x; if rhs > 1 then select inttype(x) case 11 then y = y & uint8( 2^n - 1); case 12 then y = y & uint16( 2^n - 1); case 14 then y = y & uint32( 2^n - 1); end end return; end n = ones(x)*n; if type(x) == 1 then a = 2^32; y_LSB = uint32( x - double(uint32(x/a)) * a ); // LSB Less Significant Bits y_MSB = uint32( x/a ); // MSB Most Significant Bits y_LSB = ~y_LSB; y_MSB = ~y_MSB; if n <= 32 then y_LSB = y_LSB & uint32( 2^n - 1); y_MSB = uint32(0); else y_MSB = y_MSB & uint32( 2^(n-32) - 1); end y = double( a * y_MSB + y_LSB ); end endfunction// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab// Copyright (C) ???? - INRIA - Farid BELAHCENE// Copyright (C) 2008 - INRIA - Pierre MARECHAL// // This file must be used under the terms of the CeCILL.// This source file is licensed as described in the file COPYING, which// you should have received as part of this distribution. The terms// are also available at// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txtfunction y = bitget(x,pos) // BITGET function // Given an unsigned integer x, this function returns an unsigned integer // (0 or 1) which is the bit number 'pos' from the representation binary of x // if x=uint8(19) and pos=2 then bitget returns the 2th bit of the binary form of 19 ('10011') which is 1 // -Inputs : // x : an unsigned integer // pos : a positive integer between 1 and the bitmax of the x type // // -Output : // y : an unsigned integer // // F.Belahcene // P. Marechal, 5 Feb 2008 // - Add argument check // Check input arguments // ========================================================================= // check number input argument rhs = argn(2); if rhs <> 2 then error('Problem'); end // case empty matrix if isempty(x) if ~isempty(pos) & prod(size(pos))<>1 error('Problem'); else y=[] return end end // check size if (size(x,"*")>1) & (size(pos,"*")>1) & (or(size(x)<>size(pos))) then error('Problem'); end // check type if (type(x)==1 & (x-floor(x)<>0 | x<0)) .. | (type(x)==8 & (inttype(x)<10)) .. | (type(x)<>1 & type(x)<>8) then error('Problem'); end if (type(pos)==1 & (pos-floor(pos)<>0 | pos<0)) .. | (type(pos)==8 & (inttype(pos)<10)) .. | (type(pos)<>1 & type(pos)<>8) then error('Problem'); end // check pos value select inttype(x) case 0 then posmax = 52; case 11 then posmax = 8; case 12 then posmax = 16; case 14 then posmax = 32; end if (pos>posmax) | (pos<1) then error('Problem'); end // Algorithm // ========================================================================= if size(pos,"*") == 1; pos = ones(x) * pos; end if size(x,"*") == 1; x = ones(pos) * x; end if type(x)==8 then select inttype(x) case 11 then mask = uint8(2^(pos-1)); y = uint8(1 * ((x & mask) > 0)); return; case 12 then mask = uint16(2^(pos-1)); y = uint16(1 * ((x & mask) > 0)); return; case 14 then mask = uint32(2^(pos-1)); y = uint32(1 * ((x & mask) > 0)); return; end else // type == 1 a = 2^32; mask = uint32(zeros(pos)); ytemp = uint32(zeros(pos)); if or(pos<=32) then mask( pos<=32 ) = uint32( 2^(pos(pos<=32) -1 )); ytemp( pos<=32 ) = uint32( x(pos<=32) - double(uint32(x(pos<=32)/a)) * a ); // permet de r?cup?rer les 32 bits de poid faible end if or(pos>32) then mask( pos>32 ) = uint32( 2^(pos(pos>32) -32 -1 )); ytemp( pos> 32 ) = uint32( x(pos> 32)/a); // permet de r?cup?rer les 32 bits de poid fort end y = 1 * ((ytemp & mask) > 0); end endfunction// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab// Copyright (C) 2008 - INRIA - Pierre MARECHAL// // This file must be used under the terms of the CeCILL.// This source file is licensed as described in the file COPYING, which// you should have received as part of this distribution. The terms// are also available at// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txtfunction y = bitset(x,pos,v) // INRIA 2008 - Pierre MARECHAL // // BITSET function // Set bit at specified position // Check input arguments // ========================================================================= // check number input argument rhs = argn(2); if rhs < 2 then error('Error'); end // case empty matrix if isempty(x) if ~isempty(pos) & prod(size(pos))<>1 error('Error'); else y=[] return end end // check size if (size(x,"*")>1) & (size(pos,"*")>1) & (or(size(x)<>size(pos))) then error('Error'); end // check type if (type(x)==1 & (x-floor(x)<>0 | x<0)) .. | (type(x)==8 & (inttype(x)<10)) .. | (type(x)<>1 & type(x)<>8) then error('Error'); end if (type(pos)==1 & (pos-floor(pos)<>0 | pos<0)) .. | (type(pos)==8 & (inttype(pos)<10)) .. | (type(pos)<>1 & type(pos)<>8) then error('Error'); end // check pos value select inttype(x) case 0 then posmax = 52; case 11 then posmax = 8; case 12 then posmax = 16; case 14 then posmax = 32; end if (pos>posmax) | (pos<1) then error('Error'); end // check v value if rhs == 3 & .. ( ((type(v)<>1) & (type(v)<>8)) .. | ((type(x)==8) & (inttype(x)<10)) .. | ((v<>0) & (v<>1)) ) then error('Error'); end // Algorithm // ========================================================================= if size(pos,"*") == 1; pos = ones(x)*pos; end if size(x,"*") == 1; x = ones(pos)*x; end if rhs<3 then v = 1; end if type(x)==8 then select inttype(x) case 11 then mask = uint8(2^(pos-1)); case 12 then mask = uint16(2^(pos-1)); case 14 then mask = uint32(2^(pos-1)); end if v==0 then y = x & (~mask); else y = x | mask; end return; else // type == 1 a = 2^32; mask = uint32(zeros(pos)); y_MSB = uint32(zeros(pos)); y_LSB = uint32(zeros(pos)); y_LSB = uint32( x - double(uint32(x/a)) * a ); // LSB Less Significant Bits y_MSB = uint32( x/a ); // MSB Most Significant Bits if or(pos<=32) then mask( pos<=32 ) = uint32( 2^(pos(pos<=32) -1 )); if v==0 then y_LSB( pos<=32 ) = y_LSB(pos<=32) & (~mask(pos<=32)); else y_LSB( pos<=32 ) = y_LSB(pos<=32) | mask(pos<=32); end end if or(pos>32) then mask( pos>32 ) = uint32( 2^(pos(pos>32) -32 -1 )); if v==0 then y_MSB( pos>32 ) = y_MSB(pos>32) & (~ mask(pos>32)); else y_MSB( pos>32 ) = y_MSB(pos>32) | mask(pos>32); end end y = double( a * y_MSB + y_LSB ); end endfunction
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -