命令来垫零到二进制数的具体位置? [英] Command to pad zeros to specific locations in binary numbers?

查看:139
本文介绍了命令来垫零到二进制数的具体位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要垫零到二进制数的特定位置。循环二进制数阵列的形式,如 DEC2BIN(43)并添加零和调整大小的声音的车轮的再造。

如何填充零有效地二进制数在Matlab?

循环

 位置= [1,3,6];
X = de2bi(43);
XX =翻转(X);KK = 1;
第II = 1:长度(X)+长度(位置)
    如果ISEQUAL(职位(KK),XX(二))
         %通过一个索引转乘II尾进取,
         因为我觉得我的方法不好%留下来了!
         X(II)= 0;
         位置=位置+ 1;一个现在由于一个填补%持仓量增加
         KK = KK + 1;
    结束
结束

这感觉太重塑:基本上如何做到Ÿ像xxxxYabcd到xxxx0Yabcd哪里xxxxYabcd是一个二进制数字前得到0

示例


  

输入


  
  

    

101011,(1,3,6)


  
  
  

输出


  
  

    

100100110


    
    

1x010x11x(表示为x零更清楚,其中零插入的位置)


  

解决方案

赛开始了! Mendo胜,gevang是一个不错的第二,thewaywewalk是第三和jaheruddin进来第四。该夏嘉曦的 bitshifting 在晒功能的实现,遗憾的是没有得到它运行得更快。

结果

  gevang:2.2E-05
thewaywewalk:5.6975e-05
mendo:2.2102e-05
jaheruddin:0.0001693
夏嘉曦(HHH-执行不力)5.3288e-04

问题的答案暖机测试

 函数test_padding_zeros()    功能myOutput = gevang(XX,位置)
        nPads =长度(位置);
        nPadsShifts = 1:nPads;
        myOutput =一(1,长度(XX)+ nPads);新的阵列%重新索引
        myOutput(位置+ nPadsShifts)= 0; %填充值
        myOutput(myOutput == 1)= XX; %设置原始位值
    结束    功能myOutput = thewaywewalk(X,位置)
        IDX = numel(X): - 1:1;
        myOutput = num2cell(X);
        myOutput(2,IDX(位置))= {0};
        myOutput = [myOutput {:}];
    结束    功能myOutput = jaheruddin(myInput,位置)%myInput可以是行向量或矩阵!
        N =大小(myInput,2)+ numel(职位);
        myOutput =假(大小(myInput,1)中,n);
        myOutput(:,set​​xor((1:长度(位置)),1:N))= myInput;
    结束    功能myOutput = mendo(myInput,位置)
        myOutput =一(1,长度(myInput)+长度(位置));
        myOutput(仓位+(1:长度(位置)))= 0;
        myOutput(myOutput == 1)= myInput;
    结束    功能OUT = bizarreBitShift(带bnum,fromBit,shiftAmount)
        构建%面具
        MSK = UINT32((2 ^(fromBit - 1))-1);
        shiftPart = BITAND(UINT32(带bnum),bitcmp(MSK)); %bitcmp - 位补
        staticPart = BITAND(UINT32(带bnum),MSK);
        OUT = bitshift(shiftPart,shiftAmount);
        OUT = BITOR(满分,staticPart);
    结束    功能myOutput =夏嘉曦(myInput,位置)
        shiftAmount = 1;
        myOutput = sprintf的('%D',myInput);
        myOutput = BIN2DEC(myOutput);
        K = 0;
        第II = 1:长度(位置)
            fromBit =职位(II)+ K;
            myOutput = bizarreBitShift(myOutput,fromBit,shiftAmount);
            K = K + 1;
        结束        myOutput = ismember(DEC2BIN(myOutput),'1');
    结束
位置= [1 3 6]; //%示例数据
myInput = [1 0 1 0 1 1]; //%示例数据ggevang = @()gevang(myInput,位置);
tthewaywewalk = @()thewaywewalk(myInput,位置);
mmendo = @()mendo(myInput,位置);
jjaheruddin = @()jaheruddin(myInput,位置);
sshai = @()晒(myInput,位置);timeit(ggevang)
timeit(tthewaywewalk)
timeit(mmendo)
timeit(jjaheruddin)
timeit(sshai)结束

