Tutorial.BitMask History

Hide minor edits - Show changes to markup

June 04, 2010, at 07:57 PM by Equipo Traduccion -
Changed lines 21-24 from:
        y = 1010
        x = y << 1

yields: x = 0100 @]

to:
           y = 1010
           x = y << 1

resultado: x = 0100 @]

Changed lines 30-33 from:
        y = 1010
        x = y >> 1

yields: x = 0101 @]

to:
           y = 1010
           x = y >> 1

resultado: x = 0101 @]

June 04, 2010, at 07:53 PM by Equipo Traduccion -
Changed lines 1-6 from:

Bit masks are used to access specific bits in a byte of data. This is often useful as a method of iteration, for example when sending a byte of data serially out a single pin. In this example the pin needs to change it's state from high to low for each bit in the byte to be transmitted. This is accomplished using what are known as bitwise operations and a bit mask.

Bitwise operations perform logical functions that take affect on the bit level. Standard bitwise operations include AND (&) OR (|) Left Shift (<<) and Right Shift (>>).

The AND (&) operator will result in a 1 at each bit position where both input values were 1. For example:

to:

Las máscaras de bits se utilizan para acceder a partes específicas de un byte de datos. Suele ser útil como método de iteración, por ejemplo cuando se envía un byte de datos en serie a un solo pin. En este ejemplo, el pin tiene que cambiar su estado de alto (1) a bajo (0) para cada bit en el byte a transmitir. Esto se consigue con lo que se conoce como operaciones bit a bit (bitwise operations) y una máscara de bits (bit mask). Las operaciones bit a bit realizan funciones lógicas que tienen efecto al nivel de bits. Las operaciones bit a bit estándar incluyen AND (&) OR (|) Left Shift o desplazamiento a la izquierda (<<) y Right Shift o desplazamiento a la derecha (>>)

El operador AND (&) resultará 1 en cada posición de bit donde ambos valores de entrada eran: 1. Por ejemplo:

Changed lines 11-12 from:

The OR (|) operator (also known as Inclusive Or) will result in a 1 at each bit position where either input values were 1. For example:

to:

El operador OR (|) también conocido como "Inclusive OR", resultará 1 en cada posición de bit en que cualquiera de los dos valores sea 1. Por ejemplo:

Changed lines 18-19 from:

The Left Shift (<<) operator will shift a value to the left the specified number of times. For example:

to:

El operador Left Shift (<<) desplaza los bits de un valor hacia la izquierda el número de posiciones especificadas. Por ejemplo:

Changed lines 25-28 from:

All the bits in the byte get shifted one position to the left and the bit on the left end drops off.

The Right Shift (>>) operator works identically to left shift except that it shifts the value to the right the specified number of times For example:

to:

Todos los bits en el byte son desplazados una posición a la izquierda y el bit más a la izquierda se pierde.

El operador Right Shift (>>) desplaza los bits de un valor hacia la derecha el número de posiciones especificadas. Por ejemplo:

Changed lines 34-42 from:

All the bits in the byte get shifted one position to the right and the bit on the right end drops off.

For a practical example, let's take the value 170, binary 10101010. To pulse this value out of pin 7 the code might look as follows:

