How to do bit-wise operations on symbolic variables that may be very long possibly larger than inter 64
2 views (last 30 days)
Show older comments
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
Accepted Answer
John D'Errico
on 27 Jan 2024
Edited: John D'Errico
on 28 Jan 2024
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.
More Answers (2)
Walter Roberson
on 27 Jan 2024
Edited: Walter Roberson
on 28 Jan 2024
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 Comments
Sulaymon Eshkabilov
on 27 Jan 2024
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 Comments
John D'Errico
on 27 Jan 2024
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
on 28 Jan 2024
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
See Also
Categories
Find more on Assumptions 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!