Sie sind auf Seite 1von 18

Operaciones con nmeros binarios

Suma de nmeros Binarios Las posibles combinaciones al sumar dos bits son

0+0=0 0+1=1 1+0=1 1 + 1 = 10


100110101 + 11010101 1000001010

Operamos como en el sistema decimal: comenzamos a sumar desde la derecha, en nuestro ejemplo, 1 + 1 = 10, entonces escribimos 0 en la fila del resultado y llevamos 1 (este "1" se llama arrastre). A continuacin se suma el acarreo a la siguiente columna: 1 + 0 + 0 = 1, y seguimos hasta terminar todas la columnas (exactamente como en decimal).

Resta de nmeros binarios El algoritmo de la resta en binario es el mismo que en el sistema decimal. Pero conviene repasar la operacin de restar en decimal para comprender la operacin binaria, que es ms sencilla. Los trminos que intervienen en la resta se llaman minuendo, sustraendo y diferencia. Las restas bsicas 0-0, 1-0 y 1-1 son evidentes:

0-0=0 1-0=1 1-1=0 0 - 1 = no cabe o se pide prestado al proximo.

La resta 0 - 1 se resuelve, igual que en el sistema decimal, tomando una unidad prestada de la posicin siguiente: 10 - 1 = 1 y me llevo 1, lo que equivale a decir en decimal, 2 - 1 = 1. Esa unidad prestada debe devolverse, sumndola, a la posicin siguiente. Veamos algunos ejemplos:
Restamos 17 - 10 = 7 (2=345) Restamos 217 - 171 = 46 (3=690)

10001 -01010 01111

11011001 -10101011 00101110

A pesar de lo sencillo que es el procedimiento, es fcil confundirse. Tenemos interiorizado el sistema decimal y hemos aprendido a restar mecnicamente, sin detenernos a pensar en el significado del arrastre. Para simplificar las restas y reducir la posibilidad de cometer errores hay varias soluciones:

Dividir los nmeros largos en grupos. En el siguiente ejemplo, vemos cmo se divide una resta larga en tres restas cortas:
100110011101 -010101110010 010000101011 1001 -0101 0100 1001 -0111 0010 1101 -0010 1011

Utilizando el complemento a dos. La resta de dos nmeros binarios puede obtenerse sumando al minuendo el complemento a dos del sustraendo. Veamos algunos ejemplos. Hagamos la siguiente resta, 91 - 46 = 45, en binario:
1011011 -0101110 0101101 C2 de 46 = 1010010 1011011 +1010010 10101101

En el resultado nos sobra un bit, que se desborda por la izquierda. Pero, como el nmero resultante no puede ser ms largo que el minuendo, el bit sobrante se desprecia. Un ltimo ejemplo: vamos a restar 219 - 23 = 196, directamente y utilizando el complemento a dos:
11011011 -00010111 11000100 C2 de 23 = 11101001 11011011 +11101001 111000100

Y, despreciando el bit que se desborda por la izquierda, llegamos al resultado correcto: 11000100 en binario, 196 en decimal.

Utilizando el complemento a 1. La resta de dos nmeros binarios puede obtenerse sumando al minuendo el complemento a uno del sustraendo y a su vez sumarle el bit de overflow (bit que se desborda).

Producto de nmeros binarios El algoritmo del producto en binario es igual que en nmeros decimales; aunque se lleva cabo con ms sencillez, ya que el 0 multiplicado por cualquier nmero da 0, y el 1 es elelemento neutro del producto. Por ejemplo, multipliquemos 10110 por 1001:
10110 1001 10110 00000 00000 10110 11000110

En sistemas electrnicos, donde se suelen utilizar nmeros mayores, no se utiliza este mtodo sino otro llamado algoritmo de Booth.

Divisin de nmeros binarios La divisin en binario es similar a la decimal, la nica diferencia es que a la hora de hacer las restas, dentro de la divisin, estas deben ser realizadas en binario. Por ejemplo, vamos a dividir 100010010 (274) entre 1101 (13):
100010010 |1101 - 0000 010101 10001 - 1101 01000 - 0000 10000 - 1101 00111

- 0000 01110 - 1101 00001

Conversin entre binarios y decimales, binario a octal y de binario a hexadecimal


Binario a decimal