[@byte transmit = 7; //define our transmit pin byte data = 170; //value to transmit, binary 10101010 byte mask = 1; //our bitmask byte bitDelay = 100;

to:

Todos los bits en el byte son desplazados una posición a la derecha y el bit más a la derecha se pierde.

Para un ejemplo practico, vamos a tomar el valor 170, que en binario es 10101010. Para sacar este valor secuencialmente por el pin 7, el código sería el siguiente:

[@byte transmitir = 7; // define el pin por que vamos transmitir byte valor = 170; // valor a transmitir, binario 10101010 byte mascara = 1; // nuestra mascara de bits byte tiempoEntreBits = 100; // Tiempo en milisegundos entre cada bit

Changed line 45 from:
   pinMode(transmit,OUTPUT);
to:
   pinMode(transmitir,OUTPUT);
Changed lines 50-52 from:
  for (mask = 00000001; mask>0; mask <<= 1) { //iterate through bit mask
    if (data & mask){ // if bitwise AND resolves to true
      digitalWrite(transmit,HIGH); // send 1
to:
  for (mascara = 00000001; mascara>0; mascara <<= 1) { // recorre la mascara de bits
    if (data & mascara){ // si el bit esta activado
      digitalWrite(transmitir,HIGH); // envia 1
Changed lines 54-55 from:
    else{ //if bitwise and resolves to false
      digitalWrite(transmit,LOW); // send 0
to:
    else{ // si el bit no esta activado
      digitalWrite(transmitir,LOW); // envia 0
Changed line 57 from:
    delayMicroseconds(bitDelay); //delay
to:
    delayMicroseconds(tiempoEntreBits); //espera
Changed lines 62-64 from:

Here we use a FOR loop to iterate through a bit mask value, shifting the value one position left each time through the loop. In this example we use the <<= operator which is exactly like the << operator except that it compacts the statement mask = mask << 1 into a shorter line. We then perform a bitwise AND operation on the value and the bitmask. This way as the bitmask shifts left through each position in the byte it will be compared against each bit in the byte we are sending sequentially and can then be used to set our output pin either high or low accordingly. So in this example, first time through the loop the mask = 00000001 and the value = 10101010 so our operation looks like:

to:

Aquí se utiliza un bucle FOR para iterar a través de una máscara de bits, cambiando el valor de una posición hacia la izquierda cada vez que entramos en el bucle. En este ejemplo se utiliza el operador <<= que es exactamente igual que el operador <<, salvo que se compacta la declaración mascara = mascara << 1 en una línea más corta.

A continuación, realiza una operación bit a bit de tipo AND entre el valor y la máscara de bits. De esta manera, como la máscara de bits se desplaza a la izquierda por cada posición en el byte, se comparará con cada bit en el byte que estamos enviando de forma secuencial y el resultado puede ser usado para fijar el pin de salida a nivel alto (1) o a nivel bajo (0) respectivamente.

Así que en este ejemplo, la primera vez que entra en el bucle la máscara es igual a 00000001 y el valor es igual 10101010 por lo que nuestra operación es así:

Changed lines 73-74 from:

And our output pin gets set to 0. Second time throught he loop the mask = 00000010, so our operation looks like:

to:

Por lo que la salida por el pin será 0.

La segunda vez que entra en el bucle, la máscara es igual a 00000010, por lo que la operación sería así:

Changed lines 82-86 from:

And our output pin gets set to 1. The loop will continue to iterate through each bit in the mask until the 1 gets shifted left off the end of the 8 bits and our mask =0. Then all 8 bits have been sent and our loop exits.

to:

Y la salida por el pin será 1.

El bucle continuará recorriendo cada bit en la máscara hasta que el 1 se pierda por la última posición de la izquierda de los 8 bits y la máscara será entonces 0 y el bucle terminará.

August 15, 2006, at 08:00 PM by Heather Dewey-Hagborg -
Changed line 63 from:

Here we use a for loop to iterate through a bit mask value, shifting the value one position left each time through the loop. In this example we use the <<= operator which is exactly like the << operator except that it compacts the statement mask = mask << 1 into a shorter line.

to:

Here we use a FOR loop to iterate through a bit mask value, shifting the value one position left each time through the loop. In this example we use the <<= operator which is exactly like the << operator except that it compacts the statement mask = mask << 1 into a shorter line.

August 15, 2006, at 07:48 PM by Heather Dewey-Hagborg -
Changed line 63 from:

Here we use a for loop to iterate through a bit mask value, shifting the value one position left each time through the loop. In this example we use the <<= operator which is exactly like the << operator except that it compacts the statement mask = mask << 1 into a shorter line.

to:

Here we use a for loop to iterate through a bit mask value, shifting the value one position left each time through the loop. In this example we use the <<= operator which is exactly like the << operator except that it compacts the statement mask = mask << 1 into a shorter line.

August 15, 2006, at 07:46 PM by Heather Dewey-Hagborg -
Changed line 21 from:

{@

to:

[@

August 15, 2006, at 07:45 PM by Heather Dewey-Hagborg -
Changed lines 1-2 from:

Bit masks are used to access specific bits in a byte of data. This is often used as a method of iteration, for example when sending a byte of data serially out a single pin. In this example the pin needs to change it's state from high to low for each bit in the byte to be transmitted. This is accomplished using what are known as bitwise operations and a bit mask.

to:

Bit masks are used to access specific bits in a byte of data. This is often useful as a method of iteration, for example when sending a byte of data serially out a single pin. In this example the pin needs to change it's state from high to low for each bit in the byte to be transmitted. This is accomplished using what are known as bitwise operations and a bit mask.

Changed lines 7-8 from:

[@x: 10001101 y: 01010111

to:

[@

    x:  10001101
    y:  01010111
Changed lines 14-15 from:

[@ x: 10001101

to:

[@

    x:  10001101
Changed lines 21-22 from:

y = 1010 x = y << 1

to:

{@

        y = 1010
        x = y << 1
Changed lines 30-31 from:

y = 1010 x = y >> 1

to:

[@

        y = 1010
        x = y >> 1
August 15, 2006, at 07:42 PM by Heather Dewey-Hagborg -
Changed line 61 from:

So in this example, first time through the loop the mask = 00000001 and the value =10101010 so our operation looks like:

to:

So in this example, first time through the loop the mask = 00000001 and the value = 10101010 so our operation looks like:

Changed lines 64-66 from:

&10101010 _________

 00000000
to:

& 10101010

  ________
  00000000
Changed lines 72-74 from:

&10101010 _________

 00000010
to:

& 10101010

  ________
  00000010
August 15, 2006, at 07:41 PM by Heather Dewey-Hagborg -
Changed line 76 from:

And our output pin gets set to 1. The loop will continue to iterate through each bit in the mask until the 1 gets shifted left off the end of the 8 bits and our mask =0.

to:

And our output pin gets set to 1. The loop will continue to iterate through each bit in the mask until the 1 gets shifted left off the end of the 8 bits and our mask =0. Then all 8 bits have been sent and our loop exits.

August 15, 2006, at 07:40 PM by Heather Dewey-Hagborg -
Changed lines 5-6 from:

The & operator will result in a 1 at each bit position where both input values were 1. For example:

to:

The AND (&) operator will result in a 1 at each bit position where both input values were 1. For example:

Changed lines 11-12 from:

The | operator (also known as Inclusive Or) will result in a 1 at each bit position where either input values were 1. For example:

to:

The OR (|) operator (also known as Inclusive Or) will result in a 1 at each bit position where either input values were 1. For example:

Changed lines 15-18 from:

x & y: 11011111@]

Let's take the value 170, binary 10101010. To pulse this value out of pin 7 out code might look as follows:

to:

x | y: 11011111@]

The Left Shift (<<) operator will shift a value to the left the specified number of times. For example: y = 1010 x = y << 1 yields: x = 0100 @]

All the bits in the byte get shifted one position to the left and the bit on the left end drops off.

The Right Shift (>>) operator works identically to left shift except that it shifts the value to the right the specified number of times For example: y = 1010 x = y >> 1 yields: x = 0101 @]

All the bits in the byte get shifted one position to the right and the bit on the right end drops off.

For a practical example, let's take the value 170, binary 10101010. To pulse this value out of pin 7 the code might look as follows:

Changed line 48 from:
    if (data & mask){ // if bitwise and resolves to true
to:
    if (data & mask){ // if bitwise AND resolves to true
Changed lines 57-59 from:

@]

to:

@]

Here we use a for loop to iterate through a bit mask value, shifting the value one position left each time through the loop. In this example we use the <<= operator which is exactly like the << operator except that it compacts the statement mask = mask << 1 into a shorter line. We then perform a bitwise AND operation on the value and the bitmask. This way as the bitmask shifts left through each position in the byte it will be compared against each bit in the byte we are sending sequentially and can then be used to set our output pin either high or low accordingly. So in this example, first time through the loop the mask = 00000001 and the value =10101010 so our operation looks like:

 00000001
&10101010
_________
 00000000

And our output pin gets set to 0. Second time throught he loop the mask = 00000010, so our operation looks like:

 00000010
&10101010
_________
 00000010

And our output pin gets set to 1. The loop will continue to iterate through each bit in the mask until the 1 gets shifted left off the end of the 8 bits and our mask =0.

August 15, 2006, at 07:19 PM by Heather Dewey-Hagborg -
Added lines 3-14:

Bitwise operations perform logical functions that take affect on the bit level. Standard bitwise operations include AND (&) OR (|) Left Shift (<<) and Right Shift (>>).

The & operator will result in a 1 at each bit position where both input values were 1. For example:

x:  10001101
y:  01010111
x & y:  00000101

The | operator (also known as Inclusive Or) will result in a 1 at each bit position where either input values were 1. For example:

  x:  10001101
    y:  01010111
x & y:  11011111
Changed line 41 from:

@]

to:

@]

August 15, 2006, at 07:11 PM by Heather Dewey-Hagborg -
Added lines 1-29:

Bit masks are used to access specific bits in a byte of data. This is often used as a method of iteration, for example when sending a byte of data serially out a single pin. In this example the pin needs to change it's state from high to low for each bit in the byte to be transmitted. This is accomplished using what are known as bitwise operations and a bit mask.

Let's take the value 170, binary 10101010. To pulse this value out of pin 7 out code might look as follows:

byte transmit = 7; //define our transmit pin
byte data = 170; //value to transmit, binary 10101010
byte mask = 1; //our bitmask
byte bitDelay = 100;

void setup()
{
   pinMode(transmit,OUTPUT);
}

void loop()
{
  for (mask = 00000001; mask>0; mask <<= 1) { //iterate through bit mask
    if (data & mask){ // if bitwise and resolves to true
      digitalWrite(transmit,HIGH); // send 1
    }
    else{ //if bitwise and resolves to false
      digitalWrite(transmit,LOW); // send 0
    }
    delayMicroseconds(bitDelay); //delay
  }
}


   

Share