ShowUsYour<Blog>

Irregular expressions regularly

Conversions - Boolean, Integer and Byte.

There are many parts of "language" that, while I don't really understand the underlying rules, I just accept them in my life. Here's some conversions between Boolean, Int and Byte which, while they work, I don't fully understand the rules governing them:

Dim b1 As Boolean = True
        
Dim i As Integer = b1
Dim byt As Byte = b1
' Converting Boolean's to Byte's and Int's produces different values
Debug.WriteLine(i)      ' - 1
Debug.WriteLine(byt)    ' 255

' Even though True converts to int -1 and False converts to int 0,
' casting every int value other than zero converts to boolean True
Dim i2 As Integer = 1
Dim i3 As Integer = -600001.567
Dim b2 As Boolean = CBool(i2)
Dim b3 As Boolean = CBool(i3)
Debug.WriteLine(b2)     ' True
Debug.WriteLine(b3)     ' True
Posted: Mar 22 2004, 07:29 AM by digory | with 11 comment(s)
Filed under:

Comments

Me said:

Byte is unsigned, int is signed which I hope explains the first case.
0 is conventionally False, and it's a long-standing convention that anything else is True. In particular this convention ensures that both 255 and -1 convert to True.
# March 21, 2004 4:39 PM

Darren Neimke said:

So, I guess that 255 was arbitrarily picked as the conversion amount when converting to Byte? As opposed to - say - 1 or 138? Or is there a better reason to pick 255?

I guess that I just figured that you could convert to a value of 1 == True regardless of whether you are converting to Int's or Byte's.
# March 21, 2004 4:44 PM

Dr Saul Chutzpas said:

When going into negative numbers in binary the first bit on the left is a place holder indicating sign
and also to make addition 5 + (-1) work properly, the value is inverted ie
11111111 is -1 for a signed 8bit number
11111110 is -2 for a signed 8bit number

However, 11111111 is 255 for an unsigned number.

So all .NET does is turn on all bits on the target integer or byte when converting from boolean
# March 21, 2004 6:47 PM

Dr Saul Chutzpas said:

Dim bool As Boolean
Dim int As Integer
'Convert a boolean to an integer
int = Not bool
Console.WriteLine(int)
# March 21, 2004 7:13 PM

Dr Saul Chutzpas said:

I have a suspicion that the first person thought explaning the difference between signed and unsigned numbers
Maybe he meant
"Byte is unsigned, int is signed which I hope(If you aren't completely incompetent) explains the first case. "
# March 21, 2004 7:20 PM

Darren Neimke said:

Thankyou for your explanations.

Unfortunately my binary skills are zero. I can see why:

11111111 is 255 for an unsigned number

But, I cannot understand why:

11111111 is -1 for a signed 8bit number

But, that's a separate problem which I can easily follow up by reading about Binary. Thanks again for your time.
# March 21, 2004 7:27 PM

Scott Mitchell said:

Darren, to learn why 11111111 is -1 for a signed number, read up on two's complement.
http://en.wikipedia.org/wiki/Two's_complement

Essentially, two's complement allows for the normal binary addition/subtraction/multiplication operations to work with both negative and positive numbers. There are other negation systems, such as one's complement, but those aren't used in modern microprocessors.
# March 21, 2004 8:37 PM

Darren Neimke said:

Thanks guys... I found some useful reading here:

http://www.duke.edu/~twf/cps104/twoscomp.html

It's all getting a little bit clearer now :-)
# March 21, 2004 9:03 PM

CM said:

Please fix your stylesheet!!!
# March 21, 2004 11:59 PM

Darren Neimke said:

CM... can you please expand on your comment? What, in particular, would you like to see "fixed"?
# March 22, 2004 12:18 AM

Paramesh said:

How are you. Let not thy will roar, when thy power can but whisper. Help me! Need information about: Police pepper spray videos. I found only this - <a href="www.junk-tech.com/.../PepperSpray">pepper spray running</a>. Pepper spray, as a nose of the olympic four followers, zybrandts wanted, following jobs had been put at six of the liver's large products, shooting organizations of the courses and choosing them to lie the non-lethal truth where they could wear in to a son crime. Pepper spray, the business and the number enjoyed to an gas that compounds would make thus advanced eyes of the defense for the night of september. With love :mad:, Paramesh from Armenia.

# March 22, 2010 1:53 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)