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

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?

 Accepted Answer

[x(2:end), 0]

4 Comments

[x(:,2:end), zeros(size(x,1), min(1, size(x,2))] %works if the input is 2D array or row or column vector in which each row is to be shifted independently
Note that for your x, this will result in a column vector of all 0.
You need to make a decision: do you want to shift each of your values left one place, or do you want to treat your values as if they were a row vector that should be shifted left one place.
[reshape(x(2:end), 1, []), zeros(1, min(1, length(x)))] %works if the input is row or column vector intended to be treated as row vector
The min() that you see in those expressions are to account for the possibility that your x is empty: in that case you need to avoid introducing non-empty 0 into the left shift of emptiness.
Sir, when I tried this, [x(1:end)]
ans =
1
0
1
1
0
1
0
0
0
1
1
1
0
0
1
1
[x(2:end)]
ans =
0
1
1
0
1
0
0
0
1
1
1
0
0
1
1
[x(3:end)]
ans =
1
1
0
1
0
0
0
1
1
1
0
0
1
1
But in each step, at the end zeros must be added so that the entire number will always have 16 digits??? How to solve that?
>> 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]

Sign in to comment.

More Answers (1)

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 Comments

We do not know what your v is when you start executing the loop.
Suppose that when you began, v was length 16, when i was 1. Then when you get to the tests for lsb, the
v = [v(i:end); 0]
would be v = [v(1:end); 0] which would be [v(1:16); 0] which is going to create something of length 17; likewise for the lsb == 1 branch. So whatever length v is when you start, after the iteration for i=1 is finished, v will be one longer, which will lead to a mismatch of sizes when the bitxor(Z,v) is executed for i=2
v1 = {'1' '0' '0' '1' '0' '0' '1' '0' '1' '1' '1' '0' '0' '1' '1' '0'}; v=hex2dec(v1);
So, what correction should i do??
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.

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!