Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
1 registered members (TipmyPip), 18,619 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
Super-small var-type? #308284
02/02/10 18:33
02/02/10 18:33
Joined: Oct 2007
Posts: 306
Austria
A
Alan Offline OP
Senior Member
Alan  Offline OP
Senior Member
A

Joined: Oct 2007
Posts: 306
Austria
Hi there,

I'm currently working on a new project where I have to use a binary tree which may become rather big. Therefore I need to keep the nodes themselves as small in size as possible. Quite frequently I have integer values which range only from 0 to 255, so they could be saved in 1 Byte. In original C, there is the "byte" type for that. However, it seems not to exist in lite-C according to the manual. Can I define a data-type which stores an integer value between 0 and 255 in 1 Byte of memory? If so, how do I do that?

EDIT: How about True/False-Values? Boolean values take only 1 bit to save. It would be a real waste to use even a "short" integer for that. Is there any more "economic way" to do this?

Thanks in advance,


Alan

Last edited by Alan; 02/02/10 18:36.
Re: Super-small var-type? [Re: Alan] #308285
02/02/10 18:35
02/02/10 18:35
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
char

Re: Super-small var-type? [Re: Lukas] #308286
02/02/10 18:37
02/02/10 18:37
Joined: Oct 2007
Posts: 306
Austria
A
Alan Offline OP
Senior Member
Alan  Offline OP
Senior Member
A

Joined: Oct 2007
Posts: 306
Austria
I thought that character values (char) save letters only?

Re: Super-small var-type? [Re: Alan] #308287
02/02/10 18:38
02/02/10 18:38
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
Letters are nothing else than a variable stored in 1 byte. wink

Re: Super-small var-type? [Re: Lukas] #308288
02/02/10 18:40
02/02/10 18:40
Joined: Oct 2007
Posts: 306
Austria
A
Alan Offline OP
Senior Member
Alan  Offline OP
Senior Member
A

Joined: Oct 2007
Posts: 306
Austria
So I won't get any trouble because of type conversion if I write something like:

char a_var;

a_var = 180;


In some languages this DOES cause some serious trouble.


EDIT: And how about boolean values? Any chance to use only 1 Bit for them?

Last edited by Alan; 02/02/10 18:43.
Re: Super-small var-type? [Re: Alan] #308289
02/02/10 18:43
02/02/10 18:43
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
I don't know about other languages but in Lite-C or C/C++ this will work fine. There is no difference between a_var = 97; or a_var = 'a'; wink

Re: Super-small var-type? [Re: Lukas] #308290
02/02/10 18:46
02/02/10 18:46
Joined: Oct 2007
Posts: 306
Austria
A
Alan Offline OP
Senior Member
Alan  Offline OP
Senior Member
A

Joined: Oct 2007
Posts: 306
Austria
Ok, great then, thanks so far! Last question left is the thing about booleans. I really have a LOT (~15) boolean values in my struct. Is it possible to use a smaller var-type then "char" for them?

Re: Super-small var-type? [Re: Alan] #308292
02/02/10 18:49
02/02/10 18:49
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
There is BOOL but AFAIK it actually needs more than 1 bit which makes it pretty useless if you want to save space. But you can use bits of a variable and use them as flags just like the engine does with its objects if you really need to save space:

#define FLAG1 (1<<0) // Replace FLAG1 etc. with meaningful names, these exact definitions are already in atypes.h
#define FLAG2 (1<<1)
#define FLAG3 (1<<2)
#define FLAG4 (1<<3)
#define FLAG5 (1<<4)
#define FLAG6 (1<<5)
#define FLAG7 (1<<6)
#define FLAG8 (1<<7)

...

long a_var;

...

a_var |= FLAG1; // set first bit/flag to 1
a_var &= ~FLAG1; // set first bit/flag to 0

Re: Super-small var-type? [Re: Lukas] #308293
02/02/10 18:51
02/02/10 18:51
Joined: Oct 2007
Posts: 306
Austria
A
Alan Offline OP
Senior Member
Alan  Offline OP
Senior Member
A

Joined: Oct 2007
Posts: 306
Austria
Wohooo, bit-shifting, huh? Never got to actually USE this, I've only heard about it in university... impressive! Sounds like a good solution!

Thanks a LOT! You really helped me out!

Re: Super-small var-type? [Re: Alan] #308296
02/02/10 18:54
02/02/10 18:54
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
you could use bitfields:

http://en.wikipedia.org/wiki/Bit_field

joey

Page 1 of 3 1 2 3

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1