True / False in classic VB

In classic VB (and classic ASP as well), zero is considered as false and any other non-zero integer is considered as true.

While debugging an asp page, I come across following line:

If Not Request.Form.Count Then

Now form count was 2 (true) so the condition should evaluate as false (Not true). But this was always evaluating as true. It took me half an hour to find the catch.

What actually happening here was that Not is a bit-wise operator in VB. It flips the bits. So,

2 is 0000 0010 in binary.
NOT 2 is 1111 1101 in binary.

Considering that the last bit is sign bit, this becomes -3.

Now our condition If NOT (2) becomes If (-3), which always evaluates to true (non-zero integers are always true in VB).

Technorati Tags: ,,,
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s