Para realizar la conversin de binario a decimal, realice lo siguiente: 1. Inicie por el lado derecho del nmero en binario, cada nmero multiplquelo por 2 y elvelo a la potencia consecutiva (comenzando por la potencia 0). 2. Despus de realizar cada una de las multiplicaciones, sume todas y el nmero resultante ser el equivalente al sistema decimal. Ejemplos:

110101 (binario) = 53 (decimal). Proceso:


(0)=1 (1)=0 (2)=4 (3)=0 (4)=16 (5)=32

1*(2) elevado a 0*(2) elevado a 1*(2) elevado a 0*(2) elevado a 1*(2) elevado a 1*(2) elevado a La suma es: 53

10010111 (binario) = 151 (decimal). Proceso:


(0)=1 (1)=2 (2)=4 (3)=0 (4)=16 (5)=0 (6)=0 (7)=128

1*(2) elevado a 1*(2) elevado a 1*(2) elevado a 0*(2) elevado a 1*(2) elevado a 0*(2) elevado a 0*(2) elevado a 1*(2) elevado a La suma es: 151

110111 (binario) = 55 (decimal). Proceso:

1*(2) elevado a 1*(2) elevado a 1*(2) elevado a 0*(2) elevado a 1*(2) elevado a 1*(2) elevado a La suma es: 55

(0)=1 (1)=2 (2)=4 (3)=0 (4)=16 (5)=32

Decimal a binario Se divide el nmero decimal entre 2 cuyo resultado entero se vuelve a dividir entre 2 y as sucesivamente. Una vez llegados al 1 indivisible se cuentan el ltimo cociente, es decir el uno final (todo nmero binario excepto el 0 empieza por uno), seguido de los residuos de las divisiones subsiguientes. Del ms reciente hasta el primero que result. Este nmero ser el binario que buscamos. A continuacin se puede ver un ejemplo con el nmero decimal 100 pasado a binario.
100 |_2 0 50 |_2 0 25 |_2 --> 100 1 12 |_2 0 6 |_2 0 3 |_2 1 1

1100100

Otra forma de conversin consiste en un mtodo parecido a la factorizacin en nmeros primos. Es relativamente fcil dividir cualquier nmero entre 2. Este mtodo consiste tambin en divisiones sucesivas. Dependiendo de si el nmero es par o impar, colocaremos un cero o un uno en la columna de la derecha. Si es impar, le restaremos uno y seguiremos dividiendo por dos, hasta llegar a 1. Despus slo nos queda tomar el ltimo resultado de la columna izquierda (que siempre ser 1) y todos los de la columna de la derecha y ordenar los dgitos de abajo a arriba. Y luego se hara un cuadro con las potencias con el resultado. Ejemplo:
100|0 50|0 25|1 12|0 6|0 3|1 1|1

--> 1, 25-1=24 y seguimos dividiendo por 2

--> 100

1100100

Y tambin tenemos otro mtodo el mtodo de distribucin en el que distribuimos el nmero decimal y podemos tener el resultado en binario, trabaja de la siguiente manera tenemos el nmero 151 lo que tenemos que hacer es distribuir este nmero buscando el nmero ms prximo; en este caso es 128 as que en la casilla donde hay capacidad de contener el nmero que tenemos lo vamos marcando. y en las casillas que no empleamos las marcaremos con un 0. Ejemplo:
2^0= 1|1 2^1= 2|1 2^2= 4|1 2^3= 8|0 2^4= 16|1 2^5= 32|0 2^6= 64|0 2^7= 128|1 2^8= 256|0

128+16+4+2+1=151

Y sucesivos.

Binario a octal Para realizar la conversin de binario a octal, realice lo siguiente: 1) Agrupe la cantidad binaria en grupos de 3 en 3 iniciando por el lado derecho. Si al terminar de agrupar no completa 3 dgitos, entonces agregue ceros a la izquierda. 2) Posteriormente vea el valor que corresponde de acuerdo a la tabla:
Nmero en binario Nmero en octal 000 0 001 1 010 2 011 3 100 4 101 5 110 6 111 7

3) La cantidad correspondiente en octal se agrupa de izquierda a derecha. Ejemplos:

110111 (binario) = 67 (octal). Proceso:

111 = 7 110 = 6 Agrupe de izquierda a derecha: 67

11001111 (binario) = 317 (octal). Proceso:

111 = 7 001 = 1 11 entonces agregue un cero, con lo que se obtiene 011 = 3 Agrupe de izquierda a derecha: 317

