Share via


Byte (Tipo de datos, Visual Basic)

Contiene enteros de 8 bits sin signo (1 bytes) que se sitúan en el intervalo entre 0 y 255.

Comentarios

Utilice el tipo de datos Byte para contener datos binarios.

El valor predeterminado de Byte es 0.

Sugerencias de programación

  • Números negativos. Dado que Byte es un tipo sin signo, no puede representar un número negativo. Si utiliza el operador menos (-) unario en una expresión que produce un resultado del tipo Byte, Visual Basic convierte primero la expresión a Short.

  • Conversiones de formato. Cuando Visual Basic lee o escribe archivos o cuando llama a archivos DLL, métodos y propiedades, puede convertir automáticamente entre los formatos de datos. Los datos binarios almacenados en variables Byte y matrices se conservan durante estas conversiones de formato. No debería utilizar una variable String para datos binarios, ya que su contenido puede dañarse durante la conversión entre los formatos ANSI y Unicode.

  • Ampliación. El tipo de datos Byte se amplía a Short, UShort, Integer, UInteger, Long, ULong, Decimal, Single o Double. Esto significa que se puede convertir Byte en cualquiera de estos tipos sin encontrar un error OverflowException.

  • Caracteres de tipo. Byte no tiene caracteres de tipo literal ni caracteres de tipo identificador.

  • Tipo en Framework. El tipo correspondiente en .NET Framework es la estructura Byte.

Ejemplo

En el ejemplo siguiente, b es una variable Byte. Las instrucciones muestran el intervalo de la variable y la aplicación de operadores de desplazamiento de bits a ella.

' The valid range of a Byte variable is 0 through 255. 
Dim b As Byte
b = 30
' The following statement causes an error because the value is too large. 
'b = 256 
' The following statement causes an error because the value is negative. 
'b = -5 
' The following statement sets b to 6.
b = CByte(5.7)

' The following statements apply bit-shift operators to b. 
' The initial value of b is 6.
Console.WriteLine(b)
' Bit shift to the right divides the number in half. In this  
' example, binary 110 becomes 11.
b >>= 1
' The following statement displays 3.
Console.WriteLine(b)
' Now shift back to the original position, and then one more bit 
' to the left. Each shift to the left doubles the value. In this 
' example, binary 11 becomes 1100.
b <<= 2
' The following statement displays 12.
Console.WriteLine(b)

Vea también

Referencia

Resumen de tipos de datos (Visual Basic)

Byte

Funciones de conversión de tipos (Visual Basic)

Resumen de conversión (Visual Basic)

Conceptos

Uso eficiente de tipos de datos (Visual Basic)