1. Technology
Computer Number Systems
Part 4: The Other VB Numerals
 More of this Feature
• Part 1: Computer Number Systems
• Part 2: Hex, Octal, and what is "Radix"
• Part 3: Numbers in Visual Basic

VB can store information as binary or hex numbers (-1 and 0 are considered decimal integers in VB), but there are no built in data types called "binary" or "hexadecimal". Keep in mind that the illustrations above don't necessarily represent something you can do directly in VB. When a binary number like 101010 is shown, it represents the bits that are actually present, but there is no way VB can create or display that binary value directly. In other words, you can't code a statement like: binVar = 101010 where binVar is a "binary data type" because there is no "binary data type" in VB.

You can use the Byte data type to manipulate just one 8 bit segment in memory directly. As we have carefully pointed out, regular numeric types such as Integer impose their own value conversions (such as two's complement) as the data is stored and retrieved. If you want to be certain that the actual binary or hex value itself is stored, the Byte datatype is useful. The Byte data type references a single 8 bit unit of memory - which is pretty small. So Byte variables are nearly always used as Byte arrays. Manipulating individual bits has to be done through code. An example showing how to do this can be found here.

In a similar way, you can often run into problems using hex. There is some support for direct hex coding in terms of the hex literal prefix: & and the Hex function.

For example, if you use this code in a program:

    Dim lngX As Long
    lngX = &HFFFF ' &H indicates that this is a hex literal
    Debug.Print Hex(lngX)

The result that is displayed is:

FFFFFFFF

Even though you only assigned a four digit hex value to the variable, Visual Basic extends the hex F digits to the entire variable because it is interpreted at a negative number.

The numeric data types supplied by Visual Basic 6 are:

  • Byte
  • Integer
  • Long
  • Single
  • Double
  • Currency
  • Decimal

VB .NET changes this list. For one thing, like everything in VB .NET, variables are objects. The object names follow the name of the types in the list below. Also, Currency is gone and there are three different sizes (up from two) of Integer. The numeric variables supported by VB .NET are:

  • Byte (System.Int)
  • Short (System.Int16)
  • Integer (System.Int32)
  • Long (System.Int64)
  • Single (System.Single)
  • Double (System.Double)
  • Decimal (System.Decimal)

Our focus in this article was to dig down to the very lowest level of numbers in VB rather than cover the more standard programming issues using floating point and integer calculations so we won't launch into any of the details there. Hopefully, this look at the lowest level of numbers will make help make you a better programmer. To close this article, however, consider this quote by one of most important mathematicians in recent history:

Through and through the world is infested with quantity. To talk sense is to talk quantities, It is no use saying the nation is large- how large? It is no use saying that radium is scarce- how scarce? You can not evade quantity. You may fly to poetry and music and quantity and number will face you in your rhythms and your octaves.

Alfred North Whitehead (1861 - 1947)

First page > Computer Number Systems > Page 1, 2, 3, 4

©2014 About.com. All rights reserved.