1000011 (binario) = 103 (octal). Proceso:

011 = 3 000 = 0 1 entonces agregue 001 = 1 Agrupe de izquierda a derecha: 103.

Octal a binario Cada dgito octal se lo convierte en su binario equivalente de 3 bits y se juntan en el mismo orden. Ejemplo:

247 (octal) = 010100111 (binario). El 2 en binario es 10, pero en binario de 3 bits es Oc(2) = B(010); el Oc(4) = B(100) y el Oc(7) = (111), luego el nmero en binario ser 010100111.

Binario a hexadecimal Para realizar la conversin de binario a hexadecimal, realice lo siguiente: 1) Agrupe la cantidad binaria en grupos de 4 en 4 iniciando por el lado derecho. Si al terminar de agrupar no completa 4 dgitos, entonces agregue ceros a la izquierda. 2) Posteriormente vea el valor que corresponde de acuerdo a la tabla:
Nme ro en binari o

00 00

00 01

00 10

00 11

01 00

01 01

01 10

01 11

10 00

10 01

10 10

10 11

11 00

11 01

11 10

11 11

Nmer o en hexad ecimal

3) La cantidad correspondiente en hexadecimal se agrupa de izquierda a derecha. Ejemplos:

110111010 (binario) = 1BA (hexadecimal). Proceso:

1010 = A 1011 = B 1 entonces agregue 0001 = 1 Agrupe de izquierda a derecha: 1BA

11011110101 (binario) = 6F5 (hexadecimal). Proceso:

0101 = 5 1111 = F 110 entonces agregue 0110 = 6 Agrupe de izquierda a derercha: 6F5

Hexadecimal a binario

dem que para pasar de hexadecimal a binario, solo que se remplaza por el equivalente de 4 bits, como de octal a binario.

Tabla de conversin entre decimal, binario, hexadecimal, octal, BCD, Exceso 3 y Gray o Reflejado
Decimal 0 1 2 3 Binario 0000 0001 0010 0011 Hexadecimal 0 1 2 3 Octal 0 1 2 3 BCD 0000 0001 0010 0011 Exceso 3 0011 0100 0101 0110 Gray o Reflejado 0000 0001 0011 0010

4 5 6 7 8 9 10 11 12 13 14 15

0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

4 5 6 7 8 9 A B C D E F

4 5 6 7 10 11 12 13 14 15 16 17

0100 0101 0110 0111 1000 1001

0111 1000 1001 1010 1011 1100

0110 0111 0101 0100 1100 1101

http://centros.edu.xunta.es/iesmanuelchamosolamas/electricidade/fotos/numeracion.htm

Binary Number Representation System


Note: n**m means n raised to the power m

Basics
Binary numbers are simply numbers represented in base 2, as opposed to decimal (base 10).

So, in a binary number such as 1011 ^^^^ ||||-- represents 1 * 2**0 = 1 * 1 |||---represents 1 * 2**1 = 1 * 2 ||---represents 0 * 2**2 = 0 * 4 |---represents 1 * 2**3 = 1 * 8

For a total of 11 decimal. The smallest positive binary integer (in 8-bit format) is 00000000, or decimal 0. The largest number is obviously (is it obvious? it should be) 11111111, which a few moments' thought will show you is equivalent to:
2**7 + 2**6 + 2**5 + 2**4 + 2**3 + 2**2 + 2**1 + 2**0 = 255 decimal or 2**8-1.

Coincidence? No. Consider that the number 1 greater than 11111111 would be 100000000 (which takes 9 bits). This is evidently 2**8, and so we can see that the largest positive number we can represent in n bits is 2**(n-1) - 1.

Conversions
1. From decimal to binary Simply repeatedly divide the original number by two, noting all the remainders. The answer is the remainders strung together from left to right. Example: what is 106 decimal represented in binary?
2| 106 ----2| 53 r 0 ---2| 26 r 1 ---2| 13 r 0 ---2| 6 r 1 --2| 3 r 0 ---

2| 1 --0

r 1 r 1

Reading the remainders from bottom (right) to top (left) we get: 1101010 2. From binary to decimal This is even easier. Simply recall that each bit, reading from right to left, represents one higher power of 2. So the rightmost bit represents multiples of 2**0 (i.e. 1), the next multiples of 2**1 (i.e. 2) So 1101010 = 1 * 2**6 + 1 * 2**5 + 1 * 2**3 + 1 * 2**1 (leaving out the zeros)
= 64 + 32 + 8 + 2 = 106 decimal QED.

