Binary addition in MATLAB
이전 댓글 표시
Is it possible to do binary addition in MATLAB? I tried using the usual add function but it didnt work. I defined 2 numbers as binary digits, say 1010 and 1100. The output came out as 2100. It's a direct addition of 2 numbers.
댓글 수: 3
Andreas Goser
2011년 2월 21일
You could help the community to help by giving your code and not speculate
Jan
2011년 2월 21일
I assume that 1010 and 1100 results in 2110 and not 2100.
"Is it possible to do binary addition in MATLAB? I tried using the usual add function but it didnt work"
All numbers are binary. When MATLAB adds numbers it is doing very efficient binary addition for you.
N = 0b1010 + 0b1100
dec2bin(N)
채택된 답변
추가 답변 (6개)
sundeep cheruku
2013년 4월 14일
편집: Walter Roberson
2013년 4월 14일
fliplr(de2bi(bi2de(fliplr([1 0 1 0]))+bi2de(fliplr([1 1 0 0]))))
while using bi2de or de2bi it takes the first bit as LSB and last bit as MSB so fliplr is necessary to use.
Hope this might work.
댓글 수: 1
K E
2016년 7월 22일
For other newbies trying to understand: LSB and MSB are least and most significant bit and the fliplr is to get the bits in the desired order.
Andreas Goser
2011년 2월 21일
Try:
dec2bin(bin2dec('1010')+bin2dec('1100'))
댓글 수: 1
Paulo Silva
2011년 2월 21일
I wonder why matlab doesn't provide a simple way to work with binary numbers just like we do with decimal, these days any cheap calculator can do that.
Paulo Silva
2011년 2월 21일
0 개 추천
http://www.mathworks.com/matlabcentral/newsreader/view_thread/257224
댓글 수: 1
Andreas Goser
2011년 2월 21일
You were faster... I was also considering to use lmgtfy.com :-)
Datla Ashwin
2011년 3월 1일
0 개 추천
댓글 수: 1
Walter Roberson
2011년 3월 1일
You haven't defined which variety of binary addition you want.
Nicolás Dueñas
2016년 8월 2일
편집: Walter Roberson
2016년 8월 2일
0 개 추천
Kevin
2024년 11월 27일
If you don't care about the carry bit:
mod([1 0 1 0] + [1 1 0 0],2)
댓글 수: 1
That won't work if there's any carrying. Adding 7 and 1 ought to give 8 not 6:
mod([0 1 1 1] + [0 0 0 1], 2)
0b0111 + 0b0001
카테고리
도움말 센터 및 File Exchange에서 Code Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!