Functions For Bits And Bytes In VBA
This page describes VB/VBA procedures you can use to work with bits and bytes.
The VB (Classic) and VBA programming languages provide few operations for working with bits and bytes, especially when
compared to programming languages like C and C++. This page provides descriptions
You can download a module file that contains all the code describe on this page.
The functions described on this page work almost entirely with Long data types. A brief review of
integral data types may prove useful. These types can store integral data, not fractional data. That is, they can store only
whole numbers. We will not be discussing the data types that can contain fractional data.
A bit is smallest and most fundamental representation of data. It is can have only one of two
possible values: 1 or 0. When a bit has a valid of 1, it is said to be "On" or "Set". When the bit has a value of 0, it is
said to be "Off" or "Clear". To "flip" or "invert" a bit is to change its value from 0 to 1 or from 1 to 0, basically toggling its
value between the two possible values. There is no direct programmatic way, other than using logical operators in the manner described
on this page, to read or write the value of a bit.
A nibble is a set of four bits. Taken together, these bits can store a value between 0 (all bits off) and 15 (all bits on). While the term
nibble is used on this page on in other documentation, you cannot declare a variable of the data type nibble.
The smallest unit of storage that can be allocated is a byte. It is common practice to use a base-16 number format
called hexadecimal (or, usually, just hex) to represent the value of a nibble. Hex uses the characters "0" to "9" to refer to the normal,
decimal values 0 to 9, and the letters "A" to "F" to represent the values 11 through 15. Using hex notation, any value that can be stored
in a nibble can be represented by a single character. In VB/VBA, any hex value (of any length) is prefixed with the characters &H
to indicate that hex notation is being used.
A byte is (almost always) a group of 8 bits (or 2 nibbles). (In the telecom industry, especially among the older generation, a byte is
called an octet.) A byte is the smallest unit of memory that can be allocated and the smallest
unit to which you can assign a value without using any of the logical functions of the sort described on this page. A byte can contain any value between 0 and
255. As with nibbles, it is very common to use hexadecimal notation to specify the value of a byte. Since a byte is 2 nibbles, it takes to hex characters
to display a byte's value. For example, the decimal value 120 is expressed in hex as &H78. It is critical to use the
&H prefix when using hex notation. The prefix is used to specify that &H78 is the hex value (equal to
decimal 120), not the decimal 78 (the number equal to 7*10 + 8). A byte is an unsigned type, which means it can contain only positive numbers. A byte
cannot be used to store negative numbers. A byte can have a value between 0 (H&00, 0000 0000 binary) and 255
(&HFF, 1111 1111 binary).
An Integer is a 2 byte element that can store whole numbers between -32,768 and 32,767. Integers are signed quantities, meaning that they
can store both positive and negative numbers. (Some programming languages allow you to declare an integer to be either signed or unsigned. VB/VBA does not provide this
option and uses only signed values. Integers can be displayed with four hex digits, in the range of &H8000 to &H0000 for
negative numbers and in the range &H0000 to &H7FFF for positive numbers. For example, the decimal value 4660 can be
displayed in hex as ?&H1234. If you examine the hex numbers closely, you'll see why they are so commonly used. The values and sequence of digits
conveys valuable information about the bytes and bits from which they are composed. With experience, you'll see that, for example, &H8000 is
a nice, even number -- a 1 followed by seven 0s. Its decimal equivalent, -32,768, provides no information about the underlying bits and bytse.
Finally, we have the Long data type. This is a 32-bit signed integer. It can contains whole number values from -2,147,483,648 to
+2,147,483,647. It can be represented by a sequence of 8 hex characters, from &H80000000 to &H7FFFFFFF.
The functions described on this page and available in the downloadable file fall into the following
catagories:
- Testing the status of one or more bits in a Long.
- Setting or clearing one or more bits in a Long.
- Shifting and rotating the bits of a Long.
- Examining or changing a byte or integer within an Integer or Long.
- Conversion of a decimal number to either hex or binary, or vice versa.
This is some content text.
This page last updated: