PDA

Click to See Complete Forum and Search --> : [VB] boolean


dis1411
Dec 25th, 2003, 02:01 AM
i vaguely remember reading something about how in vb when a boolean variable is used, it is supposed to be 1 byte but actually allocates a lot more space

anyone even think im not making this up?

also do all variables end up working this way or what

visualAd
Dec 26th, 2003, 08:16 AM
Boolean is basically True | False. They may be the only two values which you can read from a boolean variable but in computer terms True and False are defined as the following.

True: Any non 0 value.
False: Zero 0

In actual fact when you declare a boolean data type it uses 16bits (2 bytes). The following code will reveal how the computer stores a boolean value:

Private Sub Form_Paint()
Dim bTrue As Boolean, bFalse As Boolean

bTrue = True
bFalse = False

Print "bTrue = ", CInt(bTrue)
Print "bFalse = ", CInt(bFalse)
End Sub

It is also perfectly valid to assign a bollean variable any number: i.e bTrue = 2e45. On doing so the computer will convert the number to the booolean -1 and 0.

I am not sure why a 16bit variable size is used to store the two values. It may be an issue of optimization. Or for backward compatibility with 16 bit systems.

Maybe someone else can help out here?? ;)

Protocol
Dec 26th, 2003, 12:06 PM
It may be an issue of optimization. Or for backward compatibility with 16 bit systems.

Sounds the most logical....

dis1411
Dec 26th, 2003, 03:24 PM
so it would technically be more efficient to declare it as a byte

u can use it exactly as u would a boolean, but its only a byte, so only takes 1 byte of memory

visualAd
Dec 27th, 2003, 01:45 AM
Originally posted by dis1411
so it would technically be more efficient to declare it as a byte

u can use it exactly as u would a boolean, but its only a byte, so only takes 1 byte of memory
Probably not, becuase a Boolean value is treated differently to a byte. When you declare a Boolean: it means you can assign it any value and it will be converted to True or False. e.g:

Dim myBln As Boolean
Dim myByt As Byte

' valid - value is converted to true
' before being assigned to myBln
myBln = 1345.55

' invalid - casues an overflow error
myByt = 1234.55

' valid - but not as easy as using Boolean
myBtye = CBool(1234.55)

It also means that when you do any True | False comparisons such as:

If myByt Then
doStuff.....
End If

The computer is made to convert the byte value to Boolean type each time. Whereas, if it had already been declared as a Boolean that would only have been done once when the value was assigned.

The only way declaring it as a byte would make it more efficient is in saving a little memory. If you are that concerned about memory consumption you should move to C++.

In reality at processor level all numbers are converted to a 32 bit float and shoved through 32 bits at a time - (unless you own a 64bit processor). Simply becuase that is the size of the bus and also becuase it is a lot more efficient for the processor to handle all its calculations using the same data type. So using one byte instead of 2 makes no difference.

My best advice use Boolean as the data type.

Kasracer
Dec 27th, 2003, 01:57 AM
Originally posted by visualAd

In reality at processor level all numbers are converted to a 32 bit float and shoved through 32 bits at a time - (unless you own a 64bit processor). Simply becuase that is the size of the bus and also becuase it is a lot more efficient for the processor to handle all its calculations using the same data type. So using one byte instead of 2 makes no difference.
That isn't true, it just means it can receive 32bytes at once, not everything is converted to 32byte floats. If that were true, holy **** would computers be slow.

visualAd
Dec 27th, 2003, 02:53 AM
I think you'll find they do. The processor only handles floating point numbers hence the term FLOP. They also have only one type of logic gate: e.g an AND gate. These are then used to construct other gates such as XOR, NOT, NAND and OR.

Whats more - it can only add :D

Pc_Madness
Dec 27th, 2003, 03:57 AM
Originally posted by visualAd
Boolean is basically True | False. They may be the only two values which you can read from a boolean variable but in computer terms True and False are defined as the following.

True: Any non 0 value.
False: Zero 0

In actual fact when you declare a boolean data type it uses 16bits (2 bytes). The following code will reveal how the computer stores a boolean value:

Private Sub Form_Paint()
Dim bTrue As Boolean, bFalse As Boolean

bTrue = True
bFalse = False

Print "bTrue = ", CInt(bTrue)
Print "bFalse = ", CInt(bFalse)
End Sub

It is also perfectly valid to assign a bollean variable any number: i.e bTrue = 2e45. On doing so the computer will convert the number to the booolean -1 and 0.

I am not sure why a 16bit variable size is used to store the two values. It may be an issue of optimization. Or for backward compatibility with 16 bit systems.

Maybe someone else can help out here?? ;)

Possibly because of binary? 8 bits reserved for a zero, and 8 bits reserved for -1/ 1

Each digit in, "11110000" is a bit yeah?

Maybe? :unsure:

visualAd
Dec 27th, 2003, 09:45 AM
Using two's compliment -1 is:

11111111 11111111 - (represented in 2 bytes)

and zero is:

00000000 00000000 - (represented in 2 bytes)

I'd imagain that is how they store it.

Kasracer
Dec 27th, 2003, 05:57 PM
Originally posted by visualAd
I think you'll find they do. The processor only handles floating point numbers hence the term FLOP. They also have only one type of logic gate: e.g an AND gate. These are then used to construct other gates such as XOR, NOT, NAND and OR.

Whats more - it can only add :D After checking out some info, what you said was semi true. Nothing is EVER converted into a 32bit float, however, integers, ect are all put INTO 32bit registers (I think that is the right word)

So a 16bit integer would be placed into a 32bit register. It isn't converted, the space just doesn't get used (i.e. a bunch of zeros would be added to the front of the binary number)

CornedBee
Dec 29th, 2003, 03:32 PM
You people ought to read the first few chapters of the DOS version of The Art of Assembly Lanugage Programming. Find the link in the FAQ of the Assembly forum.

CornedBee
Dec 29th, 2003, 03:33 PM
The term FLOPs/s (FLoating point OPerations per second) comes from supercomputing. Supercomputers usually do large simulations where the only interesting calculation is one on floating point numbers.

visualAd
Dec 29th, 2003, 06:06 PM
I quote what one of my computing tutors told me.

He stated that it is cheaper and more effficient for the processor to do one type of calculation on one data type, which will usually be a floating point data type the size of the bus (e.g 32bits).

I may have miss understood him. I must say this is a very interesting subject though.

CornedBee
Dec 30th, 2003, 04:18 AM
Intel's HyperThreading technology relies on the fact that a CPU has various calculation areas (floating point, integers, ...) but is using only one at a time.

CornedBee
Jan 22nd, 2004, 11:25 AM
I just recently learned how floating point maths works.

Addition in floating point is quite complicated, so I suppose it's ages slower than integral addition.