fast one: bools and types?

New to FreeBASIC? Post your questions here.
Stinktier
Posts: 26
Joined: Sep 04, 2008 10:53
Location: Sweden; Gothenburg

fast one: bools and types?

Postby Stinktier » Sep 16, 2008 8:47

can i define a variable in a type as a bool?

i want to have it like:


Code: Select all

type game
isRunning as bool
isPaused as bool
end type


do i have to use integers for this istead?
dani.user
Posts: 284
Joined: Sep 30, 2006 10:41

Postby dani.user » Sep 16, 2008 9:29

Better byte than integer, uses less memory.
The dev's are working on a branch that will contain boolean.
Stinktier
Posts: 26
Joined: Sep 04, 2008 10:53
Location: Sweden; Gothenburg

Postby Stinktier » Sep 16, 2008 9:47

thanks.
RayBritton
Posts: 306
Joined: Jun 02, 2005 7:11
Contact:

Postby RayBritton » Sep 16, 2008 14:03

I've heard that on a 32-bit CPU all types are converted to integer before they can be used, if this is true it might be better (in terms of speed) to use integers, especially since most people have at least a 1gb of ram.
Antoni
Posts: 1393
Joined: May 27, 2005 15:40
Location: Barcelona, Spain

Postby Antoni » Sep 16, 2008 14:30

The default alignment for variables in UDT's is 4 bytes, so the UDT will take the same space using integers, shorts or bytes for booleans
jofers
Posts: 1525
Joined: May 27, 2005 17:18
Contact:

Postby jofers » Sep 16, 2008 15:05

In general, memory alignment on processors is 4 bytes. x86, to be compatible with older chips, emulates non-word alignment with micro-ops, but it has to load 2 integers and mask/shift them into one. So if you DIM 2 bytes in a row, they take up 8 bytes in memory anyway.

However, having a strongly enforced BOOL type has its benefits though, and something like C++-style enumerations would be a nice feature that could implement that.
Stinktier
Posts: 26
Joined: Sep 04, 2008 10:53
Location: Sweden; Gothenburg

Postby Stinktier » Sep 16, 2008 18:10

jofers wrote:However, having a strongly enforced BOOL type has its benefits though, and something like C++-style enumerations would be a nice feature that could implement that.


You lost me at c++-style enumerations, i don't know a bit about that language.

So there's no way to access those unused bytes, then?
Mysoft
Posts: 778
Joined: Jul 28, 2005 13:56
Location: Brazil, Santa Catarina, Indaial (ouch!)
Contact:

Postby Mysoft » Sep 16, 2008 20:32

Code: Select all

type MYTYPE field=1
 ...
end type


anyway he's talking about the processor...
if one integer/byte or short are not aligned... the processor needs to get 2 integers and join them...

so if you use aligned variables that doesnt happen...
for example

Code: Select all

type AlignedBytes field=1
  as bytes BA,BB,BC,BD
  as short SA,SB
  as integer IA
end type


but even with that, transformation seems necesary for calculations... except for boolean ones :) (it can become a extensive subject)
dani.user
Posts: 284
Joined: Sep 30, 2006 10:41

Postby dani.user » Sep 16, 2008 20:35

I think the least memory would be used if you would have 32 settings, use an interger and set each bit of that integer to 0 or 1 coresponding to the 32 settings you need to store :D
dkl
Site Admin
Posts: 3212
Joined: Jul 28, 2005 14:45
Location: Germany

Postby dkl » Sep 16, 2008 20:40

If you have a lot of boolean values for a type you could also try bit flags.

You could use an integer to store the options, where each bit of the integer represents an option. A bit is set with the OR operator and can be tested with the AND operator, like this:

Code: Select all

enum
   OPTION_1 = &b001       '' first bit
   OPTION_2 = &b010       '' 2nd bit
   OPTION_3 = &b100       '' 3rd bit
end enum

'' If the integer is 0, all bits are 0, i.e. no options set/no flags true.
dim as integer flags = 0

'' Set option.
flags or= OPTION_1

'' Check is a flag is TRUE:
if ( flags and OPTION_2 ) then
   print "flag 2 is enabled"
else
   print "flag 2 is off"
end if
jofers
Posts: 1525
Joined: May 27, 2005 17:18
Contact:

Postby jofers » Sep 16, 2008 22:06

Oh, shoot, I completely forgot you were talking about using this in types. Yes, yes you can have boolean type members very easily:

Code: Select all

Type game
    isRunning:1 As Integer
    isPaused:1 As Integer
End Type


The type still aligns to 4 bytes, but the code is much more readable than masking bitflags.

Oh, and the central difference between C++ enums and FreeBASIC enums is that you can assign any integer value to a FreeBASIC enum. C++ style, this would be illegal:

Code: Select all

Enum MyEnumType
...
End Enum

Dim x As MyEnumType
x = 5
marcov
Posts: 3004
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Postby marcov » Sep 17, 2008 8:30

RayBritton wrote:I've heard that on a 32-bit CPU all types are converted to integer before they can be used, if this is true it might be better (in terms of speed) to use integers, especially since most people have at least a 1gb of ram.


Yes, but the conversions are free. And when moving/copying data, size does matters. Even IF you were right that factor would probably win out.

One doesn't have to artificially compress each record to the smallest size, but inflating it for misplaced "performance" reasons is a bit over the top.

Personally, I'd go for an (set of) enum btw.
Stinktier
Posts: 26
Joined: Sep 04, 2008 10:53
Location: Sweden; Gothenburg

Postby Stinktier » Sep 17, 2008 9:08

Well, i don't have a ton of switches i want to be booleans, so wasting memory isn't really a problem, but it would certainly make the code more easy to read using them.

---

Thanks everybody. And keep on posting if anything else in this subject comes to your mind.
R0b0t1
Posts: 3
Joined: Sep 22, 2008 0:01

Postby R0b0t1 » Sep 22, 2008 0:05

The C++ vector template operates differently when used with the class BOOL. It uses what has been discussed earlier about flags. (IIRC)

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest