how swap the bits at position L&P of A(I,j).?

A= imread('camera man.png');
k=1;
for I=1:256;
j=1:256;
end
x=0.3;
p=0.343;
for n=2:65536;
if x(n-1)>=0 & x(n-1)<=p
x(n)=x(n-1)/p;
else
x(n)=(1-x(n-1))/(1-p);
end
end
S=(x*255)+1;
Q=0.2;
p=0.343;
for n=2:65536;
if Q(n-1)>=0 & Q(n-1)<=p
Q(n)=Q(n-1)/p;
else
Q(n)=(1-Q(n-1))/(1-p);
end
end
D=(Q*255)+1;
L= mod(S(k),8)+1;
P=mod(D(k),8)+1;
swap??

2 Comments

What you want to swap?
i want to swap the bits of L&P of A(I,j). help plzz

Sign in to comment.

 Accepted Answer

Guillaume
Guillaume on 11 Jul 2018
Edited: Guillaume on 18 Jul 2018
bits = bitget(A(i, j), [L, P]);
A(i, j) = bitset(A(i, j), P, bits(1));
A(i, j) = bitset(A(i, j), L, bits(2));
As an aside, I would strongly recommend that you declare x and Q as:
x = [0.3, zeros(1, 65535)];
Q = [0.2, zeros(1, 65535)];
to avoid the constant reallocations that your code is doing.

12 Comments

Guillaume
Guillaume on 11 Jul 2018
Edited: Guillaume on 11 Jul 2018
I have no idea what you are asking in your comment. What does them refer to in correct them.
As far as I can tell I've answered your question. The two lines of code I've provided will swap bit L and P of A(i,j).
A= imread('camera man.png');
k=1:65536;
for I=1:256;
for j=1:256;
x=[0.3,zeros(1,65535)];
p=0.343;
for n=2:65536;
if x(n-1)>=0 & x(n-1)<=p
x(n)=x(n-1)/p;
else
x(n)=(1-x(n-1))/(1-p);
end
end
S=(x*255)+1;
Q=[0.2,zeros(1,65535)];
p=0.343;
for n=2:65536;
if Q(n-1)>=0 & Q(n-1)<=p
Q(n)=Q(n-1)/p;
else
Q(n)=(1-Q(n-1))/(1-p);
end
end
D=(Q*255)+1;
L=mod(S(k),8)+1;
P=mod(D(k),8)+1;
bits = bitget(A(I,j),[L,P]);
A(I,j)=bitor(bitset(A(I,j),[P,L],bits));
end
end
I have tried, the corrections that my code you recommended me, in my code. But, it is still not working and always coming up with same error. I do not how to remove these errors to make my code run.
Clearly, you have bugs in your code. The ones I've spotted:
  • L and P are 1x65536 vectors. Your question implies they should be scalar
  • L and P are vectors of non-integer values. So for example, you would trying to swap bits 6.688 and 1.0321.
  • for each I and j, you recalculate x, S, Q, P, D, yet they never change. So you waste time repeating the exact same calculations 65535 times.
You need to rethink what your code is doing. We can't do that for you. Maybe you need to round L and P, maybe you need to iterate over the elements of L and P at the same time you iterate over the elements of A. And you certainly need to take the calculate of L and P out of the I, j loops if it doesn't depend on them.
I would also suggest that you use the debugger to understand what your code is actually doing.
Once you've sorted out the issues, the code I have given you will swap scalar integer bit P with scalar integer bit L.
Have you used the debugger to step through your code like I told you?
The main problem with your code, as I said, is that L and O are non-integer, because S (S1) and D (S2) are non-integer.
am used floor function to convert S&D into integer butt problem is still there in code.
Screenshots are useless, we can't copy/paste from them to test your code. And "problem is still there in code" is also useless without a description of the problem.
Nonetheless, I've made a mistake in my answer. The proper code should be:
bits = bitget(A(i, j), [L, P]);
A(i, j) = bitset(A(i, j), P, bits(1));
A(i, j) = bitset(A(i, j), L, bits(2));
what is mean by "bits(1),bits(2)"? because code is running only for first entry.
bits(1) is the bit that bitget extracted at position L, bits(2) is the bit that bitget extracted at position P.
documentation for bitget
documentation for bitset

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!