Bitshift from a specific bit onwards in binary number? [duplicate]

不羁岁月 提交于 2019-12-23 07:08:57

问题


My idea for bit shifting binary numbers from many locations onwards is outlined below. I formulated this problem first as a zero-padding problem here but I am now starting to feel there may be an elegant bitshift solution. I can do bitshift such as bitshift(5,1) where 101 to 1010 but how to do it from a specific bit? For example, 101 from a certain bit shifted such as from the second bit onwards so that 101 to 1001?

One shifting from a specific location onwards

myBinary=de2bi(23);      %bits in increasing order, bi2de format from DEC number
                         %23=11101(BI decreasing), BI_bi2de=10111(increasing)

shiftAmount=3;           %amount to shift the bits
threshold=3;             %bits shifted in the increasing order from threshold

%Correct result:
%10111 --> 10'111 ---> 10000111 (shifted 3 times from the threshold 3)

tail=myBinary(1:threshold);
myBinary(1:threshold)=[];
myBinary=[zeros(1,shiftAmount),myBinary];
myBinary=[tail, myBinary] %in increasing order
myBinary=flip(myBinary)   %in decreasing order

Shifting many times from many locations onwards

From 10010 to 11 and then back to 10010, the last step requires 3 bit shifts. So the zeros could be stored as [0,2,3] in order to get from 11 to 10010 back. My idea is to use the above 'One shifting' method many times but feeling there may be easier solution here.

11--- bit 0 onwards ---> 110 
  --- bit 1 onwards ---> 1010 
  --- bit 3 onwards ---> 10010

How to bitshift from a specific location in a binary number onwards?


回答1:


use a mask to seperate the input into the "shifting part" and "static part".
Shift the "shifting part" and then re-assemble the number using a bitor operation:

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 );



回答2:


What about these two lines:

a = [ 1 0 1 0 1]
pos = 3;   %counting started with 0 from the right
n = 1;     %number of shifts

b = de2bi( 2^(pos+n)*bi2de( a(1:end-pos) ,'left-msb' ) ,'left-msb' );
b(end-pos+1:end) = a(end-pos+1:end);

returns:

b =

     1     0     0     1     0     1

and for pos = 1 as well as pos = 2:

b =

     1     0     1     0     0     1

If you would count your bits the same way Matlab does, from left to right, with the most siginificant bit on the right, the code is much easier:

b = de2bi( 2^(pos+n)*bi2de( a(pos+1:end) ) );
b(1:pos) = a(1:pos);

And regarding your "Shifting many times from many locations onwards" request I really don't see why this should be different from your previous question.



来源:https://stackoverflow.com/questions/20840409/bitshift-from-a-specific-bit-onwards-in-binary-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!