Diferencia entre revisiones de «bit()»

De ArduWiki
Saltar a: navegación, buscar
(Parámetros)
(Ejemplo 3)
 
(No se muestran 13 ediciones intermedias del mismo usuario)
Línea 1: Línea 1:
 
== Descripción ==
 
== Descripción ==
Calcula el valor del bit especificado por el parametro.
+
Calcula el valor del bit especificado por el parámetro.
 
+
{| class="wikitable"
{| class="wikitable col2cen"
 
!bit!!valor
 
|-
 
|0||1
 
|-
 
|1||2
 
|-
 
|2||4
 
|-
 
|4||8
 
|-
 
|5||16
 
 
|-
 
|-
|6||32
+
| bit || 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7
 
|-
 
|-
|7||64
+
| valor || 1 || 2 || 4 || 8 || 16 || 32 || 64 || 128
 
|}
 
|}
  
Línea 31: Línea 19:
  
 
== Retornos ==
 
== Retornos ==
Retorna el bit 2^n
+
Retorna el bit 2^valor o 2^variable.
  
 
== Advertencias ==
 
== Advertencias ==
 
Nada.
 
Nada.
  
== Ejemplo ==
+
== Ejemplos 1 ==
Parpadea el LED a bordo (pin 13).
+
<syntaxhighlight lang="c++">
 +
bit(2);    //4
 +
bit(5);    //32
 +
bit(8);    //256
 +
</syntaxhighlight>
  
 +
== Ejemplos 2 ==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 
void setup(){
 
void setup(){
   pinMode(LED_BUITIN, OUTPUT);
+
   Serial.begin(9600);
 +
  for (byte i=0; i<10; i++){
 +
      Serial.println(bit(i));
 +
  }
 
}
 
}
 
void loop(){
 
void loop(){
   PINB = bit(5);
+
   //Nada
   delay(500);
+
}
 +
</syntaxhighlight>
 +
 
 +
== Ejemplo 3 ==
 +
Parpadea el LED a bordo.
 +
 
 +
<syntaxhighlight lang="c++">
 +
void setup() {
 +
  DDRB = DDRB | bit(5);    //Configura el pin digital 13 (bit 5) como OUTPUT, no altera el resto.
 +
}
 +
void loop() {
 +
  PORTB = PORTB ^ bit(5); //Invertir estado del pin digital 13 (bit 5), no altera el resto.
 +
   delay(500);
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== Vea también ==
 
== Vea también ==
* [[bitClear()]]
+
<categorytree mode=all>Funciones bit y byte</categorytree>
* [[bitRead()]]
+
 
* [[bitSet()]]
+
<categorytree mode=all>Operador bit a bit</categorytree>
* [[bitWrite()]]
 
* [[highByte()]]
 
* [[lowByte()]]
 
  
== Referencias ==
+
== Referencias externas ==
 
* [https://www.arduino.cc/reference/es/language/functions/time/millis/ Guia de referencia de Arduino]
 
* [https://www.arduino.cc/reference/es/language/functions/time/millis/ Guia de referencia de Arduino]
  
[[Category:Funciones]]
+
[[Category:Funciones bit y byte]]

Revisión actual del 18:29 17 jul 2019

Descripción

Calcula el valor del bit especificado por el parámetro.

bit 0 1 2 3 4 5 6 7
valor 1 2 4 8 16 32 64 128

Sintaxis

bit(valor);
bit(variable);

Parámetros

valor
valor a evaluar.
variable
variable a evaluar.

Retornos

Retorna el bit 2^valor o 2^variable.

Advertencias

Nada.

Ejemplos 1

bit(2);     //4
bit(5);     //32
bit(8);     //256

Ejemplos 2

void setup(){
   Serial.begin(9600);
   for (byte i=0; i<10; i++){
      Serial.println(bit(i));
   }
}
void loop(){
   //Nada
}

Ejemplo 3

Parpadea el LED a bordo.

void setup() {
   DDRB = DDRB | bit(5);    //Configura el pin digital 13 (bit 5) como OUTPUT, no altera el resto.
}
void loop() {
   PORTB = PORTB ^ bit(5);  //Invertir estado del pin digital 13 (bit 5), no altera el resto.
   delay(500);
}

Vea también



Referencias externas