How to do bit-wise operations on symbolic variables that may be very long possibly larger than inter 64
1 次查看(过去 30 天)
显示 更早的评论
Below is something I was able to do with some smaller numbers in base 2 and I can do base 10 also. Using Min,Max I can do AND and OR but I haven't been able to understand the use of the Symbolic Tool Box to do this symbolically .
a=12345
b=9867
c=0
n=0
while a>0 |b>0
digita=mod(a,2)
digitb=mod(b,2)
c=c+max(digita,digitb)*2^n
n=n+1
a=floor(a/2)
b=floor(b/2)
end
c
采纳的回答
John D'Errico
2024-1-27
编辑:John D'Errico
2024-1-28
Why is there a problem? Just define the numbers as syms. Make sure that you don't make the mistake of computing 2^n as a DOUBLE. That would surely cause failure at some point if n grows large enough.
a = sym('1234566726356446657575412365654161131310325345345');
b = sym('9866848646846846864684635468141413513513511534146867');
c = sym(0);
n=0;
while a>0 || b>0
digita=mod(a,2);
digitb=mod(b,2);
c=c+max(digita,digitb)*sym(2)^n;
n=n+1;
a=floor(a/2);
b=floor(b/2);
end
n
c
a
b
Make sure you keep everything a sym, and you will have no problem. Note my use of the || operator in the while statement. It is not really that significant here, but it can improve the speed of your code.
However, did you really need to use a loop there? Of course not.
a = sym('1234566726356446657575412365654161131310325345345');
b = sym('9866848646846846864684635468141413513513511534146867');
% in case we need leading zero bits on the smaller number
% I could also pad the smaller, but this test is trivial.
if a > b
abin = dec2bin(a);
bbin = dec2bin(b,numel(abin));
else
bbin = dec2bin(b);
abin = dec2bin(a,numel(bbin));
end
abin
bbin
% I could compute c here in many ways, but bin2dec does
% not generate a sym. In my large integer codes, I provided
% that as an option, but not hard to do.
cbin = abin;
cbin(bbin == '1') = '1';
nc = numel(cbin);
c = dot(cbin - '0',sym(2).^(nc-1:-1:0))
Note that c is the same in both cases, but that a loop was never necessary.
Ok, could we have used direct bit operators here? For example, we can do this:
a = 12345;
b = 9867;
bitor(a,b)
The problem is, bitor does not apply to symbolic numbers. Sorry.
更多回答(2 个)
Walter Roberson
2024-1-27
编辑:Walter Roberson
2024-1-28
a = sym(12345);
b = sym(9867);
c = sym(0);
n = sym(0);
while a>0 |b>0
digita=mod(a,2);
digitb=mod(b,2);
c=c+max(digita,digitb)*2^n;
n=n+1;
a=floor(a/2);
b=floor(b/2);
end
c
n
0 个评论
Sulaymon Eshkabilov
2024-1-27
Is this what you are trying to get:
syms a b c n % Create symbols
a = 12345;
b = 9867;
c = sym(0);
n = 0;
while a > 0 || b > 0
digita = mod(a, 2);
digitb = mod(b, 2);
c = c + max(digita, digitb) * 2^n;
n = n + 1;
a = floor(a / 2);
b = floor(b / 2);
end
disp(c);
6 个评论
John D'Errico
2024-1-27
Um, predefining those variables as syms does NOTHING.
syms a b c n % Create symbols
a = 12345;
b = 9867;
whos a b
Do you see that now, a and b are DOUBLES?
Sulaymon Eshkabilov
2024-1-28
Yes, I overlooked. This is what it should be like:
a = sym(12345);
b = sym(9867);
c = sym(0);
n = sym(0);
while a > 0 || b > 0
digita = mod(a, 2);
digitb = mod(b, 2);
c = c + max(digita, digitb) * 2^n;
n = n + 1;
a = floor(a / 2);
b = floor(b / 2);
end
disp(c);
whos a b c n
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!