Arithmetic
Binary arithmetic is simple, especially addition. Here is the addition table for binary:
+ | 0 1 -------0 | 0 1 1 | 1 0C

Where the C means there is a "carry" out of 1 to the left.


So 0101 + 0011 ---1000 = 5 decimal = 3 decimal = 8 decimal

Subtraction is similarly easy. Here is the subtraction table for binary:


- | 0 1 -------0 | 0 1B 1 | 1 0

Where the B means there is a "borrow" from the left. So:


0101 - 0011 ---= 5 decimal = 3 decimal

0010

= 2 decimal

Multiplication is even simpler. Here is the multiplication table for binary:


* | 0 1 -------0 | 0 0 1 | 0 1

Here is an example of long multiplication - done both British and North American ways.
0011 * 0101 ---0011 + 001100 -----001111 = 3 decimal = 5 decimal

= 15 decimal

or
0011 * 0101 ---001100 + 0011 -----001111 = 3 decimal = 5 decimal

= 15 decimal

Negative numbers
The above applies to positive numbers, but how can we represent a negative number? We cannot use a '-' sign because all we can store in the computer is zeros and ones. There are three methods which have been used. The last is still in use today. For the sake of simplicity all numbers in the following discussion are 8 bits long. The arguments apply to any length of binary number. 1. Signed Magnitude With this system the leftmost bit is not considered part of the number, but as a representation of its sign:
o o

0 => positive 1 => negative

The remaining n-1 (in our case 7) bits represent the number - the magnitude.
So: and 00000101 10000101 would be 5 decimal would be -5 decimal.

Unfortunately there are a few problems with this. Firstly consider zero.
00000000 but so does 10000000 (-0 to be precise) represents 0

This is inconvenient as it complicates the circuitry. We should like to be able to compare two numbers (a very common operation) by saying: "if the bit patterns are the same the two numbers have the same value; if they are different then they have different values." Unfortunately the second half of this is not true for signed magnitude binary numbers: the two patterns could represent +0 and -0, so extra circuitry must be incorporated to test for this. Secondly, arithmetic with signed magnitude is complex. Although adding two positive numbers is not a problem (unless the result overflows into the sign bit, but we'll worry about that in COMP182 ). But consider adding +6 to -4.
+6 = 00000110 -4 = 10000100

If we simply add the bit patterns we shall get:


10001010

Which is -10 decimal, and not the -2 we wanted. The answer is simple (!): if the two numbers have the same sign then we can add them (except for the sign bit) and put the common sign bit in the result. Here are two examples:
+3 = 00000011 +6 = 00000110 ------------+ 00001001 -7 = 10000111 -4 = 10000100 ------------+ 10001011

= +9 decimal

= -11 decimal

If the two numbers are of opposite signs - i.e one plus and one minus then the rule is: subtract the smaller magnitude from the larger and give the result the sign of the larger original number.
+3 = 00000011 -6 = 10000110 ------------+ 10000011

= -3

Signed magnitude therefore has two strikes against it: 1) the arithmetic circuitry has to be more complex to deal with the problem of oppositelysigned numbers; 2) there are two representations of zero. 2. 1's Complement In the 1's complement system you get a negative number simply by inverting ('flipping') every bit in the positive version.
Thus 00000101

is +5 decimal and so 11111010 is -5 decimal.

The advantage of 1's complement is that arithmetic is now so much simpler: we don't have to worry about opposite signs - it will all 'come out in the wash'. The rule for adding numbers in 1's complement is simple: just add the two numbers together plus if there is a carry out to the left this is added in as a 1 at the right. (Simpler view: the left carry out is added at the right, whether it's a 1 or a 0). Example:
+3 = 00000011 +6 = 00000110 ------------+ 00001001 +7 = 00000111 +4 = 00000100 ------------+ 00001011 now and

= +9 decimal

= +11 decimal

-7 = 11111000 -4 = 11111011

-7 = 11111000 -4 = 11111011 1 ------------+ 11110100

<- the 'end around carry' bit from the left = -11 decimal (flip the bits and you get 00001011 or +11 decimal)

