Binary addition in MATLAB

33 visualizaciones (últimos 30 días)
Datla Ashwin
Datla Ashwin el 21 de Feb. de 2011
Comentada: Steven Lord el 28 de Nov. de 2024
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 comentarios
Jan
Jan el 21 de Feb. de 2011
I assume that 1010 and 1100 results in 2110 and not 2100.
Stephen23
Stephen23 el 27 de Nov. de 2024
Editada: Stephen23 el 27 de Nov. de 2024
"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
N = uint8 22
dec2bin(N)
ans = '10110'

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de Feb. de 2011
You need to see the Fixed Point Toolbox.
You need to see it because it requires you to make explicit numerous assumptions about your arithmetic.
For example, since you used 1010 and 1100 and not (say) 00001010 and 00001100, then we don't know whether you simply used "leading 0 suppression" or if you mean to restrict your arithmetic to 4 digits. If you mean to restrict your arithmetic to 4 digits, then you need to define whether upon overflow (such as you get here) you want the overflowing bits to be discarded or you want to "saturate" to the highest possible representable value 1111 . Your addition is thus answerable by a minimum of 3 different values: 10110, 0110, and 1111.
But then there is the difficulty that when you use binary, the '+' operator often means "or", which would give you the additional possible answer 1110.
More generally, are the values to be added 52 bits long or less? If so then their sum is exactly representable in a double precision number for fast calculations. But as soon as you go to 53 bits, the sum might need 54 bits: sums that occupy 54 through 64 bits can only be done directly in R2010b or later, unless through the Fixed Point Toolbox.
  1 comentario
Walter Roberson
Walter Roberson el 21 de Feb. de 2011
Oh yes, I forgot: in the earlier days of programming, on the 4 bit machines, it was common for the only "add" instruction to be "add with carry", in which the processor "Carry" flag would be set upon overflow like this case, and the next "add" instruction would automatically add the value of the Carry bit to the bottom bit.
Then there were the process control systems of that era, in which (for reasons I don't fully understand) a carry would be added back in to the result -- e.g., producing 0111 in this case.
A few of the less common systems of the 4 bit days had a "chained add", which would continue to move "backwards" along words adding in the carry until it detected that no carry was generated by the addition. This was related in some ways to the "add with carry" idea, but was aimed at adding constants held in small registers to numbers represented as a series of memory locations, automatically doing the carry upwards as far as was needed.
If you want to do binary addition, do it *right*

Iniciar sesión para comentar.

Más respuestas (6)

sundeep cheruku
sundeep cheruku el 14 de Abr. de 2013
Editada: Walter Roberson el 14 de Abr. de 2013
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 comentario
K E
K E el 22 de Jul. de 2016
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.

Iniciar sesión para comentar.


Andreas Goser
Andreas Goser el 21 de Feb. de 2011
Try:
dec2bin(bin2dec('1010')+bin2dec('1100'))
  1 comentario
Paulo Silva
Paulo Silva el 21 de Feb. de 2011
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.

Iniciar sesión para comentar.


Paulo Silva
Paulo Silva el 21 de Feb. de 2011
http://www.mathworks.com/matlabcentral/newsreader/view_thread/257224
  1 comentario
Andreas Goser
Andreas Goser el 21 de Feb. de 2011
You were faster... I was also considering to use lmgtfy.com :-)

Iniciar sesión para comentar.


Datla Ashwin
Datla Ashwin el 1 de Mzo. de 2011
i have tried using dec2bin but it didnt work for me
  1 comentario
Walter Roberson
Walter Roberson el 1 de Mzo. de 2011
You haven't defined which variety of binary addition you want.

Iniciar sesión para comentar.


Nicolás Dueñas
Nicolás Dueñas el 2 de Ag. de 2016
Editada: Walter Roberson el 2 de Ag. de 2016

Kevin
Kevin el 27 de Nov. de 2024
If you don't care about the carry bit:
mod([1 0 1 0] + [1 1 0 0],2)
  1 comentario
Steven Lord
Steven Lord el 28 de Nov. de 2024
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)
ans = 1×4
0 1 1 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
0b0111 + 0b0001
ans = uint8 8

Iniciar sesión para comentar.

Categorías

Más información sobre Code Generation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by