diff --git a/addons/bitwise/$PBOPREFIX$ b/addons/bitwise/$PBOPREFIX$ new file mode 100644 index 000000000..013cc7cb6 --- /dev/null +++ b/addons/bitwise/$PBOPREFIX$ @@ -0,0 +1 @@ +x\cba\addons\bitwise \ No newline at end of file diff --git a/addons/bitwise/CfgFunctions.hpp b/addons/bitwise/CfgFunctions.hpp new file mode 100644 index 000000000..e1a39e56a --- /dev/null +++ b/addons/bitwise/CfgFunctions.hpp @@ -0,0 +1,20 @@ +class CfgFunctions { + class CBA { + class Bitwise { + PATHTO_FNC(bitflagsCheck); + PATHTO_FNC(bitflagsCheckBool); + PATHTO_FNC(bitflagsFlip); + PATHTO_FNC(bitflagsSet); + PATHTO_FNC(bitflagsUnset); + PATHTO_FNC(bitwiseAND); + PATHTO_FNC(bitwiseLROT); + PATHTO_FNC(bitwiseLSHFT); + PATHTO_FNC(bitwiseNOT); + PATHTO_FNC(bitwiseOR); + PATHTO_FNC(bitwiseRROT); + PATHTO_FNC(bitwiseRSHFT); + PATHTO_FNC(bitwiseXOR); + PATHTO_FNC(logBase2); + }; + }; +}; \ No newline at end of file diff --git a/addons/bitwise/config.cpp b/addons/bitwise/config.cpp new file mode 100644 index 000000000..a00236453 --- /dev/null +++ b/addons/bitwise/config.cpp @@ -0,0 +1,17 @@ +#include "script_component.hpp" + +class CfgPatches { + class ADDON { + name = CSTRING(component); + units[] = {}; + weapons[] = {}; + requiredVersion = REQUIRED_VERSION; + requiredAddons[] = {"cba_common"}; + author = "$STR_CBA_Author"; + authors[] = {"Daisy"}; + url = "$STR_CBA_URL"; + VERSION_CONFIG; + }; +}; + +#include "CfgFunctions.hpp" \ No newline at end of file diff --git a/addons/bitwise/fnc_bitflagsCheck.sqf b/addons/bitwise/fnc_bitflagsCheck.sqf new file mode 100644 index 000000000..a639085a1 --- /dev/null +++ b/addons/bitwise/fnc_bitflagsCheck.sqf @@ -0,0 +1,41 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitflagsCheck + +Description: + Checks if a given flagset has the specified flags set. + + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _flagset - flagset to check for flags within + _flags - flags to check for + +Returns: + The sum of not-set bits on success, -1 on error. + +Examples: + (begin example) + [47,22] call CBA_fnc_bitflagsCheck // returns 16 + // 47's set bits = 101111 (32,8,4,2,1) + // 22's set bits = 010110 (16,4,2) + // common set bits = 000110 (4,2) + // 22's unique bits = 010000 (16) + // sum of 22's unique bits = 16 + + [47,6] call CBA_fnc_bitflagsCheck // returns 0 + // 47's set bits = 101111 (32,8,4,2,1) + // 6's set bits = 000110 (4,2) + // common set bits = 000110 (4,2) + // 6's unique bits = 000000 (0) + // sum of 6's unique bits = 0 + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) +#define BITQUANT(num) floor BASE2LOG(num) + 1 + +params ["_flagset","_flags"]; +_flagset = [_flagset,BITQUANT(_flagset max _flags)] call CBA_fnc_bitwiseNOT; +[_flagset,_flags] call CBA_fnc_bitwiseAND; \ No newline at end of file diff --git a/addons/bitwise/fnc_bitflagsCheckBool.sqf b/addons/bitwise/fnc_bitflagsCheckBool.sqf new file mode 100644 index 000000000..b30effdcf --- /dev/null +++ b/addons/bitwise/fnc_bitflagsCheckBool.sqf @@ -0,0 +1,30 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitflagsCheckBool + +Description: + Checks if a given flagset has the specified flags set. + + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _flagset - flagset to check for flags within + _flags - flags to check for + +Returns: + True on success, false otherwise. + +Examples: + (begin example) + [70,47] call CBA_fnc_bitflagsCheckBool // returns false: + // 70's set bits = 1000110 (64,4,2) + // 47's set bits = 0101111 (32,8,4,2,1) + // common set bits = 0000110 (4,2) + // 47's unique bits = 0101001 (32,8,1) + // sum of 47's unique bits = 41 + // 41 != 0 so return false + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +(_this call CBA_fnc_bitflagsCheck) isEqualTo 0 \ No newline at end of file diff --git a/addons/bitwise/fnc_bitflagsFlip.sqf b/addons/bitwise/fnc_bitflagsFlip.sqf new file mode 100644 index 000000000..8985287cd --- /dev/null +++ b/addons/bitwise/fnc_bitflagsFlip.sqf @@ -0,0 +1,29 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitflagsFlip + +Description: + Flips the flags' specified bits in the flagset. + + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _flagset - flagset to flip flags within + _flags - flags to flip + +Returns: + Sum of set bits on success, -1 otherwise. + +Examples: + (begin example) + [62,23] call CBA_fnc_bitflagsFlip; // returns 41 + // 62's set bits = 111110 (32,16,8,4,2) + // 23's set bits = 010111 (16,4,2,1) + // common set bits = 010110 (16,4,2,1) + // unique set bits = 101001 (32,8,1) + // sum of unique bits = 41 + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +_this call CBA_fnc_bitwiseXOR; \ No newline at end of file diff --git a/addons/bitwise/fnc_bitflagsSet.sqf b/addons/bitwise/fnc_bitflagsSet.sqf new file mode 100644 index 000000000..ec320b214 --- /dev/null +++ b/addons/bitwise/fnc_bitflagsSet.sqf @@ -0,0 +1,28 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitflagsSet + +Description: + Sets the flags' specified bits in the flagset. Has no effect if the bits are already set. + + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _flagset - flagset to set flags within + _flags - flags to set + +Returns: + Sum of set bits on success, false otherwise. + +Examples: + (begin example) + [55,12] call CBA_fnc_bitflagsSet; // returns 63 + // 55's set bits = 110111 (32,16,4,2,1) + // 12's set bits = 001100 (8,4) + // all set bits = 111111 (32,16,8,4,2,1) + // sum of all set bits = 63 + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +_this call CBA_fnc_bitwiseOR; \ No newline at end of file diff --git a/addons/bitwise/fnc_bitflagsUnset.sqf b/addons/bitwise/fnc_bitflagsUnset.sqf new file mode 100644 index 000000000..4ff8deaa5 --- /dev/null +++ b/addons/bitwise/fnc_bitflagsUnset.sqf @@ -0,0 +1,32 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitflagsUnset + +Description: + Unsets the flags' specified bits in the flagset. Has no effect if the bits are already unset. + + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _flagset - flagset to set flags within + _flags - the flags to unset + +Returns: + Sum of set bits on success, -1 otherwise. + +Examples: + (begin example) + [55,14] call CBA_fnc_bitflagsUnset; // returns 49 + // 55's set bits = 110111 (32,16,4,2,1) + // 14's set bits = 001110 (8,4,2) + // common set bits = 000110 (4,2) + // 55's unique bits = 110001 (32,16,1) + // sum of unique bits = 49 + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +params [["_flagset",0,[0]],"_flags"]; +private _andReturn = [_flagset,_flags] call CBA_fnc_bitwiseAND; +if (_andReturn < 0) exitWith {-1}; +(floor abs _flagset) - _andReturn diff --git a/addons/bitwise/fnc_bitwiseAND.sqf b/addons/bitwise/fnc_bitwiseAND.sqf new file mode 100644 index 000000000..3db7cb513 --- /dev/null +++ b/addons/bitwise/fnc_bitwiseAND.sqf @@ -0,0 +1,50 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseAND + +Description: + Performs a bitwise AND operation between two numbers. + + * This function converts all inputs into positive integers. + * This function also sorts the input array. Despite the naming of _min and _max, the input array does not have to be presorted. + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _min - a number + _max - another number to compare to the first + +Returns: + Resulting number on success, -1 otherwise. + +Examples: + (begin example) + [55,25] call CBA_fnc_bitwiseAND; // returns 17 + // 55's set bits = 110111 (32,16,4,2,1) + // 25's set bits = 011001 (16,8,1) + // common set bits = 010001 (16,1) + // sum of common bits = 17 + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +#define BITGRAB(num,pow) ((floor (num / pow)) mod 2) +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) + +private _input = _this; +_input resize 2; +_input = _input apply {floor abs _x}; +_input sort true; +_input params [["_min",0,[0]],["_max",1,[1]]]; + +private _end = floor BASE2LOG(_min); //1/ln(2) = 1.44269502162933349609 +if (_end >= 24) exitWith {-1}; // precision drop after 2^24 (16777216) + +private _power = 0; +private _return = 0; + +for "_i" from 0 to _end do { + _power = 2^_i; + _return = _return + (_power * (BITGRAB(_max,_power) * BITGRAB(_min,_power))); +}; + +_return \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseLROT.sqf b/addons/bitwise/fnc_bitwiseLROT.sqf new file mode 100644 index 000000000..915b1beba --- /dev/null +++ b/addons/bitwise/fnc_bitwiseLROT.sqf @@ -0,0 +1,73 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseLROT + +Description: + Performs a bitwise LEFT-ROTATE-NO-CARRY operation on a given number. Bits that are both 1 are summed. + + * This function converts all inputs into positive integers. + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _num - the number to rotate + _numRot - the number of rotations to perform + _numBits - the number of bits this number should occupy (optional) + +Returns: + Rotated number on success, -1 otherwise. + +Examples: + (begin example) + 25 call CBA_fnc_bitwiseLROT; // returns 19 + // 25's set bits = 11001 (16,8,1) + // shift left by 1 = 11001_ + // move leftmost bit to back = 10011 (16,2,1) + // sum of rotated bits = 19 + + [25,2] call CBA_fnc_bitwiseLROT; // returns 7 + // rotates once to 10011 (19) + // rotates twice to 00111 (7) + + [25,2,6] call CBA_fnc_bitwiseLROT; // returns 37 + // 25's set bits 011001 + // rotates once to 110010 (50) + // rotates twice to 100101 (37) + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +#define BITGRAB(num,pow) ((floor (num / pow)) mod 2) +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) +#define BITQUANT(num) floor BASE2LOG(num) + 1 + +params [["_num",0,[0]],["_numRot",0,[0]],"_numBits"]; + +_num = floor abs _num; +_numRot = floor abs _numRot; + +private _bitCount = BITQUANT(_num); +if (!isNil "_numBits" && {_numBits isEqualType 0 && {_numBits > _bitCount}}) then {_bitCount = (floor abs _numBits)}; +if (_bitCount > 24) exitWith {-1}; +if (_numRot > _bitCount) then {_numRot = _numRot % _bitCount;}; // trim excess rotations + +private _power = 0; +private _amend = 0; + +if (_numRot > (_bitCount / 2)) exitWith { + _numRot = _numRot - (floor (_bitCount / 2)); + + for "_i" from 0 to _numRot-1 do { + _power = 2^_i; + _amend = _amend + (BITGRAB(_num,_power) * (_power * 2^_numBits)); // _pow * 2^_numBits == 2^(_i + _numBits) + }; + + floor ((_num + _amend) / 2^_numRot) // sum and bitshift right +}; + +_num = 2^_numRot * _num; // bitshift left by numRot +for "_i" from _bitCount to (_bitCount + _numRot) do { // check bits over limit + _power = 2^_i; + _amend = _amend + (BITGRAB(_num,_power) * (_power - 2^(_i - _bitCount))); +}; + +_num - _amend \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseLSHFT.sqf b/addons/bitwise/fnc_bitwiseLSHFT.sqf new file mode 100644 index 000000000..34e63ee80 --- /dev/null +++ b/addons/bitwise/fnc_bitwiseLSHFT.sqf @@ -0,0 +1,35 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseLSHFT + +Description: + Performs a bitwise LEFT-SHIFT operation on a given number. + + * This function converts all inputs into positive integers. + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will cause unexpected behavior. + +Parameters: + _num - the number to shift + _numShift - the number of shifts to perform + +Returns: + Shifted number + +Examples: + (begin example) + 25 call CBA_fnc_bitwiseLSHFT; // returns 50 + // 25's set bits = 11001 (16,8,1) + // shift bits left by 1, fill one's place with 0 = 110010 (32,16,2) + // sum of shifted bits = 50 + [25,3] call CBA_fnc_bitwiseLSHFT; // returns 200 + // 25's set bits = 11001 (16,8,1) + // shift bits left by 3, fill blank places with 0 = 11001000 (128,64,8) + // sum of shifted bits = 200 + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +params [["_num",1,[0]],["_numShift",0,[0]]]; +_num = floor _num; +_numShift = floor _numShift; +_num * 2^_numShift \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseNOT.sqf b/addons/bitwise/fnc_bitwiseNOT.sqf new file mode 100644 index 000000000..525e021ca --- /dev/null +++ b/addons/bitwise/fnc_bitwiseNOT.sqf @@ -0,0 +1,45 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseNOT + +Description: + Performs a bitwise NOT operation on a number. All bits are flipped. + + The _numBits argument is limited to numbers greater the number of bits _num already occupies. + + * This function converts all inputs into positive integers. + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _num - the number to invert + _numBits - the number of bits this number should occupy (optional) + +Returns: + Inverted number on success, -1 otherwise. + +Examples: + (begin example) + 12 call CBA_fnc_bitwiseNOT; // returns 1 + // 12's set bits = 110 (8,4) + // flip all bits = 001 (1) + // sum of bits = 1 + + [12,6] call CBA_fnc_bitwiseNOT; // returns 57 + // 12's set bits = 000110 (8,4) + // flip all bits = 111001 (32,16,8,1) + // sum of bits = 57 + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) + +params [["_num",1,[0]],"_numBits"]; + +_num = floor abs _num; + +private _bitCount = floor BASE2LOG(_num) + 1; +if (!isNil "_numBits" && {_numBits isEqualType 0 && {_numBits > _bitCount}}) then {_bitCount = floor abs _numBits}; +if (_bitCount > 24) exitWith {-1}; + +(2^_bitCount)-1-_num \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseOR.sqf b/addons/bitwise/fnc_bitwiseOR.sqf new file mode 100644 index 000000000..e4c72ec25 --- /dev/null +++ b/addons/bitwise/fnc_bitwiseOR.sqf @@ -0,0 +1,54 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseOR + +Description: + Performs a bitwise OR operation between two numbers. Bits that are both zero are not summed. + + * This function converts all inputs into positive integers. + * This function also sorts the input array. Despite the naming of _min and _max, the input array does not have to be presorted. + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _min - a number + _max - another number to compare to the first + +Returns: + Resulting number on success, -1 otherwise. + +Examples: + (begin example) + [55,15] call CBA_fnc_bitwiseOR; // returns 63 + // 55's set bits = 110111 (32,16,4,2,1) + // 15's set bits = 001111 (8,4,2,1) + // all set bits = 111111 (32,16,8,4,2,1) + // sum of all set bits = 63 + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +#define BITGRAB(num,pow) ((floor (num / pow)) mod 2) +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) + +private _input = _this; +_input resize 2; +_input = _input apply {floor abs _x}; +_input sort true; +_input params [["_min",0,[0]],["_max",1,[1]]]; + +private _end = floor BASE2LOG(_max); //1/ln(2) = 1.44269502162933349609 +if (_end > 24) exitWith {-1}; // precision drop after 2^24 (16777216) + +private _power = 0; +private _return = 0; +private _maxBit = 0; +private _minBit = 0; + +for "_i" from 0 to _end do { + _power = 2^_i; + _maxBit = BITGRAB(_max,_power); + _minBit = BITGRAB(_min,_power); + _return = _return + (_power * ((_maxBit + _minBit) - (_maxBit * _minBit))); +}; + +_return \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseRROT.sqf b/addons/bitwise/fnc_bitwiseRROT.sqf new file mode 100644 index 000000000..0ea68bfe0 --- /dev/null +++ b/addons/bitwise/fnc_bitwiseRROT.sqf @@ -0,0 +1,74 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseRROT + +Description: + Performs a bitwise RIGHT-ROTATE-NO-CARRY operation on a given number. + + * This function converts all inputs into positive integers. + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _num - the number to rotate + _numRot - the number of rotations to perform + _numBits - the number of bits this number should occupy (optional) + +Returns: + Rotated number on success, -1 otherwise. + +Examples: + (begin example) + 17 call CBA_fnc_bitwiseRROT; // returns 24 + // 17's set bits = 10001 (16,1) + // shift right by 1 = _1000|1 + // move rightmost bit to front = 11000 (16,8) + // sum of rotated bits = 24 + + [17,2] call CBA_fnc_bitwiseRROT; // returns 12 + // rotates once to 11000 (24) + // rotates again to 01100 (12) + + [17,2,7] call CBA_fnc_bitwiseRROT; // returns 36 + // 17's set bits 0010001 + // rotates once to 1001000 (72) + // rotates again to 0100100 (36) + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +#define BITGRAB(num,pow) ((floor (num / pow)) mod 2) +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) +#define BITQUANT(num) floor BASE2LOG(num) + 1 + +params [["_num",0,[0]],["_numRot",0,[0]],"_numBits"]; + +_num = floor abs _num; +_numRot = floor abs _numRot; + +private _bitCount = BITQUANT(_num); +if (!isNil "_numBits" && {_numBits isEqualType 0 && {_numBits > _bitCount}}) then {_bitCount = (floor abs _numBits)}; +if (_bitCount > 24) exitWith {-1}; +if (_numRot > _bitCount) then {_numRot = _numRot % _bitCount;}; // trim excess rotations + +private _power = 0; +private _amend = 0; + +if (_numRot > (_bitCount / 2)) exitWith { + + _numRot = _numRot - (floor (_bitCount / 2)); + _num = _num * 2^_numRot; + + for "_i" from _bitCount to (_bitCount + _numRot) do { // check bits over limit + _power = 2^_i; + _amend = _amend + (BITGRAB(_num,_power) * (_power - 2^(_i - _bitCount))); + }; + + _num - _amend +}; + +for "_i" from 0 to _numRot-1 do { + _power = 2^_i; + _amend = _amend + (BITGRAB(_num,_power) * (_power * 2^_numBits)); // _pow * 2^_numBits == 2^(_i + _numBits) +}; + +floor ((_num + _amend) / 2^_numRot) // sum and bitshift right \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseRSHFT.sqf b/addons/bitwise/fnc_bitwiseRSHFT.sqf new file mode 100644 index 000000000..a2cdd7eff --- /dev/null +++ b/addons/bitwise/fnc_bitwiseRSHFT.sqf @@ -0,0 +1,37 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseRSHFT + +Description: + Performs a bitwise logical RIGHT-SHIFT operation on a given number. + + * This function converts all inputs into positive integers. + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will cause unexpected behavior. + +Parameters: + _num - the number to shift + _numShift - the number of shifts to perform + +Returns: + Shifted number + +Examples: + (begin example) + 25 call CBA_fnc_bitwiseRSHFT; // returns 12 + // 25's set bits = 11001| (16,8,1) + // shift bits right by 1 = 1100| + // discard bits after 1's place = 1100| (8,4) + // sum of shifted bits = 12 + [25,3] call CBA_fnc_bitwiseRSHFT; // returns 3 + // 25's set bits = 11001| (16,8,1) + // shift bits right by 3 = 11|001 + // discard bits after 1's place = 11| (2,1) + // sum of shifted bits = 3 + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +params [["_num",1,[0]],["_numShift",0,[0]]]; +_num = floor _num; +_numShift = floor _numShift; +floor (_num / 2^_numShift) \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseXOR.sqf b/addons/bitwise/fnc_bitwiseXOR.sqf new file mode 100644 index 000000000..bbb8236c9 --- /dev/null +++ b/addons/bitwise/fnc_bitwiseXOR.sqf @@ -0,0 +1,51 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseXOR + +Description: + Performs a bitwise XOR operation between two numbers. Bits that are different than each other are summed. + + * This function converts all inputs into positive integers. + * This function also sorts the input array. Despite the naming of _min and _max, the input array does not have to be presorted. + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _min - a number + _max - another number to compare to the first + +Returns: + Resulting number on success, -1 otherwise. + +Examples: + (begin example) + [55,15] call CBA_fnc_bitwiseXOR; // returns 56 + // 55's set bits = 110111 (32,16,4,2,1) + // 15's set bits = 001111 (8,4,2,1) + // common set bits = 000111 (4,2,1) + // unique set bits = 111000 (32,16,8) + // sum of unique bits = 56 + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +#define BITGRAB(num,pow) ((floor (num / pow)) mod 2) +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) + +private _input = _this; +_input resize 2; +_input = _input apply {floor abs _x}; +_input sort true; +_input params [["_min",0,[0]],["_max",1,[1]]]; + +private _end = floor ((ln _max)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 +if (_end > 24) exitWith {-1}; // precision drop after 2^24 (16777216) + +private _power = 0; +private _return = 0; + +for "_i" from 0 to _end do { + _power = 2^_i; + _return = _return + (_power * ((BITGRAB(_max,_power) + BITGRAB(_min,_power)) % 2)); +}; + +_return \ No newline at end of file diff --git a/addons/bitwise/fnc_logBase2.sqf b/addons/bitwise/fnc_logBase2.sqf new file mode 100644 index 000000000..ba2c3a804 --- /dev/null +++ b/addons/bitwise/fnc_logBase2.sqf @@ -0,0 +1,27 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_logBase2 + +Description: + Returns the Base-2 (binary) logarithm of the specified number. + + * This function converts all inputs into positive numbers. + * This function returns a non-negative number (with the exception of negative infinity for an input of 0). + +Parameters: + _num - a number + +Returns: + Binary logarithm of _num + +Examples: + (begin example) + 8 call CBA_fnc_logBase2; // returns 3 + + -7.62 call CBA_fnc_logBase2; // returns 2.93 + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +private _num = _this param [0,1,[0]]; +(ln (abs _num))*1.44269502162933349609 \ No newline at end of file diff --git a/addons/bitwise/script_component.hpp b/addons/bitwise/script_component.hpp new file mode 100644 index 000000000..ce92e13b5 --- /dev/null +++ b/addons/bitwise/script_component.hpp @@ -0,0 +1,13 @@ +#define COMPONENT bitwise +#include "\x\cba\addons\main\script_mod.hpp" + + +#ifdef DEBUG_ENABLED_COMMON + #define DEBUG_MODE_FULL +#endif + +#ifdef DEBUG_SETTINGS_COMMON + #define DEBUG_SETTINGS DEBUG_SETTINGS_COMMON +#endif + +#include "\x\cba\addons\main\script_macros.hpp" \ No newline at end of file