The additive inverse of one. (-1 + 1 = 0)

Also, 1's negative square root. (-1^2 = 1)

Also, the square of i. (i^2 = -1)

What most sane libc functions return on error. Seems to be great UNIX tradition, though some things like POSIX threads break it.

If the function returns a pointer, then it's obviously going to return NULL on error. I was using an old Borland DOS compiler only to see it returned -7 on error, how crazy/random is that?!
One of the hardest basic things that a C / C++ programmer has to get used to when working with Visual Basic is the concept of truth in VB is numerically equal to -1. The reserved word "True" is equal to -1, because of this, and can be a bit strange to work with at first.

Therefore, a statement like

if MyFunction(myArgs) Then

end if
...Would fail if your program returns 1. It is a good VB practice to always return True as if it were an atomic object, rather than the number -1. This flies in the face of conventional programming, where -1 is oftentimes an error.

Correction: thanks to yerricde* VB returns -1 because it is an integer with all it's bits as 1's. This is very useful for ANDing something against it.

I find myself sticking to C conventions many times, and returning numbers from my programs, even if it may not work as well. It is best to resign yourself to one practice or the other, and not mixing the two. Functions may fail unexpectedly, if you return 1, and not -1. Use the words True and False, and try not to do more than what it was designed to do; play by the rules of the game, and you will survive the ordeal.

* I guess you learn something new every day

For the past several months, I've been unfortunate enough to do lots of programming in VB6. In that time, I've learned a lot of little quirks. Some specific to VB, some relative to programming in general.

Contrary to what is stated above, it will not fail if you just use If Method(arg) Then. Only the return value 0 will evaluate to False, and anything else (including 1 and -1) will evaluate to True.

It is preferable to return a boolean from a method when control structures depend on that return value, but there are alternative techniques. If the method's return value has to be a number that is actually used for other things, and you just happen to be using it in a conditional statement, it's better to explicitly compare the return value to some other value.

'For example: If Method(arg) > 0 Then
  'things are pretty clear this way
End If
'or
If Method(arg) <> -1 Then
  'this, too, easily readable
End If

As far as True being equal to -1, it may seem strange, but it makes a little sense if you understand a few other things. All logical operators in VB (And, Or, Xor, Not, whatever) are bitwise. In VB, the expression 7 And 8 will result in 0 because 7 is 00000111 and 8 is 00001000. Neither have any bits in common that are on, so the result is 00000000.

In other languages, there are both the bitwise operators (&, |, ^, ~) which of course operate on the bits of a value, and the logical operators (&&, ||, !) which only operate on boolean values.

The Boolean type in VB does not store its value in a single bit, but in a byte, and VB also has no unsigned integer types. Hopefully you've already thought ahead and realized what happens, but here's the long and short of it:
False is 00000000
Not False is the same as True
Not is bitwise
Therefore, Not 00000000 is 11111111, which in a signed integer is -1.

They had to make True equivalent to -1 because if it was merely 1 (00000001), then Not True would evaluate to -2 (11111110). This happens with any signed integer, not just the 8-bit variety.


Another funny thing with -1 is something a coworker accidentally did and could not figure out (although he has a Masters Degree in Computer Science and should have been seen it coming). He's writing a program to pull data from one database and stick it in another (both are different types and structures), and there was an integer column where some values were -1. After he did a test conversion, he was verifying the data, but found that the fields that were previously -1 were now 18446744073709551615.

It tried to assign -1 to the field, right? Right, but there's a snag. A signed integer with the value -1 means all the bits are 1 instead of 0. So what happens when you try to assign a value with all bits on to an unsigned integer? You get that integer's highest possible value because Two's Complement has nothing to do with unsigned integers, so all bits set to 1 just means the highest possible value, and a 64-bit integer's highest value is 18446744073709551615.

Log in or register to write something here or to contact authors.