Thursday, April 26, 2007

True in Visual Basic is "-1". Why???

People have been wondering why Visual Basic represents True as -1 whereas most of the other languages consider 1 as true. Here's a brief explanation for why it is like that.

In 2's complement number system, range of numbers that can be represented is -2n-1 to 2n-1 - 1 where n represents the number of bits. For representing True or False, only 1 bit is required and, thus, n = 1. Therefore, according to that formula, a single bit can represent numbers ranging from -1 to 0. 0 is considered as false and -1 is considered as true.

No comments: