Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - If a Boolean constant value of True is assigned to an integer variable, why is the value of the integer variable-1? Can you explain it in detail From VB rookie
If a Boolean constant value of True is assigned to an integer variable, why is the value of the integer variable-1? Can you explain it in detail From VB rookie
Boolean value itself is an integer, but programming languages (such as VB) set constants such as True and False for the convenience of programmers, but their actual values are-1 and 0.

Why-1 and 0? Let's see:

True =-1( 10 radix) =&; HFFFF( 16 radix) =11111(binary).

False = 0( 10 radix) =&; H0000( 16) = 0000000 (binary)

As you can see,-1 is an 8-bit integer of 1, and 0 is an 8-bit integer of 0, which explains why you can get False without operation on True, and you can get True without operation on False, because these two numbers are completely opposite!

So the actual value of True is-1. You can verify it yourself in the instant window of VB:

false+ 1

false * 100

- 100

true/ 10

-0. 1

Do you see it? Really can directly participate in numerical operation! Because it is actually-1!

To add: If you want to convert an ordinary numerical value into a Boolean value, the rule of VB (similar to other languages) is that any non-zero value is true and 0 is false. For example, judging whether a number is not equal to 0 is generally done like this;

If x<& gt is 0.

But it can actually be reduced to

If x, then