Which is the easiest method for shifting binary digits to the right?

조회 수: 1 (최근 30일)
x1 = {'1' '0' '1' '1' '0' '1' '0' '0' '0' '1' '1' '1' '0' '0' '1' '1'};
x=hex2dec(x1);
Thus in command window,
x =
1
0
1
1
0
1
0
0
0
1
1
1
0
0
1
1
Hence to do shift operation, which is the simplest way?
by one place, 0110100011100110
next, 1101000111001100
next , 1010001110011000
and so on?
Can somebody help me?

채택된 답변

Walter Roberson
Walter Roberson 2018년 2월 15일
  댓글 수: 4
Walter Roberson
Walter Roberson 2018년 2월 18일
>> x = [ 1
0
1
1
0
1
0
0
0
1
1
1
0
0
1
1]
x =
1
0
1
1
0
1
0
0
0
1
1
1
0
0
1
1
>> [reshape(x(2:end), 1, []), zeros(1, min(1, length(x)))].'
[reshape(x(2:end), 1, []), zeros(1, min(1, length(x)))].'
ans =
0
1
1
0
1
0
0
0
1
1
1
0
0
1
1
0
>> length(ans)
ans =
16
Or you could be lazy in your error checking and just use
[x(2:end); 0]
Darsana P M
Darsana P M 2018년 2월 23일
thanks for the answer sir.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Darsana P M
Darsana P M 2018년 2월 23일
for i=1:1:16
if x(i) == 0
% yj = y;
Z = Z
else x(i) == 1
Z = bitxor(Z,v);
end
if lsb == 0
v = [v(i:end); 0];
else lsb == 1
v = [v(i:end); 0];
v = bitxor(v,R);
end
end
I got an error stating this: Error using bitxor Inputs must have the same size.
Error in shifty (line 38) Z = bitxor(Z,v);
When I checked the size of v it was, 17
  댓글 수: 4
Darsana P M
Darsana P M 2018년 2월 23일
So, what correction should i do??
Walter Roberson
Walter Roberson 2018년 2월 23일
You can get around the first problem by changing
v = [v(i:end); 0]
to
v = [v(i+1:end); 0]
However, this will just postpone the problem one iteration: on the next iteration you will be generating a shorter v (because i has increased) and that shorter v will fail the Z = bitxor(Z,v) since Z did not also get shorter.
I think you need to revise your algorithm. One of the major revisions you need is that you need to document the code -- the purpose of the code, and a description of what you expect each step to accomplish.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by