function for addition 2^16 modulo

조회 수: 10 (최근 30일)
Saranya Krishnamurthy
Saranya Krishnamurthy 2021년 4월 17일
댓글: Saranya Krishnamurthy 2021년 4월 20일
Addition modulo (denoted with a boxed plus ⊞)
what is the command for 2^16 addition modulo: a ⊞ b?
Can someone help?
  댓글 수: 2
James Tursa
James Tursa 2021년 4월 17일
Are a and b type double, or an integer type?
Saranya Krishnamurthy
Saranya Krishnamurthy 2021년 4월 19일
binary
even if its integer its fine

댓글을 달려면 로그인하십시오.

답변 (2개)

Bjorn Gustavsson
Bjorn Gustavsson 2021년 4월 19일
Just make sure that your ints a and b are in int32 or larger number of bit-formats, then use:
C = mod(a+b,2^16)
HTH

John D'Errico
John D'Errico 2021년 4월 19일
편집: John D'Errico 2021년 4월 19일
Your numbers are in BINARY form, and you want to add modulo 2^16.
The simplest solution is to convert to a decimal integer. I'll assume they are stored as chars. But if they are boolean vectors, just as easy.
A = '101100110001';
B = '10010000001';
AplusB = mod(bin2dec(A) + bin2dec(B),2^16) % as a base 10 integer result
AplusB = 4018
AplusB = dec2bin(AplusB)
AplusB = '111110110010'
You can do it all in one line. Or, you could even write a little function.
binplus16 = @(a,b) dec2bin(mod(bin2dec(A) + bin2dec(B),2^16));
binplus16(A,B)
ans = '111110110010'
As you can see, the result is the same. We could have done it in a variety of ways. But this is simplest.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by