The VBScript Network and Systems Administrator's Cafe

Apr 25 2008   3:19PM GMT

How VBScript variable types are represented as binary numbers

Jerry Lees Jerry Lees Profile: Jerry Lees

In a previous posting,  Converting variable types in VBScript from one type to another type, I discussed very briefly binary numbers and promised to write another entry with more detail. If you recall we we were discussing the size a number takes up to represent it in binary form.

Everyone is comfortable with the numbers 1 and 0, even in binary. And if you’re in IT, you’re probably even comfortable with binary 10, which isn’t Ten– it’s two. Normally we count in tens (with ten digits in each place), but computers count in twos (with only two digits in each place), which is base-2 instead of base-10 like we are used to counting.

 Consider this base-10 table with the column labels at the top and the number of each column below.

10000

1000

100

10

0

9

8

1

5

3

This might look a bit abstract the way it’s presented. But if you think back to elementary school the columns in a number represent the 10′s, 100′s, etc for the number. But the number is 98,153 or (90,000 +8000+ 100+50+3)

Now consider the following binary number, again with the column label at the top and the number of each column at the bottom:

16

8

4

2

1

1

0

1

1

1

This looks a bit more cryptic, but it’s actually simpler– we’ve just been trained to think in decimal. To do the first one we actually had to do multiplication in our head. It wasn’t (90,000 +8000+ 100+50+3)— it was actually(9* 10,000) + (8* 1000) + (1 * 100) + (5 * 10) + (3* 1)… That’s rather confusing when you spread it out, isn’t it? but with only 1′s and 0′s we don’t need to do multiplication. (Because 1 * anything is the number, and 0 times anything is 0)

So this is simply 16+4+2+1, or 23! It’s just a matter of getting to start thinking in powers of 2!

Now, Since I’ve shared with you previously that a double is larger than an integer, with respect to space, we’ll continue with that example. If you refer here you’ll notice that an integer is 2 bytes in Size where a double is 8 bytes in size. What’s 400% more space among friends, right? Quickly doubles can get out of control is you’re using bunches of them, or placing them in a Database one item at a time… This is sometimes where developers eat up all our space on the servers, in addition to the backup of the backups in the backups folder that’s stored in a Do not delete – Save this stuff folder. ;-)

The other thing that can be VERY bad! is using a variable type that is to small. Did you notice in the previous link that in Integer represents -32,768 to 32,768? What happens if you add 1 to 32,768? The answer for us is, of course, 32,769! The problem is computers can’t represent that number in an integer so you get a overflow and the number represented actually looks like -32768 to the computer!

 The moral of the story is to always try to use the right type of variable if a variant doesn’t work for you, and if you plan on using anything other than VBScript, VB, VB.NET, or VBA to create an admin tool– get accustomed to the different variable types.

Enjoy!

Extra credit: 10 in base-10 numbering is Ten, 10 in base-2 numbering is 2… what is 10 in Base-16 numbering and what is it commonly called?

2  Comments on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Stoinov
    nice article. reminds me of my high school (actually technical school) classes. 10 in base 16 is A :)
    60 pointsBadges:
    report
  • Jerry Lees
    HA! Yes, you are correct 10 (Decimal is represented as "A". I wasn't clear enough though. The number "10" in HEXADECIMAL is actually 16 in decimal and 10000 in binary... a bit more than a nibble ;-) Oh my... that WAS a bad pun.
    5,335 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: