performing 1s and 2s complement,only first row is being done 2s complement,where i am wrong please tel

6 views (last 30 days)
A=["48","03","0C","F6","00","F5","40","02","04","02","94","F4","D0","F5","2C","03","50","01","38","F4","24","F7","14","04","80","00","F4","F3","5C","F8","C4","04","28","FF","84","F3","08","F9","6C","04","EC","FD","CC","F2","2C","FA","E4","04","44","FC","C4","F2","1C","FC","6C","05","E4","FB","08","F3","40","FD","60","05","5C","FA","20","F3","70","FE","C8","04","A4","F8","04","F3","1C","FF","F0","03","84","F7","D4","F3","84","00","DC","03","2C","F7","00","F5","18","02","24","03","BC","F5","88","F5","08","03","78","02","20","F5","40","F6","34","03","D0","00","08","F4","5C","F7","0C","04","DC","FF","98","F3","7C","F8","C8","04","F8","FE","48","F3","F0","F9","34","05","70","FD","C8","F2","40","FB","40","05","24","FC","C0","F2","DC","FB","F4","04","18","FB","E4","F2","70","FD","D4","04","A8","F9","54","F3","F4","FE","FC","04","68","F8","50","F3","0C","00","34","04","50","F7","80","F3","BC","00","54","03","00","F6","8C","F4","48","02","60","03","CC","F5","A0","F5","14","03","D8","01","44","F4","28","F7","D0","03","70","00","44","F4","E0","F7","70","04","D4","FF","6C","F3","A8","F8","E8","04","9C","FE","3C","F3","38","FA","2C","05","FC","FC","EC","F2","38","FB","0C","05","20","FC","C4","F2","80","FC","24","05","40","FA","BC","F2","14","FE","1C","05","38","F9","10","F3"];
B=A';
C = reshape(A,2,[])';
D=join(C);
E=regexprep(D,'[^\w'']',''); %remove spaces
F=hex2dec(E); %hex2dec
G=dec2bin(F);
c=not(G-'0'); % one's complement
d=1;
c1=double(c);
c2=dec2hex(c1');
c3=reshape(c2,16,[])';
c4=string(c3);
d=1;
c5_vec=zeros(i,k);
for i=1:512
for k=16:-1:1
r=d & c(i,k);
c5(i,k)=xor(d,c(i,k));
c5_vec=c5;
d=r;
end
end
Here c5 is expected to save 2s complement of all the elements,but only first row is being 2s complemented.

Answers (1)

ag
ag on 4 Dec 2024 at 16:26
Hi Akshay,
The issue that you are facing is because the carry value "d" is not being reset after the completion of each row's calculation.
The below code snippet demonstrates the modification that should resolve the issue:
for i=1:512
d = 1; % resetting carry value for each row
for k=16:-1:1
r=d & c(i,k);
c5(i,k)=xor(d,c(i,k));
c5_vec=c5;
d=r;
end
end
Hope this helps!

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!