Integer versus boolean
#1

0 and 1 are interpreted as false and true.
so you can basically use an integer as a boolean , since i want to optimize the memory usage i was asking myself what the size of a boolean and the size of an integer is.
I am not sure if it differs from language to language so i am asking here now, please don't answer if you don't know it for sure
Reply
#2

They both have the same size. If you want some optimization, take a look here: https://sampforum.blast.hk/showthread.php?tid=216730

- "Char-arrays"
- "Bit-flags in enums"

You can also use rBits or y_bit.
Reply
#3

so basically boolean is an integer that you can only put 1, 0 , false and true into , but internally it is an integer ? (32 bit value?)

And yeah i am into optiimization, but i still want my code to be clean and readable for everyone
Reply
#4

Yes, Boolean is still 32 bit regardless its tag.

I myself use packed arrays a lot when I know the value of the arrays will not exceed 0-255.
Reply
#5

You can also store 32 true/false values in a single variable if you really want to save memory. By using bit flags.

Код:
enum PLAYER_FLAGS :(<<= 1)
{
    LOGGED_IN = 1,
    IN_RACE
};

new PLAYER_FLAGS: PlayerFlags[MAX_PLAYERS];

//when player logs in...
PlayerFlags[playerid] |= LOGGED_IN;

//when player logs out
PlayerFlags[playerid] &= ~LOGGED_IN;

//check if player is logged in.
if( PlayerFlags[playerid] & LOGGED_IN )
EDIT: Sorry didn't realize that link had already been posted.
Reply
#6

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
They both have the same size. If you want some optimization, take a look here: https://sampforum.blast.hk/showthread.php?tid=216730

- "Char-arrays"
- "Bit-flags in enums"

You can also use rBits or y_bit.
PHP код:
    new bool:test[10 char];
    
test{0} = false;
    
test{1} = true;
    
printf("Char 0: %i",test{0});
    
printf("Char 1: %i",test{1}); 
so i have tried that, and this will save up space cause every true(1) and false(0) is stored as a single character?
Reply
#7

Packed arrays are 8 bit.
For 1 bit, use one of the above (rBits or y_bit).
Reply
#8

I will be using rbits, that seems pretty simple, thanks
Reply
#9

Quote:
Originally Posted by [Bios]Marcel
Посмотреть сообщение
I will be using rbits, that seems pretty simple, thanks
Bit Functions is faster:
https://sampforum.blast.hk/showthread.php?tid=591223
Reply
#10

prove it , but prove it so i can understand it ^^
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)