Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Does Boolean = Byte?

Author
Message
Robert The Robot
19
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 12th Jan 2011 20:29
In theory, a boolean is a single bit and so can only be 1 or 0, while a byte is 8 bits and so can range from 0 to 255. However, I'm a bit confused because DBPro lets me do this:

and for me it prints 255, which it shouldn't.

I don't think I'm overwriting memory outside the boolean field, though, as this code works:


I have two boolean fields next to each other and set TestArray(0).One to 255, which (if I am overwriting memory) should change TestArray(0).Two from zero - yet it doesn't. It's as though each boolean field has a full byte of memory associated with it.

So, is the Boolean field here really a Byte by another name?

We spend our lives chasing dreams. Dark Basic lets us catch some of them.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 12th Jan 2011 20:34 Edited at: 12th Jan 2011 20:34
A boolean always uses at least a full byte, regardless of the language, it's just that most languages enforce the restriction that it can only be 1 or 0, whereas DBPro does not

[b]
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 12th Jan 2011 21:18
In the case of my plug-ins, I try to convert DBPro's 'byte' booleans to actual booleans. This is the case in both the bank plug-in and the file plug-in, but not the Arglist plug-in (as I took a shortcut to reduce code).

If you want an actual 0/1 boolean, then simply compare you value against 0:


kaedroho
18
Years of Service
User Offline
Joined: 21st Aug 2007
Location: Oxford,UK
Posted: 12th Jan 2011 21:19
You shouldn't use booleans in DBPro. Use integers instead. The reason why is that DBPro converts booleans to integers before doing any calculations with them. These conversions are quite slow. Dwords, words and bytes also do this.

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 12th Jan 2011 21:37
Also... Is it just me, or do bitwise manipulations break when you're using bytes? I had a bunch of trouble a while back when I was using bytes and bitwise operators, then I changed everything to integers and it all worked fine :S
I didn't test much on it tho, so it might just be some other thing I was doing wrong.

Robert The Robot
19
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 12th Jan 2011 21:41 Edited at: 12th Jan 2011 21:44
I never realised that about Booleans - although I suppose it makes sense in order to stop clogging up memory with various bits here and there.

I'm not actually doing calculations with boolean fields, just checking states (like the parameters in Set Object) to see if they are on or off - I'll probably end up just dumping all my boolean fields into a single byte and just check the state of each bit to read off the parameters.

@IanM
Why do you make the conversion of "Byte" boolean to true boolean - surely it just slows thefunction down slightly as (as Diggsey says) most languages use a full byte anyway?

@kaedroho
Just curious, if the data types get converted to integer for any calculations, what's the advantage of having these multiple data types?

You've given me a lot to think about here!



[Edit] Just seen Neuro Fuzzy's post - I used bitwise operators a lot in DBC, but I haven't had a chance to try them out in DBPro. I'll let you know how I go on!

We spend our lives chasing dreams. Dark Basic lets us catch some of them.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 12th Jan 2011 23:54
Yes, most languages use at least a byte to store a boolean (some use a full integer), but they also carry out the check and correction to a valid value whenever an assignment to the variable is made, yet you don't see 'don't use booleans' posted in performance-related pages.

Basically, I don't think you'll even notice the difference in speed. It's a few extra instructions out of the many billions you run every second. It literally takes longer for the data to be loaded from memory when you read the value than it does to convert it to 0/1.

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 13th Jan 2011 21:29 Edited at: 13th Jan 2011 21:29
Plus VC++ has an optimising compiler, so if you write this:


And then don't use bytevar anymore, it will probably use the same memory location for both memory locations. The only addition would be the check ensuring it is true or false (which in this case may be desirable).

Finally, DBPro has on average 4 extra instructions per line for error handling, and about twice as many instructions as necessary for calculations, so I wouldn't worry about an extra few instructions! (I don't blame TGC: an optimising compiler is a seriously difficult piece of software to perfect! Even MSVC++'s optimising compiler still has a few bugs in)

[b]

Login to post a reply

Server time is: 2026-07-21 15:13:55
Your offset time is: 2026-07-21 15:13:55