欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Matlab 十进制整数转化为二进制原码、补码

程序员文章站 2022-07-15 10:25:22
...

整数转化为二进制原码

function y = dec2yuanma(x,N)
   %1<=N<=53
    y=zeros(N,length(x));
    for i=1:length(x)
            if x(i)<0
                x(i)=2.^(N-1)-x(i);
            end
            d=flip(dec2bin(x(i)));
            b=zeros(N,1);
            for j=1:length(d)
                b(j)=str2num(d(j));
            end
            y(:,i)=b;
    end   
end

整数转化为二进制补码

function y = dec2buma(x,N)
   %1<=N<=53
    y=zeros(N,length(x));
    for i=1:length(x)
            if x(i)<0
                x(i)=2.^N+x(i);
            end
            d=flip(dec2bin(x(i)));
            b=zeros(N,1);
            for j=1:length(d)
                b(j)=str2num(d(j));
            end
            y(:,i)=b;
    end   
end
相关标签: MATLAB