How to do bit-wise operations on symbolic variables that may be very long possibly larger than inter 64
2 ビュー (過去 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
参考
カテゴリ
Help Center および File Exchange で Assumptions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!