If the two numbers are of opposite signs - i.e one plus and one minus the the same method works:
+7 = 00000111 -4 = 11111011 1 ------------00000011 -7 = 11111000 +4 = 00000100 0 ------------11111100

<- 'eac' = +3 decimal

<- zero 'eac' here = -3 decimal (flip the bits and you get 00000011 = +3 decimal)

Now, what if we add +7 = 00000111 -7 = 11111000 0 ------------11111111

<- again zero eac = -0 (flip the bits and you get 00000000 = +0)

So, although the arithmetic works a treat we still have two representations for zero. 3. 2's complement With 2's complement arithmetic we make the converson from positive to negative (and vice-versa) slightly more complex. To go from one to the other now takes two stages: 1) flip all the bits 2) add 1. (This is almost like adding in the end around carry ahead of time).
+4 = 00000100 so to generate -4 1) 2) 11111011 + 1 -------11111100 +7 = 00000111 11111000

And 1)

2)

+ 1 -------11111001 = -7

Now we can do arithmetic as usual, ignoring carries and sign.


+3 = 00000011 +6 = 00000110 ------------+ 00001001 -7 = 11111001 -4 = 11111100 ------------+ 11110101

= +9 decimal

= +11 decimal (flip: 00001010 + 1 = 00001011 = +11 decimal)

(Don't forget we ignored the carry out to the left).

If the two numbers are of opposite signs - i.e one plus and one minus the the same method works:
+7 = 00000111 -4 = 11111100 ------------00000011 -7 = 11111001 +4 = 00000100 ------------11111101

= +3 decimal

= -3 decimal (flip: 00000010 + 1 = 00000011 = +3 decimal)

Now, what if we add +7 = 00000111 -7 = 11111001 ------------00000000

= 0

Interesting! Now what if we try to generate -0 from +0? +0 = 00000000 11111111 + 1 -------00000000

Aha! In 2's complement we: 1. have simpler arithmetic and 2. only have a single representation for zero This is why 2's complement is today exclusively used for integer binary arithmetic.

A tip
In order to convert say 10011011 from binary to decimal we must first ask whether the bit pattern represents a signed or unsigned binary number. There is absolutely nothing in the bit pattern itself that tells us - it is important that you appreciate this. (In fact those 8 bits might represent an ASCII or EBCDIC character for all we know). If it is unsigned then the usal conversion method (rightmost bit represents 1, next 2, next 4 etc.) works. E.g.:
10011011 ^^^^^^^^ ||||||||-- represents 1 * 2**0 = 1 * 1 |||||||---represents 1 * 2**1 = 1 * 2 ||||||---represents 0 * 2**2 = 0 * 4 |||||---represents 1 * 2**3 = 1 * 8 ||||---represents 1 * 2**4 = 1 * 16 |||---represents 0 * 2**5 = 0 * 32 ||---represents 0 * 2**6 = 0 * 64 |---represents 1 * 2**7 = 1 * 128

If it is a 2's complement signed number then we can either use the algorithm described above (flip bits and add 1) to turn it into its positive value (any binary number with its leftmost bit 1 will be negative: this is true - albeit for different reasons - for all three systems). But then we still have to convert the resulting positive number. On the other hand, we can view a 2's complement number like this:
10011011 ^^^^^^^^ ||||||||-- represents 1 * 2**0 = 1 * 1 |||||||---represents 1 * 2**1 = 1 * 2 ||||||---represents 0 * 2**2 = 0 * 4 |||||---represents 1 * 2**3 = 1 * 8 ||||---represents 1 * 2**4 = 1 * 16 |||---represents 0 * 2**5 = 0 * 32 ||---represents 0 * 2**6 = 0 * 64 |---represents 1 * -(2**7) = 1 * -128

Notice that the leftmost bit's negativity (if you will) now completely outweighs the positivity of all the others combined (so if the leftmost bit is a 1 the number must be negative). So the bit pattern above, if a positive number, represents 155 (= 1 + 2 + 8 + 16 + 128)

But if it is a 2's complement number it is negative and has the value -101 (= 1 + 2 + 8 + 16 + -128). We can confirm this by using the slow algorithm:
10011011 flip: 01100100 + 1 -------01100101 = 1 + 4 + 32 + 64 = 101

So the original bit pattern represented -101 as claimed. (And some days the bear eats you).

Das könnte Ihnen auch gefallen