1. Home
  2. Computing & Technology
  3. Visual Basic
Computer Number Systems
Part 3: Numbers in Visual Basic
 More of this Feature
• Part 1: Computer Number Systems
• Part 2: Hex, Octal, and what is "Radix"
• Part 4: The Other VB Numerals
 
 Join the Discussion
Is this the kind of article that helps you?
Let us know!
 
 Related Resources
• Calculating Color
The first article in this series
• Symbolic Logic
The second article in this series
 
 Elsewhere on the Web
• The NAND Gate in Circuit Design
• The Definition of a Byte
• A Byte array tutorial
 

Visual Basic has a rich evolution in the way it represents numbers, but it's one that can be confusing too. Probably the most confusing thing has been the innovation of using the Variant data type to represent numbers. Up to VB 6, all numbers - in fact, all data - could be represented by a single data type called the Variant data type. A Variant is a special kind of data type that can contain different types of information, depending on how it is used. So, at the programming level, you could consider floating point, integers, and string representations of numbers to all be the same data type - the Variant data type. In fact, however, VB 6 uses the most compact data type that works and changes the type dynamically if necessary. As Microsoft Help states it, "A Variant variable is not a variable with no type; rather, it is a variable that can freely change its type."

After this "wonderful" innovation, VB .NET has come around full circle and stores data in a way that is similar to the way it was in the earliest versions of VB: binary, floating point, integer, and so forth. Wellllllll .... give 'em credit for finally figuring it out and now doing it (IMHO) the "right" way.

In any case, whether it is VB 6 or VB .NET, the actual data types used are roughly the same. VB .NET does introduce some new innovations, however, and we'll point them out. First, let's start with a data type that has been very important up to this point in discussing logical calculations and statements: the Boolean data type that is used for True and False. In explaining how logic calculations, we used 1 and 0 for True and False. While you could use these values in a program, in VB 6 and VB .NET, Boolean variables are stored as 16-bit (2-byte) numbers, and a Boolean variable can only be True or False. When other numeric types are converted to Boolean values, 0 becomes False and all other values become True. When Boolean values are converted to other data types, False becomes 0 and True becomes -1, not 1. This starts to make more sense if you look at these VB variables as though they were 16-bit integers. As integers ...
True = -1 = 1111 1111 1111 1111
False = 0 = 0000 0000 0000 0000 

The reason the binary equivalents are like this is that computers use something called "two's complement" to represent negative numbers. In fact, virtually every computer actually stores binary numbers using two's complement because the computer circuitry can handle arithmetic this way a lot faster.

First, a basic description of the method. Let's use our example number, 7382 (decimal). Earlier, we converted this to 1110011010110 (binary). It's important to start using the full 16 bit binary number when doing two's complement arithmetic.

To get the two's complement of this value, -7382, write out the number in binary. Then invert the digits and add one to the result. For out example:

0001110011010110

Then invert the digits. 0 becomes 1, 1 becomes 0.

1110001100101001

Then we add 1.

1110001100101001
               1
1110001100101010

Notice that the two's complement of the number can simply be added to another number to get the right answer (throwing away the high order 1 that is carried during the addition):

 9876 = 0010011010010100
-7382 = 1110001100101010
-----   ----------------
 2494 = 0000100110111110

To come "full circle" on the topic of two's complement, consider the two's complement of the number -1 (decimal).

the number        0000000000000001
invert the digits 1111111111111110
add 1             1111111111111111

So, -1 is 1111111111111111 in two's complement.

Next page > The Other VB Numerals > Page 1, 2, 3, 4

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2008 About.com, a part of The New York Times Company.

All rights reserved.