I need to pad zeros to specific locations in binary numbers. Looping the array form of a binary number such as dec2bin(43) and adding the zeros and adjusting the size sounds reinvention of the wheel.

How to pad zeros efficiently to binary numbers in Matlab?

Looping

positions=[1,3,6];
x=de2bi(43);
xx=flip(x);    

kk=1;
for ii=1:length(x)+length(positions)
    if isequal(positions(kk),xx(ii))
         %Transfer the tail from ii by one index ahead,
         %left out here because I think my method is bad!
         x(ii)=0;
         positions=positions+1;  %Increase positions by one now due to one padding
         kk=kk+1;
    end
end

which feels too much reinvention: basically how to get 0 just before Y like from xxxxYabcd to xxxx0Yabcd where xxxxYabcd is a binary number?

Example

Input

101011, (1,3,6)

Output

100100110

1x010x11x (showing location of zeros clearer with x where zeros inserted)

解决方案

Race begins! Mendo wins, gevang is a good second, thewaywewalk is the third and jaheruddin comes in fourth. The Shai's bitshifting is implemented in the shai function, unfortunately not yet getting it run faster.

Results

gevang :2.2e-05
thewaywewalk :5.6975e-05
mendo :2.2102e-05
jaheruddin :0.0001693
shai (poor hhh-implementation) 5.3288e-04

Warmed-up testing of the answers

function test_padding_zeros()

    function myOutput=gevang(xx,positions)
        nPads = length(positions);
        nPadsShifts = 1:nPads;
        myOutput = ones(1, length(xx) + nPads); % re-indexing on the new array
        myOutput(positions + nPadsShifts) = 0;  % padding values
        myOutput(myOutput==1) = xx;                    % set original bit values
    end

    function myOutput=thewaywewalk(x,positions)
        idx = numel(x):-1:1;
        myOutput = num2cell(x);
        myOutput(2,idx(positions)) = {0};
        myOutput = [myOutput{:}];
    end

    function myOutput=jaheruddin(myInput,positions) % myInput can be a row vector or a matrix!
        n = size(myInput,2)+numel(positions);
        myOutput = false(size(myInput,1),n);
        myOutput(:,setxor((1:length(positions)),1:n))=myInput;
    end

    function myOutput=mendo(myInput,positions)
        myOutput = ones(1,length(myInput)+length(positions));
        myOutput(positions+(1:length(positions))) = 0;
        myOutput(myOutput==1) = myInput;
    end

    function out = bizarreBitShift( bNum, fromBit, shiftAmount )
        % construct a mask
        msk = uint32( (2^( fromBit - 1 ) )-1 ); 
        shiftPart = bitand( uint32(bNum), bitcmp(msk) ); % bitcmp - complement of bits
        staticPart = bitand( uint32(bNum), msk );
        out = bitshift( shiftPart , shiftAmount );
        out = bitor( out, staticPart );
    end

    function myOutput=shai(myInput,positions)
        shiftAmount=1;
        myOutput=sprintf('%d',myInput);
        myOutput=bin2dec(myOutput);
        k=0;
        for ii=1:length(positions)
            fromBit=positions(ii)+k;
            myOutput=bizarreBitShift(myOutput, fromBit, shiftAmount);
            k=k+1;
        end

        myOutput=ismember(dec2bin(myOutput),'1');
    end




positions = [1 3 6]; %// example data
myInput = [1 0 1 0 1 1]; %// example data

ggevang=@() gevang(myInput,positions);
tthewaywewalk=@() thewaywewalk(myInput,positions);
mmendo=@() mendo(myInput,positions);
jjaheruddin=@() jaheruddin(myInput,positions);
sshai=@() shai(myInput,positions);

timeit(ggevang)
timeit(tthewaywewalk)
timeit(mmendo)
timeit(jjaheruddin)
timeit(sshai)

end

这篇关于命令来垫零到二进制数的具体位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