M=[2 32 45;3 54 12 98;134 245 69];
m1=(str2num(dec2bin(M)));
m=reshape(m1,[3 3]);
mm=m(:);
disp(dec2bin(bit_rotate(m,1),8))
function data = bit_rotate(data,nBits)
dataBits = log2(double(intmax(class(data)))+1);
nBits = rem(nBits,dataBits);
if nBits == 0
return
end
shiftedData = bitshift(data,nBits);
lostData = bitxor(data,bitshift(shiftedData,-nBits));
rotatedData = bitshift(lostData,nBits-sign(nBits)*dataBits);
data = shiftedData+rotatedData;
end
6 Comments
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/478957-how-to-do-left-and-right-bit-rotation#comment_742351
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/478957-how-to-do-left-and-right-bit-rotation#comment_742351
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/478957-how-to-do-left-and-right-bit-rotation#comment_742358
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/478957-how-to-do-left-and-right-bit-rotation#comment_742358
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/478957-how-to-do-left-and-right-bit-rotation#comment_742365
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/478957-how-to-do-left-and-right-bit-rotation#comment_742365
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/478957-how-to-do-left-and-right-bit-rotation#comment_742389
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/478957-how-to-do-left-and-right-bit-rotation#comment_742389
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/478957-how-to-do-left-and-right-bit-rotation#comment_742569
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/478957-how-to-do-left-and-right-bit-rotation#comment_742569
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/478957-how-to-do-left-and-right-bit-rotation#comment_742728
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/478957-how-to-do-left-and-right-bit-rotation#comment_742728
Sign in to comment.