# VBA Number Types: Byte

## General information about "Byte"

### From 0 to 255

In VBA, a number of the type `Byte` is a number that can contain values in {0,1,..., 255}.

The following VBA code works.

When you assign a value outside of the range {0,1,..., 255}, the VBA code will crash. At run time, it will generate an overflow error.

### Hexadecimal assignment

You can use hexadecimal assignment in VBA.

### No fractional values

A number of the type `Byte` cannot contain fractional values, like 3.1415. Be careful, because of an implicit conversion, this VBA code does work.

### Default

On declaration, by default, VBA assigns the value 0 to the number of the type `Byte`.

### One byte

A number of the type `Byte` takes up one byte in the memory. In a byte, there are 8 bits, each of which can take the value 0 or 1. Hence, a byte can have 28 possible bit representations. Each of these 256 possible bit representations corresponds to one value in {0,1,..., 255}.

To illustrate, one possible bit representation could be as follows. The VBA bit representation might be slightly different, but the logic is the same.

BitsValue
000000000
000000011
000000102
000000113
...
11111110254
11111111255

Here is how to read the bit representation. For instance, the bit representation 01000001

Bit 10 x 128=0
Bit 21 x 64=64
Bit 30 x 32=0
Bit 40 x 16=0
Bit 50 x 8=0
Bit 60 x 4=0
Bit 70 x 2=0
Bit 81 x 1=1

corresponds to 65 (= 64 + 1).

### Doing +,-,/ etc.

Even with `Byte`, you can make additions, subtractions, multiplications and so on. Numercial operations are exact, provided that you stay within the range {0,1,..., 255}.

This VBA code will crash because the result is outside the range {0,1,..., 255}.

Of course, division by zero is forbidden. This VBA code will also crash.

Be very careful when doing numerical operations. This VBA code does not crash.

It is probably not a good idea to use numbers of the type `Byte()` for doing mathematics. The range {0,1,..., 255} is very restrictive. The use of the type byte lies elsewhere.

### Conversion to `Byte`

VBA offers a conversion function `CByte()` that will try to convert anything into a number of the type `Byte`.

### The link to extended ASCII

ASCII stands for American Standard Code for Information Interchange. The ACSII character set consists of 128 characters, and is probably the most well known character set in the (Western) world. Each ASCII character uses 7 bits which allows for 2^7 possible values. Some ASCII characters are printable, others are not. Over time, more characters were needed and an 8th bit was added. ASCII character sets that use 8 bits - or one byte - are called extended ASCII sets. There are many extended ASCII sets. In each extended ASCII set, each value in {0,1,..., 255} corresponds to one character.

Also the function `Asc()` can be seen as a conversion function to a number of the type `Byte`. The function `Asc()` converts an extended ASCII character to a number in the range {0,1,..., 255}.

### Bit operations

VBA supports a limited number of bit operations.

There are no bit shift operators in VBA. However, if needed, the bit shift operators can be programmed.