[Include] Bit Functions
#1

Include contains functions for manipulating bits / cells.

PHP Code:
/****************************************************************************************************
 *                                                                                                  *
 *                                     )(   Bit Functions   )(                                      *
 *                                                                                                  *
 * Copyright © 2019 Abyss Morgan. All rights reserved.                                              *
 *                                                                                                  *
 * Website:  adm.ct8.pl                                                                             *
 * Download: adm.ct8.pl/r/download                                                                  *
 *                                                                                                  *
 * Plugins: None                                                                                    *
 * Modules: None                                                                                    *
 *                                                                                                  *
 * File Version: 1.8.0                                                                              *
 *                                                                                                  *
 * bit 0 - 31:                                                                                      *
 * (bit 31) --> 11111111000000001111010001010000 <-- (bit 0)                                        *
 *                                                                                                  *
 * Example cell mode:                                                                               *
 * Mode 2 (cell id 15) --> 11 11 11 11 00 00 00 00 11 11 01 00 01 01 00 00 <-- (cell id 0)          *
 * Mode 4 (cell id 7) --> 1111 1111 0000 0000 1111 0100 0101 0000 <-- (cell id 0)                   *
 * Mode 8 (cell id 3) --> 11111111 00000000 11110100 01010000 <-- (cell id 0)                       *
 * Mode 16 (cell id 1) --> 1111111100000000 1111010001010000 <-- (cell id 0)                        *
 *                                                                                                  *
 * General Macros:                                                                                  *
 * GetValueBit(value,bit);                                                                          *
 * SetValueBit(&value,bit,power);  //power 0 or 1                                                   *
 * SetValueBitTrue(&value,bit);                                                                     *
 * SetValueBitFalse(&value,bit);                                                                    *
 * GetCellValue(value,cellid,mode);                                                                 *
 * SetCellValue(&value,cellid,mode,power);                                                          *
 * GetCellValueEx(value,offset,cellsize);                                                           *
 * SetCellValueEx(&value,offset,cellsize,power);                                                    *
 * InvertValue(value);                                                                              *
 * InvertValueEx(value,key); //default key 0xFFFFFFFF                                               *
 * bool:CheckValue(value,&count=0); //even - false,uneven - true                                    *
 *                                                                                                  *
 * File Byte Macros:                                                                                *
 * ExtractValue(value,&byte1,&byte2,&byte3,&byte4);                                                 *
 * ExtractFloat(Float:value,&byte1,&byte2,&byte3,&byte4);                                           *
 * MergeValue(&value,byte1,byte2,byte3,byte4);                                                      *
 * MergeFloat(&Float:value,byte1,byte2,byte3,byte4);                                                *
 * MergeValueEx(byte1,byte2,byte3,byte4);                                                           *
 * Float:MergeFloatEx(byte1,byte2,byte3,byte4);                                                     *
 *                                                                                                  *
 * Dynamic Toggle Config Macros:                                                                    *
 * GetConfigAddress(itemid);                                                                        *
 * GetConfigBit(itemid);                                                                            *
 * GetConfigSize(max_items);                                                                        *
 * IsToggleConfigInformation(variable,itemid);                                                      *
 * ToggleConfigInformation(variable,itemid,value); //values: 1/0                                    *
 *                                                                                                  *
 ****************************************************************************************************/ 
Download:
BitFunctions.inc

Example:
PHP Code:
new ExampleConfig[GetConfigSize(MAX_PLAYERS)];
//get:
IsToggleConfigInformation(ExampleConfig,playerid);
//set:
ToggleConfigInformation(ExampleConfig,playerid,1); 
Value Extract/Merge:
PHP Code:
new val 0x89ABCDEF;
new 
abcd;
ExtractValue(val,a,b,c,d);
printf("%x %x %x %x",a,b,c,d); //prints 89 AB CD EF
new tmp 0;
MergeValue(tmp,a,b,c,d);
printf("%x",tmp); //prints 89ABCDEF 
Reply
#2

Snippets, you could make better stuff such as bitwise bitshift and so on, convert RGB to RGBA or ARGB (would be more appreciated), btw the XOR operator "^" works fine in pawn, what's wrong on use it instead of a macro? lol
Reply
#3

[deleted]
Reply
#4

updated

GetValueBit, InvertValue, InvertValueEx - use macros

SetValueBit - less operations to perform


added new macros
PHP Code:
ExtractValue(value, &byte1, &byte2, &byte3, &byte4);
ExtractFloat(Float:value, &byte1, &byte2, &byte3, &byte4);
MergeValue(&valuebyte1byte2byte3byte4);
MergeFloat(&Float:valuebyte1byte2byte3byte4); 
Reply
#5

updated (faster)
PHP Code:
SetValueBit(&valuebitpower); //power 0 or 1 
Added new macros:
PHP Code:
SetValueBitTrue(&valuebit);
SetValueBitFalse(&valuebit);

SetCellValue(&valuecellidmodepower);
GetCellValue(valuecellidmode);

MergeValueEx(byte1byte2byte3byte4);
Float:MergeFloatEx(byte1byte2byte3byte4); 
Function SetCellValue and GetCellValue

They are used to manipulate the n bit cells


Example:
PHP Code:
printf("%d, %d",GetCellValue(0x89ABCDEF,1,3),GetCellValue(0x89ABCDEF,1,8));
//print: 5, 205

//mode 3: 10 001 001 101 010 111 100 110 111 101 111 <-- cell id 0
//cell id 1 = 0b101 = 5

//mode 8: 10001001 10101011 11001101 11101111 <-- cell id 0
//cell id 1 = 0b11001101 = 205 
Reply
#6

Good stuff. But I guess it's not so useful
Reply
#7

Update v1.8.0:


- Added macros:
PHP Code:
GetConfigAddress(itemid);
GetConfigBit(itemid);
GetConfigSize(max_items);
IsToggleConfigInformation(variable,itemid);
ToggleConfigInformation(variable,itemid,value); 
- Example code:
PHP Code:
//vehicle
new ark[GetConfigSize(MAX_VEHICLES)];
for(new 
0MAX_VEHICLESi++){
    
ToggleConfigInformation(ark,i,1);
}

//player
new ExampleConfig[GetConfigSize(MAX_PLAYERS)];

//get:
IsToggleConfigInformation(ExampleConfig,playerid);

//set:
ToggleConfigInformation(ExampleConfig,playerid,1); 
Reply
#8

Could I get an example of something like

player can have 4 types of documents, i could have it done by having arrays like hasDocumentA hasDocumentB, but how do I do it using bits?
Reply
#9

e.g. one variable
PHP Code:
new PlayerDocument[4][GetConfigSize(MAX_PLAYERS)]; //4 * 32 * 4 B = 512 Bytes
//new bool:PlayerDocument[4][MAX_PLAYERS]; //4 * 1000 * 4 B = 16000 Bytes 
PHP Code:
//get: documentid 0 - 3
if(IsToggleConfigInformation(PlayerDocument[documentid],playerid)){

}

//set: 
ToggleConfigInformation(PlayerDocument[documentid],playerid,1); //1 or 0 
Reply
#10

thanks man, appreciated !
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)