From e30936e7124930a023ed02d7678b63598e8a7b2d Mon Sep 17 00:00:00 2001 From: Daisy <92063434+Anderanged@users.noreply.github.com> Date: Wed, 11 Dec 2024 20:10:16 -0700 Subject: [PATCH 01/11] beginning commit --- addons/bitwise/fnc_bitflagsCheck.sqf | 28 +++++ addons/bitwise/fnc_bitflagsCheckBool.sqf | 28 +++++ addons/bitwise/fnc_bitflagsFlip.sqf | 29 +++++ addons/bitwise/fnc_bitflagsSet.sqf | 28 +++++ addons/bitwise/fnc_bitflagsUnset.sqf | 37 +++++++ addons/bitwise/fnc_bitwiseAND.sqf | 39 +++++++ addons/bitwise/fnc_bitwiseLROT.sqf | 57 ++++++++++ addons/bitwise/fnc_bitwiseLSHFT.sqf | 28 +++++ addons/bitwise/fnc_bitwiseNOT.sqf | 29 +++++ addons/bitwise/fnc_bitwiseOR.sqf | 40 +++++++ addons/bitwise/fnc_bitwiseOperators.sqf | 132 +++++++++++++++++++++++ addons/bitwise/fnc_bitwiseRROT.sqf | 51 +++++++++ addons/bitwise/fnc_bitwiseRSHFT.sqf | 26 +++++ addons/bitwise/fnc_bitwiseXOR.sqf | 37 +++++++ 14 files changed, 589 insertions(+) create mode 100644 addons/bitwise/fnc_bitflagsCheck.sqf create mode 100644 addons/bitwise/fnc_bitflagsCheckBool.sqf create mode 100644 addons/bitwise/fnc_bitflagsFlip.sqf create mode 100644 addons/bitwise/fnc_bitflagsSet.sqf create mode 100644 addons/bitwise/fnc_bitflagsUnset.sqf create mode 100644 addons/bitwise/fnc_bitwiseAND.sqf create mode 100644 addons/bitwise/fnc_bitwiseLROT.sqf create mode 100644 addons/bitwise/fnc_bitwiseLSHFT.sqf create mode 100644 addons/bitwise/fnc_bitwiseNOT.sqf create mode 100644 addons/bitwise/fnc_bitwiseOR.sqf create mode 100644 addons/bitwise/fnc_bitwiseOperators.sqf create mode 100644 addons/bitwise/fnc_bitwiseRROT.sqf create mode 100644 addons/bitwise/fnc_bitwiseRSHFT.sqf create mode 100644 addons/bitwise/fnc_bitwiseXOR.sqf diff --git a/addons/bitwise/fnc_bitflagsCheck.sqf b/addons/bitwise/fnc_bitflagsCheck.sqf new file mode 100644 index 000000000..8916006e1 --- /dev/null +++ b/addons/bitwise/fnc_bitflagsCheck.sqf @@ -0,0 +1,28 @@ +/* ---------------------------------------------------------------------------- +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 be clamped. + +Parameters: + _flagset - flagset to check for flags within + _flags - flags to check for + +Returns: + Sum of set bits as a decimal number on success, false otherwise. + +Examples: + (begin example) + [47,6] call CBA_fnc_bitflagsCheck // returns 6 + // 47's set bits = 32+8+4+2+1 + // 6's set bits = 4+2 + // set bits in common = 4, 2 + // sum of common set bits = 6 + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +_this 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..687799038 --- /dev/null +++ b/addons/bitwise/fnc_bitflagsCheckBool.sqf @@ -0,0 +1,28 @@ +/* ---------------------------------------------------------------------------- +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 if at least one flag bit is set within the flagset, false otherwise. + +Examples: + (begin example) + [47,6] call CBA_fnc_bitflagsCheckBool // returns true: + // 47's set bits = 32+8+4+2+1 + // 6's set bits = 4+2 + // set bits in common = 4, 2 + // sum of common set bits = 6 which is > 0 so = true + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +_this call CBA_fnc_bitwiseAND > 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..b2e5a2ca2 --- /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 for + +Returns: + Sum of set bits as a decimal number on success, false otherwise. + +Examples: + (begin example) + [63,23] call CBA_fnc_bitflagsFlip; // returns 40 + // 63's set bits = 32,16,8,4,2,1 + // 23's set bits = 16,4,2,1 + // bits in common = 16,4,2,1 + // bits not in common = 32,8 + // sum of unique bits = 40 + (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..6d4c04af3 --- /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,8] call CBA_fnc_bitflagsSet; // returns 63 + // 55's set bits = 32,16,4,2,1 + // 8's set bits = 8 + // all set bits = 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..d5efe304c --- /dev/null +++ b/addons/bitwise/fnc_bitflagsUnset.sqf @@ -0,0 +1,37 @@ +/* ---------------------------------------------------------------------------- +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 - a number representing currently set bitflags + _flags - the flags to unset + +Returns: + Sum of set bits as a decimal number on success, false otherwise. + +Examples: + (begin example) + [55,17] call CBA_fnc_bitflagsUnset; // returns 38 + // 55's set bits = 32,16,4,2,1 + // 17's set bits = 16,1 + // common bits = 16,1 + // 55's unique bits = 32,4,2 + // sum of all set bits = 38 + + // alternatively: + [32+16+4+2+1,16+1] call CBA_fnc_bitflagsUnset; + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +params ["_flagset","_flags"]; +[_flagset,_flags call CBA_fnc_bitwiseNOT] call CBA_fnc_bitwiseAND; + +// or + +_flagset - (_this call CBA_fnc_bitwiseAND) diff --git a/addons/bitwise/fnc_bitwiseAND.sqf b/addons/bitwise/fnc_bitwiseAND.sqf new file mode 100644 index 000000000..8e6d1169f --- /dev/null +++ b/addons/bitwise/fnc_bitwiseAND.sqf @@ -0,0 +1,39 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseAND + +Description: + Performs a bitwise AND operation between two decimal numbers. + + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _num1 - a number + _num2 - another number to compare to the first + +Returns: + Sum of set bits as a decimal number on success, false otherwise. + +Examples: + (begin example) + [55,17] call CBA_fnc_bitwiseAND; // returns 17 + // 55's set bits = 32,16,4,2,1 + // 17's set bits = 16,1 + // common bits = 16,1 + // sum of common bits = 17 + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +_this = _this apply {floor abs _x}; +_this sort true; +params [["_min",0,[0]],["_max",1,[0]]]; +private _end = floor ((ln _min)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 +if (_end >= 24) exitWith {false}; // 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 * (((floor (_max / _power)) % 2) * ((floor (_min / _power))) % 2)); +}; +_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..422afdecd --- /dev/null +++ b/addons/bitwise/fnc_bitwiseLROT.sqf @@ -0,0 +1,57 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseLROT + +Description: + Performs a bitwise LEFT-ROTATE-NO-CARRY operation on a given number. Bits that are both 1 are summed. + +Parameters: + _num - the number to rotate + _numRot - the number of rotations to perform + +Returns: + Rotated number on success, false otherwise. + +Examples: + (begin example) + 25 call CBA_fnc_bitwiseLROT; // returns 19 + // 25's set bits = 16+8+1 + // 25's set bits (binary) = 11001 + // shift left by 1, move leftmost bit to back = 10011 + // new bits = (2^4)+(2^1)+(2^0) OR 16+2+1 + // sum of rotated bits = 19 + + [25,2] call CBA_fnc_bitwiseLROT; // returns 7 + // rotates once to 10011 (19) + // rotates again to 00111 (7) + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +params [["_num",1,[0]],["_numRot",1,[0]]]; +_num = floor abs _num; +_numRot = floor abs _numRot; +private _exp = floor((ln _num)*1.44269502162933349609); +if (_numRot > _exp) then { + // modulo vs clamp??? + // how much of a performace hit would it take i wonder + _numRot = _numRot % _exp; + // _numRot = _exp; + }; +private _bit = 0; +for "_i" from 1 to _numRot do { + _bit = (floor(_num / 2^_exp)) % 2; + _num = (2*_num) - (_bit * 2^(_exp+1)) + _bit; +}; +_num +// bitwiseLSROT +params [["_num",1,[0]],["_numRot",1,[0]]]; +_num = floor abs _num; +_numRot = floor abs _numRot; +private _power = 2^(floor((ln _num)*1.44269502162933349609)); +private _bit = 0; +for "_i" from 1 to _numRot do { + _bit = (floor(_num / _power)) % 2; + _num = (2*_num) - (_bit * _power * 2) + _bit; +}; +_num \ 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..179c289c5 --- /dev/null +++ b/addons/bitwise/fnc_bitwiseLSHFT.sqf @@ -0,0 +1,28 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseLSHFT + +Description: + Performs a bitwise LEFT-SHIFT operation on a given number. + + * This function returns an integer. + * 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) + captive addaction ["rescue",CBA_fnc_actionargument_path,[[],{[_target] join (group _caller)},true]] //captive joins action callers group, action is removed (true) + (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..ba19e0de7 --- /dev/null +++ b/addons/bitwise/fnc_bitwiseNOT.sqf @@ -0,0 +1,29 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseNOT + +Description: + Performs a bitwise NOT operation on a number. All bits are flipped. + This function assumes that the largest power of 2 that _num stores is the number of bits it occupies. + +* This function returns a non-negative integer. + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _num - a decimal number + +Returns: + Sum of set bits as a decimal number on success, false otherwise. + +Examples: + (begin example) + captive addaction ["rescue",CBA_fnc_actionargument_path,[[],{[_target] join (group _caller)},true]] //captive joins action callers group, action is removed (true) + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +params [["_num",1,[0]]]; +_num = floor abs _num; +private _exp = floor((ln _num)*1.44269502162933349609); +if (_exp >= 24) exitWith {false}; +(2^(_exp+1))-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..59c218043 --- /dev/null +++ b/addons/bitwise/fnc_bitwiseOR.sqf @@ -0,0 +1,40 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseOR + +Description: + Performs a bitwise OR operation between two numbers. Bits that are both zero are not summed. + + * This function returns a non-negative integer. + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _num1 - a number + _num2 - another number to compare to the first + +Returns: + Sum of set bits as a decimal number on success, false otherwise. + +Examples: + (begin example) + captive addaction ["rescue",CBA_fnc_actionargument_path,[[],{[_target] join (group _caller)},true]] //captive joins action callers group, action is removed (true) + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +_this = _this apply {floor abs _x}; +_this sort true; +params [["_min",0,[0]],["_max",1,[0]]]; +private _end = floor ((ln _max)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 +if (_end >= 24) exitWith {false}; // 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 = (floor (_max / _power)) % 2; + _minBit = (floor (_min / _power)) % 2; + _return = _return + (_power * ((_maxBit + _minBit) - (_maxBit * _minBit))); +}; +_return \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseOperators.sqf b/addons/bitwise/fnc_bitwiseOperators.sqf new file mode 100644 index 000000000..95e931b0d --- /dev/null +++ b/addons/bitwise/fnc_bitwiseOperators.sqf @@ -0,0 +1,132 @@ +//bitwiseOR; ##################################################### +_this = _this apply {floor abs _x}; +_this sort true; +params [["_min",0,[0]],["_max",1,[0]]]; +private _end = floor ((ln _max)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 +if (_end >= 24) exitWith {false}; // 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 = (floor (_max / _power)) % 2; + _minBit = (floor (_min / _power)) % 2; + _return = _return + (_power * ((_maxBit + _minBit) - (_maxBit * _minBit))); +}; +_return +// bitwiseNOT #################################################### +params [["_num",1,[0]]]; +_num = floor abs _num; +(2^((floor((ln _num)*1.44269502162933349609))+1))-1-_num; +// bitwiseAND +_this = _this apply {floor abs _x}; +_this sort true; +params [["_min",0,[0]],["_max",1,[0]]]; +private _end = floor ((ln _min)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 +if (_end >= 24) exitWith {false}; // 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 * (((floor (_max / _power))%2)*((floor (_min / _power)))%2)); +}; +_return +//bitwiseXOR ##################################################### +_this sort true; +params [["_min",0,[0]],["_max",1,[0]]]; +_min = floor abs _min; +_max = floor abs _max; +private _end = floor ((ln _max)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 +if (_end >= 24) exitWith {false}; // 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 * ((((floor (_max / _power)) % 2) + ((floor (_min / _power)) % 2)) % 2)); +}; +_return +// bitwiseRSHIFT ################################################# +params [["_num",1,[0]],["_numShift",0,[0]]]; +_num = floor _num; +_numShift = floor _numShift; +floor (_num / 2^_numShift) +// bitwiseLSHIFT ################################################# +params [["_num",1,[0]],["_numShift",0,[0]]]; +_num = floor _num; +_numShift = floor _numShift; +_num * 2^_numShift + +// bitwiseLSROT ################################################## +params [["_num",1,[0]],["_numRot",1,[0]]]; +_num = floor abs _num; +_numRot = floor abs _numRot; +private _exp = floor((ln _num)*1.44269502162933349609); +private _bit = 0; +for "_i" from 1 to _numRot do { + _bit = (floor(_num / 2^_exp)) % 2; + _num = (2*_num) - (_bit * 2^(_exp+1)) + _bit; +}; +_num +// bitwiseLSROT +params [["_num",1,[0]],["_numRot",1,[0]]]; +_num = floor abs _num; +_numRot = floor abs _numRot; +private _power = 2^(floor((ln _num)*1.44269502162933349609)); +private _bit = 0; +for "_i" from 1 to _numRot do { + _bit = (floor(_num / _power)) % 2; + _num = (2*_num) - (_bit * _power * 2) + _bit; +}; +_num + +// bitwiseRSROT ################################################## +params [["_num",1,[0]],["_numRot",1,[0]]]; +_num = floor abs _num; +_numRot = floor abs _numRot; +private _exp = floor((ln _num)*1.44269502162933349609); +for "_i" from 1 to _numRot do { + _num = (floor (_num / 2)) + ((_num % 2) * 2^_exp); +}; +_num +// bitwiseRSROT +params [["_num",1,[0]],["_numRot",1,[0]]]; +_num = floor abs _num; +_numRot = floor abs _numRot; +private _power = 2^(floor((ln _num)*1.44269502162933349609)); +for "_i" from 1 to _numRot do { + _num = (floor (_num / 2)) + ((_num % 2) * _power); +}; +_num + +//bitflagsSet #################################################### +params [["_flagset",1,[0]],["_flags",0,[0]]]; +[_flagset,_flags] call CBA_fnc_bitwiseOR; + +//bitflagsUnset ################################################## +params [["_flagset",1,[0]],["_flags",0,[0]]]; +[_flagset,_flags call CBA_fnc_bitwiseNOT] call CBA_fnc_bitwiseAND; + +//bitflagsFlip ################################################### +params [["_flagset",1,[0]],["_flags",0,[0]]]; +[_flagset,_flags] call CBA_fnc_bitwiseXOR; + +//bitflagsCheck ################################################## +params [["_flagset",1,[0]],["_flags",0,[0]]]; +[_flagset,_flags] call CBA_fnc_bitwiseAND; + +//bitflagsCheckBool ############################################## +params [["_flagset",1,[0]],["_flags",0,[0]]]; +[_flagset,_flags] call CBA_fnc_bitwiseAND > 0 + +//bitflagsCheckToArray ########################################### +params [["_flagset",1,[0]],["_flags",0,[0]]]; +private _end = floor ((ln _flags)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 +if (_end >= 24) exitWith {false}; // precision drop after 2^24 (16777216) +private _power = 0; +private _return = []; +for "_i" from 0 to _end do { + _power = 2^_i; + _return pushBack (_power * (((floor (_flagset / _power))%2)*((floor (_flags / _power))%2))); +}; +_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..99ba407e9 --- /dev/null +++ b/addons/bitwise/fnc_bitwiseRROT.sqf @@ -0,0 +1,51 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseRROT + +Description: + Performs a bitwise RIGHT-ROTATE-NO-CARRY operation on a given number. + + * This function returns a non-negative integer. + * 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 rotate + _numRot - the number of rotations to perform + +Returns: + Rotated number on success, false otherwise. + +Examples: + (begin example) + 17 call CBA_fnc_bitwiseRROT; // returns 24 + // 17's set bits = 16+1 + // 17's set bits (binary) = 10001 + // shift right by 1, move rightmost bit to front = 11000 + // new bits = (2^4)+(2^3) OR 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) + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +params [["_num",1,[0]],["_numRot",1,[0]]]; +_num = floor abs _num; +_numRot = floor abs _numRot; +private _exp = floor((ln _num)*1.44269502162933349609); +if (_numRot > _exp) then {_numRot = _numRot % _exp;}; +for "_i" from 1 to _numRot do { + _num = (floor (_num / 2)) + ((_num % 2) * 2^_exp); +}; +_num +// bitwiseRSROT +params [["_num",1,[0]],["_numRot",1,[0]]]; +_num = floor abs _num; +_numRot = floor abs _numRot; +private _power = 2^(floor((ln _num)*1.44269502162933349609)); +for "_i" from 1 to _numRot do { + _num = (floor (_num / 2)) + ((_num % 2) * _power); +}; +_num \ 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..b615a970f --- /dev/null +++ b/addons/bitwise/fnc_bitwiseRSHFT.sqf @@ -0,0 +1,26 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseRSHFT + +Description: + Performs a bitwise logical RIGHT-SHIFT operation on a given number. + + * This function returns an integer. +Parameters: + _num - the number to shift + _numShift - the number of shifts to perform + +Returns: + Shifted number + +Examples: + (begin example) + captive addaction ["rescue",CBA_fnc_actionargument_path,[[],{[_target] join (group _caller)},true]] //captive joins action callers group, action is removed (true) + (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..51502e1d7 --- /dev/null +++ b/addons/bitwise/fnc_bitwiseXOR.sqf @@ -0,0 +1,37 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitwiseXOR + +Description: + Performs a bitwise XOR operation between two numbers. Bits that are different than each other are summed. + + * This function returns a non-negative integer. + * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. + +Parameters: + _num1 - a number + _num2 - another number to compare to the first + +Returns: + Sum of set bits as a decimal number on success, false otherwise. + +Examples: + (begin example) + captive addaction ["rescue",CBA_fnc_actionargument_path,[[],{[_target] join (group _caller)},true]] //captive joins action callers group, action is removed (true) + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +_this sort true; +params [["_min",0,[0]],["_max",1,[0]]]; +_min = floor abs _min; +_max = floor abs _max; +private _end = floor ((ln _max)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 +if (_end >= 24) exitWith {false}; // 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 * ((((floor (_max / _power)) % 2) + ((floor (_min / _power)) % 2)) % 2)); +}; +_return \ No newline at end of file From ba0deb405787544756576042a2cdbafd4a177f2c Mon Sep 17 00:00:00 2001 From: Daisy <92063434+Anderanged@users.noreply.github.com> Date: Thu, 12 Dec 2024 01:04:00 -0700 Subject: [PATCH 02/11] bye --- addons/bitwise/fnc_bitwiseOperators.sqf | 132 ------------------------ 1 file changed, 132 deletions(-) delete mode 100644 addons/bitwise/fnc_bitwiseOperators.sqf diff --git a/addons/bitwise/fnc_bitwiseOperators.sqf b/addons/bitwise/fnc_bitwiseOperators.sqf deleted file mode 100644 index 95e931b0d..000000000 --- a/addons/bitwise/fnc_bitwiseOperators.sqf +++ /dev/null @@ -1,132 +0,0 @@ -//bitwiseOR; ##################################################### -_this = _this apply {floor abs _x}; -_this sort true; -params [["_min",0,[0]],["_max",1,[0]]]; -private _end = floor ((ln _max)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 -if (_end >= 24) exitWith {false}; // 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 = (floor (_max / _power)) % 2; - _minBit = (floor (_min / _power)) % 2; - _return = _return + (_power * ((_maxBit + _minBit) - (_maxBit * _minBit))); -}; -_return -// bitwiseNOT #################################################### -params [["_num",1,[0]]]; -_num = floor abs _num; -(2^((floor((ln _num)*1.44269502162933349609))+1))-1-_num; -// bitwiseAND -_this = _this apply {floor abs _x}; -_this sort true; -params [["_min",0,[0]],["_max",1,[0]]]; -private _end = floor ((ln _min)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 -if (_end >= 24) exitWith {false}; // 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 * (((floor (_max / _power))%2)*((floor (_min / _power)))%2)); -}; -_return -//bitwiseXOR ##################################################### -_this sort true; -params [["_min",0,[0]],["_max",1,[0]]]; -_min = floor abs _min; -_max = floor abs _max; -private _end = floor ((ln _max)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 -if (_end >= 24) exitWith {false}; // 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 * ((((floor (_max / _power)) % 2) + ((floor (_min / _power)) % 2)) % 2)); -}; -_return -// bitwiseRSHIFT ################################################# -params [["_num",1,[0]],["_numShift",0,[0]]]; -_num = floor _num; -_numShift = floor _numShift; -floor (_num / 2^_numShift) -// bitwiseLSHIFT ################################################# -params [["_num",1,[0]],["_numShift",0,[0]]]; -_num = floor _num; -_numShift = floor _numShift; -_num * 2^_numShift - -// bitwiseLSROT ################################################## -params [["_num",1,[0]],["_numRot",1,[0]]]; -_num = floor abs _num; -_numRot = floor abs _numRot; -private _exp = floor((ln _num)*1.44269502162933349609); -private _bit = 0; -for "_i" from 1 to _numRot do { - _bit = (floor(_num / 2^_exp)) % 2; - _num = (2*_num) - (_bit * 2^(_exp+1)) + _bit; -}; -_num -// bitwiseLSROT -params [["_num",1,[0]],["_numRot",1,[0]]]; -_num = floor abs _num; -_numRot = floor abs _numRot; -private _power = 2^(floor((ln _num)*1.44269502162933349609)); -private _bit = 0; -for "_i" from 1 to _numRot do { - _bit = (floor(_num / _power)) % 2; - _num = (2*_num) - (_bit * _power * 2) + _bit; -}; -_num - -// bitwiseRSROT ################################################## -params [["_num",1,[0]],["_numRot",1,[0]]]; -_num = floor abs _num; -_numRot = floor abs _numRot; -private _exp = floor((ln _num)*1.44269502162933349609); -for "_i" from 1 to _numRot do { - _num = (floor (_num / 2)) + ((_num % 2) * 2^_exp); -}; -_num -// bitwiseRSROT -params [["_num",1,[0]],["_numRot",1,[0]]]; -_num = floor abs _num; -_numRot = floor abs _numRot; -private _power = 2^(floor((ln _num)*1.44269502162933349609)); -for "_i" from 1 to _numRot do { - _num = (floor (_num / 2)) + ((_num % 2) * _power); -}; -_num - -//bitflagsSet #################################################### -params [["_flagset",1,[0]],["_flags",0,[0]]]; -[_flagset,_flags] call CBA_fnc_bitwiseOR; - -//bitflagsUnset ################################################## -params [["_flagset",1,[0]],["_flags",0,[0]]]; -[_flagset,_flags call CBA_fnc_bitwiseNOT] call CBA_fnc_bitwiseAND; - -//bitflagsFlip ################################################### -params [["_flagset",1,[0]],["_flags",0,[0]]]; -[_flagset,_flags] call CBA_fnc_bitwiseXOR; - -//bitflagsCheck ################################################## -params [["_flagset",1,[0]],["_flags",0,[0]]]; -[_flagset,_flags] call CBA_fnc_bitwiseAND; - -//bitflagsCheckBool ############################################## -params [["_flagset",1,[0]],["_flags",0,[0]]]; -[_flagset,_flags] call CBA_fnc_bitwiseAND > 0 - -//bitflagsCheckToArray ########################################### -params [["_flagset",1,[0]],["_flags",0,[0]]]; -private _end = floor ((ln _flags)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 -if (_end >= 24) exitWith {false}; // precision drop after 2^24 (16777216) -private _power = 0; -private _return = []; -for "_i" from 0 to _end do { - _power = 2^_i; - _return pushBack (_power * (((floor (_flagset / _power))%2)*((floor (_flags / _power))%2))); -}; -_return \ No newline at end of file From dd44eb71a2cca63cdd2648ea4761fad277839905 Mon Sep 17 00:00:00 2001 From: Daisy <92063434+Anderanged@users.noreply.github.com> Date: Thu, 12 Dec 2024 01:33:47 -0700 Subject: [PATCH 03/11] structure and bugfixing --- addons/bitwise/$PBOPREFIX$ | 1 + addons/bitwise/CfgFunctions.hpp | 19 +++++++++++ addons/bitwise/config.cpp | 17 ++++++++++ addons/bitwise/fnc_bitflagsCheck.sqf | 21 ++++++------ addons/bitwise/fnc_bitflagsCheckAll.sqf | 40 +++++++++++++++++++++++ addons/bitwise/fnc_bitflagsCheckBool.sqf | 28 ---------------- addons/bitwise/fnc_bitflagsFlip.sqf | 16 ++++----- addons/bitwise/fnc_bitflagsSet.sqf | 10 +++--- addons/bitwise/fnc_bitflagsUnset.sqf | 29 +++++++---------- addons/bitwise/fnc_bitwiseAND.sqf | 16 +++++---- addons/bitwise/fnc_bitwiseLROT.sqf | 41 +++++++++--------------- addons/bitwise/fnc_bitwiseLSHFT.sqf | 5 ++- addons/bitwise/fnc_bitwiseNOT.sqf | 24 +++++++++----- addons/bitwise/fnc_bitwiseOR.sqf | 16 ++++++--- addons/bitwise/fnc_bitwiseRROT.sqf | 23 ++++--------- addons/bitwise/fnc_bitwiseRSHFT.sqf | 5 ++- addons/bitwise/fnc_bitwiseXOR.sqf | 13 ++++++-- addons/bitwise/fnc_logBase2.sqf | 26 +++++++++++++++ addons/bitwise/script_component.hpp | 13 ++++++++ 19 files changed, 228 insertions(+), 135 deletions(-) create mode 100644 addons/bitwise/$PBOPREFIX$ create mode 100644 addons/bitwise/CfgFunctions.hpp create mode 100644 addons/bitwise/config.cpp create mode 100644 addons/bitwise/fnc_bitflagsCheckAll.sqf delete mode 100644 addons/bitwise/fnc_bitflagsCheckBool.sqf create mode 100644 addons/bitwise/fnc_logBase2.sqf create mode 100644 addons/bitwise/script_component.hpp 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..3ea71d7a0 --- /dev/null +++ b/addons/bitwise/CfgFunctions.hpp @@ -0,0 +1,19 @@ +class CfgFunctions { + class PREFIX { + class COMPONENT { + PATHTO_FNC(bitflagsCheck); + PATHTO_FNC(bitflagsCheckAll); + 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); + }; + }; +}; \ 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 index 8916006e1..c0675eab6 100644 --- a/addons/bitwise/fnc_bitflagsCheck.sqf +++ b/addons/bitwise/fnc_bitflagsCheck.sqf @@ -2,27 +2,28 @@ Function: CBA_fnc_bitflagsCheck Description: - Checks if a given flagset has the specified flags set. + Checks if a given flagset has at least one of the specified flags set. - * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will be clamped. + * 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 + _flagset - flagset to check for flags within Returns: - Sum of set bits as a decimal number on success, false otherwise. + True on success, false otherwise. Examples: (begin example) - [47,6] call CBA_fnc_bitflagsCheck // returns 6 - // 47's set bits = 32+8+4+2+1 - // 6's set bits = 4+2 - // set bits in common = 4, 2 - // sum of common set bits = 6 + [70,47] call CBA_fnc_bitflagsCheck // returns true: + // 70's set bits = 1000110 (64,4,2) + // 47's set bits = 0101111 (32,8,4,2,1) + // common set bits = 0000110 (4,2) + // sum of common set bits = 6 + // 6 > 0 so return true (end) Author: Daisy ---------------------------------------------------------------------------- */ -_this call CBA_fnc_bitwiseAND; \ No newline at end of file +_this call CBA_fnc_bitwiseAND > 0 \ No newline at end of file diff --git a/addons/bitwise/fnc_bitflagsCheckAll.sqf b/addons/bitwise/fnc_bitflagsCheckAll.sqf new file mode 100644 index 000000000..d32c0e488 --- /dev/null +++ b/addons/bitwise/fnc_bitflagsCheckAll.sqf @@ -0,0 +1,40 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_bitflagsCheckAll + +Description: + Checks if a given flagset has all 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: + _flags - flags to check for + _flagset - flagset to check for flags within + +Returns: + True on success, sum of unset bits on failure, false otherwise. + +Examples: + (begin example) + [22,47] call CBA_fnc_bitflagsCheckAll // returns 16 + // 22's set bits = 010110 (16,4,2) + // 47's set bits = 101111 (32,8,4,2,1) + // common set bits = 000110 (4,2) + // 22's unique bits = 010000 (16) + // 16 != 0 so return 16 + + [6,47] call CBA_fnc_bitflagsCheckAll // returns true + // 6's set bits = 000110 (4,2) + // 47's set bits = 101111 (32,8,4,2,1) + // common set bits = 000110 (4,2) + // 6's unique bits = 000000 (0) + // 0 = 0 so return true + (end) + +Author: + Daisy +---------------------------------------------------------------------------- */ +_this sort true; +params ["_flags","_flagset"]; +private _res = [_flags,_flagset call CBA_fnc_bitwiseNOT] call CBA_fnc_bitwiseAND; +if (_res isEqualTo 0) exitWith {true}; +_res \ No newline at end of file diff --git a/addons/bitwise/fnc_bitflagsCheckBool.sqf b/addons/bitwise/fnc_bitflagsCheckBool.sqf deleted file mode 100644 index 687799038..000000000 --- a/addons/bitwise/fnc_bitflagsCheckBool.sqf +++ /dev/null @@ -1,28 +0,0 @@ -/* ---------------------------------------------------------------------------- -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 if at least one flag bit is set within the flagset, false otherwise. - -Examples: - (begin example) - [47,6] call CBA_fnc_bitflagsCheckBool // returns true: - // 47's set bits = 32+8+4+2+1 - // 6's set bits = 4+2 - // set bits in common = 4, 2 - // sum of common set bits = 6 which is > 0 so = true - (end) - -Author: - Daisy ----------------------------------------------------------------------------- */ -_this call CBA_fnc_bitwiseAND > 0 \ No newline at end of file diff --git a/addons/bitwise/fnc_bitflagsFlip.sqf b/addons/bitwise/fnc_bitflagsFlip.sqf index b2e5a2ca2..ed82880bc 100644 --- a/addons/bitwise/fnc_bitflagsFlip.sqf +++ b/addons/bitwise/fnc_bitflagsFlip.sqf @@ -7,20 +7,20 @@ Description: * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. Parameters: + _flags - flags to flip _flagset - flagset to flip flags within - _flags - flags to flip for Returns: - Sum of set bits as a decimal number on success, false otherwise. + Sum of set bits on success, false otherwise. Examples: (begin example) - [63,23] call CBA_fnc_bitflagsFlip; // returns 40 - // 63's set bits = 32,16,8,4,2,1 - // 23's set bits = 16,4,2,1 - // bits in common = 16,4,2,1 - // bits not in common = 32,8 - // sum of unique bits = 40 + [23,62] call CBA_fnc_bitflagsFlip; // returns 41 + // 23's set bits = 010111 (16,4,2,1) + // 62's set bits = 111110 (32,16,8,4,2) + // common set bits = 010110 (16,4,2,1) + // unique set bits = 101001 (32,8,1) + // sum of unique bits = 41 (end) Author: diff --git a/addons/bitwise/fnc_bitflagsSet.sqf b/addons/bitwise/fnc_bitflagsSet.sqf index 6d4c04af3..49425ac57 100644 --- a/addons/bitwise/fnc_bitflagsSet.sqf +++ b/addons/bitwise/fnc_bitflagsSet.sqf @@ -7,18 +7,18 @@ Description: * 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 + _flagset - flagset to set flags within Returns: Sum of set bits on success, false otherwise. Examples: (begin example) - [55,8] call CBA_fnc_bitflagsSet; // returns 63 - // 55's set bits = 32,16,4,2,1 - // 8's set bits = 8 - // all set bits = 32,16,8,4,2,1 + [12,55] call CBA_fnc_bitflagsSet; // returns 63 + // 12's set bits = 001100 (8,4) + // 55's set bits = 110111 (32,16,4,2,1) + // all set bits = 111111 (32,16,8,4,2,1) // sum of all set bits = 63 (end) diff --git a/addons/bitwise/fnc_bitflagsUnset.sqf b/addons/bitwise/fnc_bitflagsUnset.sqf index d5efe304c..eddc17e24 100644 --- a/addons/bitwise/fnc_bitflagsUnset.sqf +++ b/addons/bitwise/fnc_bitflagsUnset.sqf @@ -4,34 +4,29 @@ Function: CBA_fnc_bitflagsUnset Description: Unsets the flags' specified bits in the flagset. Has no effect if the bits are already unset. + * This function assumes the larger number is the flagset and the smaller number is the flags for calculation parity. * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. Parameters: - _flagset - a number representing currently set bitflags _flags - the flags to unset + _flagset - a number representing currently set bitflags Returns: - Sum of set bits as a decimal number on success, false otherwise. + Sum of set bits on success, false otherwise. Examples: (begin example) - [55,17] call CBA_fnc_bitflagsUnset; // returns 38 - // 55's set bits = 32,16,4,2,1 - // 17's set bits = 16,1 - // common bits = 16,1 - // 55's unique bits = 32,4,2 - // sum of all set bits = 38 - - // alternatively: - [32+16+4+2+1,16+1] call CBA_fnc_bitflagsUnset; + [14,55] call CBA_fnc_bitflagsUnset; // returns 49 + // 14's set bits = 001110 (8,4,2) + // 55's set bits = 110111 (32,16,4,2,1) + // common set bits = 000110 (4,2) + // 55's unique bits = 110001 (32,16,1) + // sum of unique bits = 49 (end) Author: Daisy ---------------------------------------------------------------------------- */ -params ["_flagset","_flags"]; -[_flagset,_flags call CBA_fnc_bitwiseNOT] call CBA_fnc_bitwiseAND; - -// or - -_flagset - (_this call CBA_fnc_bitwiseAND) +_this sort true; +params ["_flags","_flagset"]; +(floor abs _flagset) - ([_flags, _flagset] call CBA_fnc_bitwiseAND) diff --git a/addons/bitwise/fnc_bitwiseAND.sqf b/addons/bitwise/fnc_bitwiseAND.sqf index 8e6d1169f..701ef290b 100644 --- a/addons/bitwise/fnc_bitwiseAND.sqf +++ b/addons/bitwise/fnc_bitwiseAND.sqf @@ -11,29 +11,31 @@ Parameters: _num2 - another number to compare to the first Returns: - Sum of set bits as a decimal number on success, false otherwise. + Resulting number on success, false otherwise. Examples: (begin example) - [55,17] call CBA_fnc_bitwiseAND; // returns 17 - // 55's set bits = 32,16,4,2,1 - // 17's set bits = 16,1 - // common bits = 16,1 + [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 BITREP(num,pow) ((floor (num / pow)) mod 2) +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) _this = _this apply {floor abs _x}; _this sort true; params [["_min",0,[0]],["_max",1,[0]]]; -private _end = floor ((ln _min)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 +private _end = floor BASE2LOG(_min); //1/ln(2) = 1.44269502162933349609 if (_end >= 24) exitWith {false}; // 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 * (((floor (_max / _power)) % 2) * ((floor (_min / _power))) % 2)); + _return = _return + (_power * (BITREP(_max,_power) * BITREP(_min,_power))); }; _return \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseLROT.sqf b/addons/bitwise/fnc_bitwiseLROT.sqf index 422afdecd..ca57b7e47 100644 --- a/addons/bitwise/fnc_bitwiseLROT.sqf +++ b/addons/bitwise/fnc_bitwiseLROT.sqf @@ -4,6 +4,9 @@ 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 returns a non-negative integer. + * 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 rotate _numRot - the number of rotations to perform @@ -14,44 +17,30 @@ Returns: Examples: (begin example) 25 call CBA_fnc_bitwiseLROT; // returns 19 - // 25's set bits = 16+8+1 - // 25's set bits (binary) = 11001 - // shift left by 1, move leftmost bit to back = 10011 - // new bits = (2^4)+(2^1)+(2^0) OR 16+2+1 + // 25's set bits = 11001 (16+8+1) + // shift left by 1, 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 again to 00111 (7) + // rotates once to 10011 (19) + // rotates twice to 00111 (7) (end) Author: Daisy ---------------------------------------------------------------------------- */ +#define BITREP(num,pow) ((floor (num / pow)) mod 2) +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) params [["_num",1,[0]],["_numRot",1,[0]]]; _num = floor abs _num; _numRot = floor abs _numRot; -private _exp = floor((ln _num)*1.44269502162933349609); -if (_numRot > _exp) then { - // modulo vs clamp??? - // how much of a performace hit would it take i wonder - _numRot = _numRot % _exp; - // _numRot = _exp; - }; +private _exp = floor BASE2LOG(_num); +private _power = 2^_exp; +private _power1 = _power * 2; // 2^_exp+1 private _bit = 0; +if (_numRot > _exp) then {_numRot = _numRot % _exp}; for "_i" from 1 to _numRot do { - _bit = (floor(_num / 2^_exp)) % 2; - _num = (2*_num) - (_bit * 2^(_exp+1)) + _bit; -}; -_num -// bitwiseLSROT -params [["_num",1,[0]],["_numRot",1,[0]]]; -_num = floor abs _num; -_numRot = floor abs _numRot; -private _power = 2^(floor((ln _num)*1.44269502162933349609)); -private _bit = 0; -for "_i" from 1 to _numRot do { - _bit = (floor(_num / _power)) % 2; - _num = (2*_num) - (_bit * _power * 2) + _bit; + _bit = BITREP(_num,_power); + _num = (2*_num) - (_bit * _power1) + _bit; }; _num \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseLSHFT.sqf b/addons/bitwise/fnc_bitwiseLSHFT.sqf index 179c289c5..bd2cb9f09 100644 --- a/addons/bitwise/fnc_bitwiseLSHFT.sqf +++ b/addons/bitwise/fnc_bitwiseLSHFT.sqf @@ -16,7 +16,10 @@ Returns: Examples: (begin example) - captive addaction ["rescue",CBA_fnc_actionargument_path,[[],{[_target] join (group _caller)},true]] //captive joins action callers group, action is removed (true) + 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 (end) Author: diff --git a/addons/bitwise/fnc_bitwiseNOT.sqf b/addons/bitwise/fnc_bitwiseNOT.sqf index ba19e0de7..43f0b9c4f 100644 --- a/addons/bitwise/fnc_bitwiseNOT.sqf +++ b/addons/bitwise/fnc_bitwiseNOT.sqf @@ -3,27 +3,35 @@ Function: CBA_fnc_bitwiseNOT Description: Performs a bitwise NOT operation on a number. All bits are flipped. - This function assumes that the largest power of 2 that _num stores is the number of bits it occupies. + By default, this function assumes that the largest power of 2 that _num stores is the number of bits it occupies. -* This function returns a non-negative integer. + * This function returns a non-negative integer. * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. Parameters: - _num - a decimal number + _num - a number + _base - the number of bits this number occupies (optional) Returns: - Sum of set bits as a decimal number on success, false otherwise. + Resulting number on success, false otherwise. Examples: (begin example) - captive addaction ["rescue",CBA_fnc_actionargument_path,[[],{[_target] join (group _caller)},true]] //captive joins action callers group, action is removed (true) + 12 call CBA_fnc_bitwiseNOT; // returns 1 + // 12's set bits = 110 (8,4) + // flip all bits = 001 (1) + [12] call CBA_fnc_bitwiseNOT; // returns 1 + // 12's set bits = 110 (8,4) + // flip all bits = 001 (1) (end) Author: Daisy ---------------------------------------------------------------------------- */ -params [["_num",1,[0]]]; +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) +params [["_num",1,[0]],"_base"]; _num = floor abs _num; -private _exp = floor((ln _num)*1.44269502162933349609); +private _exp = floor BASE2LOG(_num); +if (!isNil "_base" && {_base isEqualType 0}) then {_exp = floor abs _base}; if (_exp >= 24) exitWith {false}; -(2^(_exp+1))-1-_num; \ No newline at end of file +(2^(_exp+1))-1-_num \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseOR.sqf b/addons/bitwise/fnc_bitwiseOR.sqf index 59c218043..0e0d556fd 100644 --- a/addons/bitwise/fnc_bitwiseOR.sqf +++ b/addons/bitwise/fnc_bitwiseOR.sqf @@ -12,20 +12,26 @@ Parameters: _num2 - another number to compare to the first Returns: - Sum of set bits as a decimal number on success, false otherwise. + Resulting number on success, false otherwise. Examples: (begin example) - captive addaction ["rescue",CBA_fnc_actionargument_path,[[],{[_target] join (group _caller)},true]] //captive joins action callers group, action is removed (true) + [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 BITREP(num,pow) ((floor (num / pow)) mod 2) +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) _this = _this apply {floor abs _x}; _this sort true; params [["_min",0,[0]],["_max",1,[0]]]; -private _end = floor ((ln _max)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 +private _end = BASE2LOG(_max); //1/ln(2) = 1.44269502162933349609 if (_end >= 24) exitWith {false}; // precision drop after 2^24 (16777216) private _power = 0; private _return = 0; @@ -33,8 +39,8 @@ private _maxBit = 0; private _minBit = 0; for "_i" from 0 to _end do { _power = 2^_i; - _maxBit = (floor (_max / _power)) % 2; - _minBit = (floor (_min / _power)) % 2; + _maxBit = BITREP(_max,_power); + _minBit = BITREP(_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 index 99ba407e9..2efab1f01 100644 --- a/addons/bitwise/fnc_bitwiseRROT.sqf +++ b/addons/bitwise/fnc_bitwiseRROT.sqf @@ -17,34 +17,25 @@ Returns: Examples: (begin example) 17 call CBA_fnc_bitwiseRROT; // returns 24 - // 17's set bits = 16+1 - // 17's set bits (binary) = 10001 - // shift right by 1, move rightmost bit to front = 11000 - // new bits = (2^4)+(2^3) OR 16+8 + // 17's set bits = 10001 (16+1) + // shift right by 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 once to 11000 (24) // rotates again to 01100 (12) (end) Author: Daisy ---------------------------------------------------------------------------- */ +#define BASE2LOG(number) ((ln number)*1.44269502162933349609) params [["_num",1,[0]],["_numRot",1,[0]]]; _num = floor abs _num; _numRot = floor abs _numRot; -private _exp = floor((ln _num)*1.44269502162933349609); -if (_numRot > _exp) then {_numRot = _numRot % _exp;}; -for "_i" from 1 to _numRot do { - _num = (floor (_num / 2)) + ((_num % 2) * 2^_exp); -}; -_num -// bitwiseRSROT -params [["_num",1,[0]],["_numRot",1,[0]]]; -_num = floor abs _num; -_numRot = floor abs _numRot; -private _power = 2^(floor((ln _num)*1.44269502162933349609)); +private _exp = floor BASE2LOG(_num); +private _power = 2^_exp; +if (_numRot > _exp) then {_numRot = _numRot % _exp}; for "_i" from 1 to _numRot do { _num = (floor (_num / 2)) + ((_num % 2) * _power); }; diff --git a/addons/bitwise/fnc_bitwiseRSHFT.sqf b/addons/bitwise/fnc_bitwiseRSHFT.sqf index b615a970f..097adb8b3 100644 --- a/addons/bitwise/fnc_bitwiseRSHFT.sqf +++ b/addons/bitwise/fnc_bitwiseRSHFT.sqf @@ -14,7 +14,10 @@ Returns: Examples: (begin example) - captive addaction ["rescue",CBA_fnc_actionargument_path,[[],{[_target] join (group _caller)},true]] //captive joins action callers group, action is removed (true) + 25 call CBA_fnc_bitwiseRSHFT; // returns 12 + // 25's set bits = 11001 (16+8+1) + // shift bits right by 1, discard bit in 1's place = 1100 (8+4) + // sum of shifted bits = 12 (end) Author: diff --git a/addons/bitwise/fnc_bitwiseXOR.sqf b/addons/bitwise/fnc_bitwiseXOR.sqf index 51502e1d7..86e0beaeb 100644 --- a/addons/bitwise/fnc_bitwiseXOR.sqf +++ b/addons/bitwise/fnc_bitwiseXOR.sqf @@ -12,16 +12,23 @@ Parameters: _num2 - another number to compare to the first Returns: - Sum of set bits as a decimal number on success, false otherwise. + Resulting number on success, false otherwise. Examples: (begin example) - captive addaction ["rescue",CBA_fnc_actionargument_path,[[],{[_target] join (group _caller)},true]] //captive joins action callers group, action is removed (true) + [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 BITREP(num,pow) ((floor (num / pow)) mod 2) +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) _this sort true; params [["_min",0,[0]],["_max",1,[0]]]; _min = floor abs _min; @@ -32,6 +39,6 @@ private _power = 0; private _return = 0; for "_i" from 0 to _end do { _power = 2^_i; - _return = _return + (_power * ((((floor (_max / _power)) % 2) + ((floor (_min / _power)) % 2)) % 2)); + _return = _return + (_power * ((BITREP(_max,_power) + BITREP(_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..7052d65f1 --- /dev/null +++ b/addons/bitwise/fnc_logBase2.sqf @@ -0,0 +1,26 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_logBase2 + +Description: + Returns the Base-2 (binary) logarithm of the specified number. + + * 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 From 0a7f6365c163a09b6ce08e083bcc90561cb0e424 Mon Sep 17 00:00:00 2001 From: Daisy <92063434+Anderanged@users.noreply.github.com> Date: Thu, 12 Dec 2024 01:34:55 -0700 Subject: [PATCH 04/11] capitalization --- addons/bitwise/CfgFunctions.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/bitwise/CfgFunctions.hpp b/addons/bitwise/CfgFunctions.hpp index 3ea71d7a0..125cb9063 100644 --- a/addons/bitwise/CfgFunctions.hpp +++ b/addons/bitwise/CfgFunctions.hpp @@ -1,6 +1,6 @@ class CfgFunctions { - class PREFIX { - class COMPONENT { + class CBA { + class Bitwise { PATHTO_FNC(bitflagsCheck); PATHTO_FNC(bitflagsCheckAll); PATHTO_FNC(bitflagsFlip); From 6256d04146d31b726f045f9ed0b157a0c627573f Mon Sep 17 00:00:00 2001 From: Daisy <92063434+Anderanged@users.noreply.github.com> Date: Thu, 12 Dec 2024 13:21:16 -0700 Subject: [PATCH 05/11] more bugfixing --- addons/bitwise/fnc_bitflagsCheck.sqf | 1 + addons/bitwise/fnc_bitflagsCheckAll.sqf | 9 +++++---- addons/bitwise/fnc_bitflagsFlip.sqf | 1 + addons/bitwise/fnc_bitflagsSet.sqf | 1 + addons/bitwise/fnc_bitflagsUnset.sqf | 14 +++++++------- addons/bitwise/fnc_bitwiseNOT.sqf | 2 +- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/addons/bitwise/fnc_bitflagsCheck.sqf b/addons/bitwise/fnc_bitflagsCheck.sqf index c0675eab6..e2069af85 100644 --- a/addons/bitwise/fnc_bitflagsCheck.sqf +++ b/addons/bitwise/fnc_bitflagsCheck.sqf @@ -4,6 +4,7 @@ Function: CBA_fnc_bitflagsCheck Description: Checks if a given flagset has at least one of the specified flags set. + * This function assumes the larger number is the flagset and the smaller number is the flags for calculation parity. * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. Parameters: diff --git a/addons/bitwise/fnc_bitflagsCheckAll.sqf b/addons/bitwise/fnc_bitflagsCheckAll.sqf index d32c0e488..ae854d082 100644 --- a/addons/bitwise/fnc_bitflagsCheckAll.sqf +++ b/addons/bitwise/fnc_bitflagsCheckAll.sqf @@ -11,7 +11,7 @@ Parameters: _flagset - flagset to check for flags within Returns: - True on success, sum of unset bits on failure, false otherwise. + True on success, sum of not-set bits on failure, false otherwise. Examples: (begin example) @@ -33,8 +33,9 @@ Examples: Author: Daisy ---------------------------------------------------------------------------- */ -_this sort true; -params ["_flags","_flagset"]; -private _res = [_flags,_flagset call CBA_fnc_bitwiseNOT] call CBA_fnc_bitwiseAND; +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) +params ["_flagset","_flags"]; +_flagset = [_flagset,floor BASE2LOG(_flagset max _flags)] call CBA_fnc_bitwiseNOT; +private _res = [_flags,_flagset] call CBA_fnc_bitwiseAND; if (_res isEqualTo 0) exitWith {true}; _res \ No newline at end of file diff --git a/addons/bitwise/fnc_bitflagsFlip.sqf b/addons/bitwise/fnc_bitflagsFlip.sqf index ed82880bc..56632eef8 100644 --- a/addons/bitwise/fnc_bitflagsFlip.sqf +++ b/addons/bitwise/fnc_bitflagsFlip.sqf @@ -4,6 +4,7 @@ Function: CBA_fnc_bitflagsFlip Description: Flips the flags' specified bits in the flagset. + * This function assumes the larger number is the flagset and the smaller number is the flags for calculation parity. * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. Parameters: diff --git a/addons/bitwise/fnc_bitflagsSet.sqf b/addons/bitwise/fnc_bitflagsSet.sqf index 49425ac57..4d693b4e9 100644 --- a/addons/bitwise/fnc_bitflagsSet.sqf +++ b/addons/bitwise/fnc_bitflagsSet.sqf @@ -4,6 +4,7 @@ Function: CBA_fnc_bitflagsSet Description: Sets the flags' specified bits in the flagset. Has no effect if the bits are already set. + * This function assumes the larger number is the flagset and the smaller number is the flags for calculation parity. * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. Parameters: diff --git a/addons/bitwise/fnc_bitflagsUnset.sqf b/addons/bitwise/fnc_bitflagsUnset.sqf index eddc17e24..0d0760ccb 100644 --- a/addons/bitwise/fnc_bitflagsUnset.sqf +++ b/addons/bitwise/fnc_bitflagsUnset.sqf @@ -4,21 +4,20 @@ Function: CBA_fnc_bitflagsUnset Description: Unsets the flags' specified bits in the flagset. Has no effect if the bits are already unset. - * This function assumes the larger number is the flagset and the smaller number is the flags for calculation parity. * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. Parameters: - _flags - the flags to unset _flagset - a number representing currently set bitflags + _flags - the flags to unset Returns: Sum of set bits on success, false otherwise. Examples: (begin example) - [14,55] call CBA_fnc_bitflagsUnset; // returns 49 - // 14's set bits = 001110 (8,4,2) + [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 @@ -27,6 +26,7 @@ Examples: Author: Daisy ---------------------------------------------------------------------------- */ -_this sort true; -params ["_flags","_flagset"]; -(floor abs _flagset) - ([_flags, _flagset] call CBA_fnc_bitwiseAND) +params ["_flagset","_flags"]; +private _andReturn = [_flagset,_flags] call CBA_fnc_bitwiseAND; +if (_andReturn isEqualType false) exitWith {false}; +(floor abs _flagset) - _andReturn diff --git a/addons/bitwise/fnc_bitwiseNOT.sqf b/addons/bitwise/fnc_bitwiseNOT.sqf index 43f0b9c4f..b90fce35b 100644 --- a/addons/bitwise/fnc_bitwiseNOT.sqf +++ b/addons/bitwise/fnc_bitwiseNOT.sqf @@ -32,6 +32,6 @@ Author: params [["_num",1,[0]],"_base"]; _num = floor abs _num; private _exp = floor BASE2LOG(_num); -if (!isNil "_base" && {_base isEqualType 0}) then {_exp = floor abs _base}; +if (!isNil "_base" && {_base isEqualType 0 && {_base > _exp}}) then {_exp = floor abs _base}; if (_exp >= 24) exitWith {false}; (2^(_exp+1))-1-_num \ No newline at end of file From 7747aaed74727535d732022755b809d353079a39 Mon Sep 17 00:00:00 2001 From: Daisy <92063434+Anderanged@users.noreply.github.com> Date: Thu, 12 Dec 2024 21:14:32 -0700 Subject: [PATCH 06/11] fixes, new func, restructuring and rephrasing of comments --- addons/bitwise/CfgFunctions.hpp | 1 + addons/bitwise/fnc_bitflagsCheck.sqf | 1 - addons/bitwise/fnc_bitflagsCheckAll.sqf | 16 +++++++--------- addons/bitwise/fnc_bitflagsFlip.sqf | 9 ++++----- addons/bitwise/fnc_bitflagsSet.sqf | 7 +++---- addons/bitwise/fnc_bitflagsUnset.sqf | 6 +++--- addons/bitwise/fnc_bitwiseAND.sqf | 7 ++++--- addons/bitwise/fnc_bitwiseLROT.sqf | 11 ++++++----- addons/bitwise/fnc_bitwiseLSHFT.sqf | 10 +++++++--- addons/bitwise/fnc_bitwiseNOT.sqf | 15 ++++++++------- addons/bitwise/fnc_bitwiseOR.sqf | 6 +++--- addons/bitwise/fnc_bitwiseRROT.sqf | 23 +++++++++++++++++++---- addons/bitwise/fnc_bitwiseRSHFT.sqf | 14 +++++++++++--- addons/bitwise/fnc_bitwiseXOR.sqf | 6 +++--- addons/bitwise/fnc_logBase2.sqf | 1 + 15 files changed, 80 insertions(+), 53 deletions(-) diff --git a/addons/bitwise/CfgFunctions.hpp b/addons/bitwise/CfgFunctions.hpp index 125cb9063..70ea19e2e 100644 --- a/addons/bitwise/CfgFunctions.hpp +++ b/addons/bitwise/CfgFunctions.hpp @@ -14,6 +14,7 @@ class CfgFunctions { PATHTO_FNC(bitwiseRROT); PATHTO_FNC(bitwiseRSHFT); PATHTO_FNC(bitwiseXOR); + PATHTO_FNC(logBase2); }; }; }; \ No newline at end of file diff --git a/addons/bitwise/fnc_bitflagsCheck.sqf b/addons/bitwise/fnc_bitflagsCheck.sqf index e2069af85..c0675eab6 100644 --- a/addons/bitwise/fnc_bitflagsCheck.sqf +++ b/addons/bitwise/fnc_bitflagsCheck.sqf @@ -4,7 +4,6 @@ Function: CBA_fnc_bitflagsCheck Description: Checks if a given flagset has at least one of the specified flags set. - * This function assumes the larger number is the flagset and the smaller number is the flags for calculation parity. * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. Parameters: diff --git a/addons/bitwise/fnc_bitflagsCheckAll.sqf b/addons/bitwise/fnc_bitflagsCheckAll.sqf index ae854d082..d4e8b6e0c 100644 --- a/addons/bitwise/fnc_bitflagsCheckAll.sqf +++ b/addons/bitwise/fnc_bitflagsCheckAll.sqf @@ -7,24 +7,24 @@ Description: * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. Parameters: - _flags - flags to check for _flagset - flagset to check for flags within + _flags - flags to check for Returns: - True on success, sum of not-set bits on failure, false otherwise. + 0 on success, sum of not-set bits on failure, -1 otherwise. Examples: (begin example) - [22,47] call CBA_fnc_bitflagsCheckAll // returns 16 - // 22's set bits = 010110 (16,4,2) + [47,22] call CBA_fnc_bitflagsCheckAll // 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) // 16 != 0 so return 16 - [6,47] call CBA_fnc_bitflagsCheckAll // returns true - // 6's set bits = 000110 (4,2) + [47,6] call CBA_fnc_bitflagsCheckAll // returns true // 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) // 0 = 0 so return true @@ -36,6 +36,4 @@ Author: #define BASE2LOG(num) ((ln num)*1.44269502162933349609) params ["_flagset","_flags"]; _flagset = [_flagset,floor BASE2LOG(_flagset max _flags)] call CBA_fnc_bitwiseNOT; -private _res = [_flags,_flagset] call CBA_fnc_bitwiseAND; -if (_res isEqualTo 0) exitWith {true}; -_res \ No newline at end of file +[_flagset,_flags] call CBA_fnc_bitwiseAND; diff --git a/addons/bitwise/fnc_bitflagsFlip.sqf b/addons/bitwise/fnc_bitflagsFlip.sqf index 56632eef8..8985287cd 100644 --- a/addons/bitwise/fnc_bitflagsFlip.sqf +++ b/addons/bitwise/fnc_bitflagsFlip.sqf @@ -4,21 +4,20 @@ Function: CBA_fnc_bitflagsFlip Description: Flips the flags' specified bits in the flagset. - * This function assumes the larger number is the flagset and the smaller number is the flags for calculation parity. * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. Parameters: - _flags - flags to flip _flagset - flagset to flip flags within + _flags - flags to flip Returns: - Sum of set bits on success, false otherwise. + Sum of set bits on success, -1 otherwise. Examples: (begin example) - [23,62] call CBA_fnc_bitflagsFlip; // returns 41 - // 23's set bits = 010111 (16,4,2,1) + [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 diff --git a/addons/bitwise/fnc_bitflagsSet.sqf b/addons/bitwise/fnc_bitflagsSet.sqf index 4d693b4e9..ec320b214 100644 --- a/addons/bitwise/fnc_bitflagsSet.sqf +++ b/addons/bitwise/fnc_bitflagsSet.sqf @@ -4,21 +4,20 @@ Function: CBA_fnc_bitflagsSet Description: Sets the flags' specified bits in the flagset. Has no effect if the bits are already set. - * This function assumes the larger number is the flagset and the smaller number is the flags for calculation parity. * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. Parameters: - _flags - flags to set _flagset - flagset to set flags within + _flags - flags to set Returns: Sum of set bits on success, false otherwise. Examples: (begin example) - [12,55] call CBA_fnc_bitflagsSet; // returns 63 - // 12's set bits = 001100 (8,4) + [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) diff --git a/addons/bitwise/fnc_bitflagsUnset.sqf b/addons/bitwise/fnc_bitflagsUnset.sqf index 0d0760ccb..1c86ffc70 100644 --- a/addons/bitwise/fnc_bitflagsUnset.sqf +++ b/addons/bitwise/fnc_bitflagsUnset.sqf @@ -7,11 +7,11 @@ Description: * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. Parameters: - _flagset - a number representing currently set bitflags + _flagset - flagset to set flags within _flags - the flags to unset Returns: - Sum of set bits on success, false otherwise. + Sum of set bits on success, -1 otherwise. Examples: (begin example) @@ -28,5 +28,5 @@ Author: ---------------------------------------------------------------------------- */ params ["_flagset","_flags"]; private _andReturn = [_flagset,_flags] call CBA_fnc_bitwiseAND; -if (_andReturn isEqualType false) exitWith {false}; +if (_andReturn < 0) exitWith {-1}; (floor abs _flagset) - _andReturn diff --git a/addons/bitwise/fnc_bitwiseAND.sqf b/addons/bitwise/fnc_bitwiseAND.sqf index 701ef290b..5da210538 100644 --- a/addons/bitwise/fnc_bitwiseAND.sqf +++ b/addons/bitwise/fnc_bitwiseAND.sqf @@ -2,8 +2,9 @@ Function: CBA_fnc_bitwiseAND Description: - Performs a bitwise AND operation between two decimal numbers. + Performs a bitwise AND operation between two numbers. + * 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: @@ -11,7 +12,7 @@ Parameters: _num2 - another number to compare to the first Returns: - Resulting number on success, false otherwise. + Resulting number on success, -1 otherwise. Examples: (begin example) @@ -31,7 +32,7 @@ _this = _this apply {floor abs _x}; _this sort true; params [["_min",0,[0]],["_max",1,[0]]]; private _end = floor BASE2LOG(_min); //1/ln(2) = 1.44269502162933349609 -if (_end >= 24) exitWith {false}; // precision drop after 2^24 (16777216) +if (_end >= 24) exitWith {-1}; // precision drop after 2^24 (16777216) private _power = 0; private _return = 0; for "_i" from 0 to _end do { diff --git a/addons/bitwise/fnc_bitwiseLROT.sqf b/addons/bitwise/fnc_bitwiseLROT.sqf index ca57b7e47..f440ed361 100644 --- a/addons/bitwise/fnc_bitwiseLROT.sqf +++ b/addons/bitwise/fnc_bitwiseLROT.sqf @@ -3,8 +3,8 @@ 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 returns a non-negative integer. + + * 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: @@ -17,8 +17,9 @@ Returns: Examples: (begin example) 25 call CBA_fnc_bitwiseLROT; // returns 19 - // 25's set bits = 11001 (16+8+1) - // shift left by 1, move leftmost bit to back = 10011 (16+2+1) + // 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 @@ -31,7 +32,7 @@ Author: ---------------------------------------------------------------------------- */ #define BITREP(num,pow) ((floor (num / pow)) mod 2) #define BASE2LOG(num) ((ln num)*1.44269502162933349609) -params [["_num",1,[0]],["_numRot",1,[0]]]; +params [["_num",0,[0]],["_numRot",0,[0]]]; _num = floor abs _num; _numRot = floor abs _numRot; private _exp = floor BASE2LOG(_num); diff --git a/addons/bitwise/fnc_bitwiseLSHFT.sqf b/addons/bitwise/fnc_bitwiseLSHFT.sqf index bd2cb9f09..4bea2573d 100644 --- a/addons/bitwise/fnc_bitwiseLSHFT.sqf +++ b/addons/bitwise/fnc_bitwiseLSHFT.sqf @@ -4,7 +4,7 @@ Function: CBA_fnc_bitwiseLSHFT Description: Performs a bitwise LEFT-SHIFT operation on a given number. - * This function returns an integer. + * 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: @@ -17,9 +17,13 @@ Returns: 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) + // 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 one's place with 0 = 11001000 (128,64,8) + // sum of shifted bits = 200 (end) Author: diff --git a/addons/bitwise/fnc_bitwiseNOT.sqf b/addons/bitwise/fnc_bitwiseNOT.sqf index b90fce35b..4fa7a0b19 100644 --- a/addons/bitwise/fnc_bitwiseNOT.sqf +++ b/addons/bitwise/fnc_bitwiseNOT.sqf @@ -3,9 +3,10 @@ Function: CBA_fnc_bitwiseNOT Description: Performs a bitwise NOT operation on a number. All bits are flipped. - By default, this function assumes that the largest power of 2 that _num stores is the number of bits it occupies. + + The _base argument is limited to numbers greater than the largest power of 2 stored within _num. - * This function returns a non-negative integer. + * 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: @@ -13,16 +14,16 @@ Parameters: _base - the number of bits this number occupies (optional) Returns: - Resulting number on success, false otherwise. + Resulting 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) - [12] call CBA_fnc_bitwiseNOT; // returns 1 - // 12's set bits = 110 (8,4) - // flip all bits = 001 (1) + [12,6] call CBA_fnc_bitwiseNOT; // returns 57 + // 12's set bits = 000110 (8,4) + // flip all bits = 111001 (32,16,8,1) or 57 (end) Author: @@ -33,5 +34,5 @@ params [["_num",1,[0]],"_base"]; _num = floor abs _num; private _exp = floor BASE2LOG(_num); if (!isNil "_base" && {_base isEqualType 0 && {_base > _exp}}) then {_exp = floor abs _base}; -if (_exp >= 24) exitWith {false}; +if (_exp >= 24) exitWith {-1}; (2^(_exp+1))-1-_num \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseOR.sqf b/addons/bitwise/fnc_bitwiseOR.sqf index 0e0d556fd..1948dfe53 100644 --- a/addons/bitwise/fnc_bitwiseOR.sqf +++ b/addons/bitwise/fnc_bitwiseOR.sqf @@ -4,7 +4,7 @@ Function: CBA_fnc_bitwiseOR Description: Performs a bitwise OR operation between two numbers. Bits that are both zero are not summed. - * This function returns a non-negative integer. + * 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: @@ -12,7 +12,7 @@ Parameters: _num2 - another number to compare to the first Returns: - Resulting number on success, false otherwise. + Resulting number on success, -1 otherwise. Examples: (begin example) @@ -32,7 +32,7 @@ _this = _this apply {floor abs _x}; _this sort true; params [["_min",0,[0]],["_max",1,[0]]]; private _end = BASE2LOG(_max); //1/ln(2) = 1.44269502162933349609 -if (_end >= 24) exitWith {false}; // precision drop after 2^24 (16777216) +if (_end >= 24) exitWith {-1}; // precision drop after 2^24 (16777216) private _power = 0; private _return = 0; private _maxBit = 0; diff --git a/addons/bitwise/fnc_bitwiseRROT.sqf b/addons/bitwise/fnc_bitwiseRROT.sqf index 2efab1f01..7f48dff91 100644 --- a/addons/bitwise/fnc_bitwiseRROT.sqf +++ b/addons/bitwise/fnc_bitwiseRROT.sqf @@ -4,7 +4,7 @@ Function: CBA_fnc_bitwiseRROT Description: Performs a bitwise RIGHT-ROTATE-NO-CARRY operation on a given number. - * This function returns a non-negative integer. + * 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: @@ -17,8 +17,9 @@ Returns: Examples: (begin example) 17 call CBA_fnc_bitwiseRROT; // returns 24 - // 17's set bits = 10001 (16+1) - // shift right by 1, move rightmost bit to front = 11000 (16+8) + // 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 @@ -30,7 +31,7 @@ Author: Daisy ---------------------------------------------------------------------------- */ #define BASE2LOG(number) ((ln number)*1.44269502162933349609) -params [["_num",1,[0]],["_numRot",1,[0]]]; +params [["_num",0,[0]],["_numRot",0,[0]]]; _num = floor abs _num; _numRot = floor abs _numRot; private _exp = floor BASE2LOG(_num); @@ -39,4 +40,18 @@ if (_numRot > _exp) then {_numRot = _numRot % _exp}; for "_i" from 1 to _numRot do { _num = (floor (_num / 2)) + ((_num % 2) * _power); }; +_num + +// or + +#define BASE2LOG(number) ((ln number)*1.44269502162933349609) +params [["_num",0,[0]],["_numRot",0,[0]]]; +_num = floor abs _num; +_numRot = floor abs _numRot; +private _exp = floor BASE2LOG(_num); +private _power = 2^(_exp+1); +if (_numRot > _exp) then {_numRot = _numRot % _exp}; +for "_i" from 1 to _numRot do { + _num = floor ((_power * (_num % 2) + _num) / 2); +}; _num \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseRSHFT.sqf b/addons/bitwise/fnc_bitwiseRSHFT.sqf index 097adb8b3..a2cdd7eff 100644 --- a/addons/bitwise/fnc_bitwiseRSHFT.sqf +++ b/addons/bitwise/fnc_bitwiseRSHFT.sqf @@ -4,7 +4,9 @@ Function: CBA_fnc_bitwiseRSHFT Description: Performs a bitwise logical RIGHT-SHIFT operation on a given number. - * This function returns an integer. + * 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 @@ -15,9 +17,15 @@ Returns: Examples: (begin example) 25 call CBA_fnc_bitwiseRSHFT; // returns 12 - // 25's set bits = 11001 (16+8+1) - // shift bits right by 1, discard bit in 1's place = 1100 (8+4) + // 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: diff --git a/addons/bitwise/fnc_bitwiseXOR.sqf b/addons/bitwise/fnc_bitwiseXOR.sqf index 86e0beaeb..6524142cc 100644 --- a/addons/bitwise/fnc_bitwiseXOR.sqf +++ b/addons/bitwise/fnc_bitwiseXOR.sqf @@ -4,7 +4,7 @@ Function: CBA_fnc_bitwiseXOR Description: Performs a bitwise XOR operation between two numbers. Bits that are different than each other are summed. - * This function returns a non-negative integer. + * 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: @@ -12,7 +12,7 @@ Parameters: _num2 - another number to compare to the first Returns: - Resulting number on success, false otherwise. + Resulting number on success, -1 otherwise. Examples: (begin example) @@ -34,7 +34,7 @@ params [["_min",0,[0]],["_max",1,[0]]]; _min = floor abs _min; _max = floor abs _max; private _end = floor ((ln _max)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 -if (_end >= 24) exitWith {false}; // precision drop after 2^24 (16777216) +if (_end >= 24) exitWith {-1}; // precision drop after 2^24 (16777216) private _power = 0; private _return = 0; for "_i" from 0 to _end do { diff --git a/addons/bitwise/fnc_logBase2.sqf b/addons/bitwise/fnc_logBase2.sqf index 7052d65f1..ba2c3a804 100644 --- a/addons/bitwise/fnc_logBase2.sqf +++ b/addons/bitwise/fnc_logBase2.sqf @@ -4,6 +4,7 @@ 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: From bb37c02708dd2172c54e58506a0a10cc8330a1af Mon Sep 17 00:00:00 2001 From: Daisy <92063434+Anderanged@users.noreply.github.com> Date: Sat, 21 Dec 2024 18:46:06 -0700 Subject: [PATCH 07/11] Final bugfixing + finally working rotate funcs --- addons/bitwise/fnc_bitflagsCheck.sqf | 2 +- addons/bitwise/fnc_bitflagsCheckAll.sqf | 13 ++--- addons/bitwise/fnc_bitflagsUnset.sqf | 2 +- addons/bitwise/fnc_bitwiseAND.sqf | 9 +++- addons/bitwise/fnc_bitwiseLROT.sqf | 68 +++++++++++++++++------- addons/bitwise/fnc_bitwiseLSHFT.sqf | 2 +- addons/bitwise/fnc_bitwiseNOT.sqf | 27 ++++++---- addons/bitwise/fnc_bitwiseOR.sqf | 15 ++++-- addons/bitwise/fnc_bitwiseRROT.sqf | 69 +++++++++++++++---------- addons/bitwise/fnc_bitwiseXOR.sqf | 6 +-- 10 files changed, 138 insertions(+), 75 deletions(-) diff --git a/addons/bitwise/fnc_bitflagsCheck.sqf b/addons/bitwise/fnc_bitflagsCheck.sqf index c0675eab6..375b34184 100644 --- a/addons/bitwise/fnc_bitflagsCheck.sqf +++ b/addons/bitwise/fnc_bitflagsCheck.sqf @@ -7,8 +7,8 @@ Description: * Values above 2^24 suffer inaccuracy at the hands of the Virtual Reality Engine. Inputs exceeding this value will error. Parameters: - _flags - flags to check for _flagset - flagset to check for flags within + _flags - flags to check for Returns: True on success, false otherwise. diff --git a/addons/bitwise/fnc_bitflagsCheckAll.sqf b/addons/bitwise/fnc_bitflagsCheckAll.sqf index d4e8b6e0c..b163036e1 100644 --- a/addons/bitwise/fnc_bitflagsCheckAll.sqf +++ b/addons/bitwise/fnc_bitflagsCheckAll.sqf @@ -2,7 +2,7 @@ Function: CBA_fnc_bitflagsCheckAll Description: - Checks if a given flagset has all the specified flags set. + 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. @@ -11,7 +11,7 @@ Parameters: _flags - flags to check for Returns: - 0 on success, sum of not-set bits on failure, -1 otherwise. + The sum of not-set bits on successful execution, -1 on error. Examples: (begin example) @@ -20,20 +20,21 @@ Examples: // 22's set bits = 010110 (16,4,2) // common set bits = 000110 (4,2) // 22's unique bits = 010000 (16) - // 16 != 0 so return 16 + // sum of 22's unique bits = 16 - [47,6] call CBA_fnc_bitflagsCheckAll // returns true + [47,6] call CBA_fnc_bitflagsCheckAll // 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) - // 0 = 0 so return true + // 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,floor BASE2LOG(_flagset max _flags)] call CBA_fnc_bitwiseNOT; +_flagset = [_flagset,BITQUANT(_flagset max _flags)] call CBA_fnc_bitwiseNOT; [_flagset,_flags] call CBA_fnc_bitwiseAND; diff --git a/addons/bitwise/fnc_bitflagsUnset.sqf b/addons/bitwise/fnc_bitflagsUnset.sqf index 1c86ffc70..4ff8deaa5 100644 --- a/addons/bitwise/fnc_bitflagsUnset.sqf +++ b/addons/bitwise/fnc_bitflagsUnset.sqf @@ -26,7 +26,7 @@ Examples: Author: Daisy ---------------------------------------------------------------------------- */ -params ["_flagset","_flags"]; +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 index 5da210538..fbcc21db1 100644 --- a/addons/bitwise/fnc_bitwiseAND.sqf +++ b/addons/bitwise/fnc_bitwiseAND.sqf @@ -26,17 +26,22 @@ Examples: Author: Daisy ---------------------------------------------------------------------------- */ -#define BITREP(num,pow) ((floor (num / pow)) mod 2) +#define BITGRAB(num,pow) ((floor (num / pow)) mod 2) #define BASE2LOG(num) ((ln num)*1.44269502162933349609) + _this = _this apply {floor abs _x}; _this sort true; params [["_min",0,[0]],["_max",1,[0]]]; + 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 * (BITREP(_max,_power) * BITREP(_min,_power))); + _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 index f440ed361..16081d844 100644 --- a/addons/bitwise/fnc_bitwiseLROT.sqf +++ b/addons/bitwise/fnc_bitwiseLROT.sqf @@ -5,43 +5,71 @@ 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 cause unexpected behavior. + * 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 + _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, false otherwise. + 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) + // 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) + // 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 + + [1645,634] ---------------------------------------------------------------------------- */ -#define BITREP(num,pow) ((floor (num / pow)) mod 2) +#define BITGRAB(num,pow) ((floor (num / pow)) mod 2) #define BASE2LOG(num) ((ln num)*1.44269502162933349609) -params [["_num",0,[0]],["_numRot",0,[0]]]; +#define BITQUANT(num) floor BASE2LOG(num) + 1 + +params [["_num",0,[0]],["_numRot",0,[0]],"_numBits"]; + _num = floor abs _num; _numRot = floor abs _numRot; -private _exp = floor BASE2LOG(_num); -private _power = 2^_exp; -private _power1 = _power * 2; // 2^_exp+1 -private _bit = 0; -if (_numRot > _exp) then {_numRot = _numRot % _exp}; -for "_i" from 1 to _numRot do { - _bit = BITREP(_num,_power); - _num = (2*_num) - (_bit * _power1) + _bit; + +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 \ No newline at end of file + +_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 index 4bea2573d..34e63ee80 100644 --- a/addons/bitwise/fnc_bitwiseLSHFT.sqf +++ b/addons/bitwise/fnc_bitwiseLSHFT.sqf @@ -22,7 +22,7 @@ Examples: // 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 one's place with 0 = 11001000 (128,64,8) + // shift bits left by 3, fill blank places with 0 = 11001000 (128,64,8) // sum of shifted bits = 200 (end) diff --git a/addons/bitwise/fnc_bitwiseNOT.sqf b/addons/bitwise/fnc_bitwiseNOT.sqf index 4fa7a0b19..525e021ca 100644 --- a/addons/bitwise/fnc_bitwiseNOT.sqf +++ b/addons/bitwise/fnc_bitwiseNOT.sqf @@ -4,35 +4,42 @@ Function: CBA_fnc_bitwiseNOT Description: Performs a bitwise NOT operation on a number. All bits are flipped. - The _base argument is limited to numbers greater than the largest power of 2 stored within _num. + 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 - a number - _base - the number of bits this number occupies (optional) + _num - the number to invert + _numBits - the number of bits this number should occupy (optional) Returns: - Resulting number on success, -1 otherwise. + 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) or 57 + // 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]],"_base"]; + +params [["_num",1,[0]],"_numBits"]; + _num = floor abs _num; -private _exp = floor BASE2LOG(_num); -if (!isNil "_base" && {_base isEqualType 0 && {_base > _exp}}) then {_exp = floor abs _base}; -if (_exp >= 24) exitWith {-1}; -(2^(_exp+1))-1-_num \ No newline at end of file + +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 index 1948dfe53..5ea38e3f6 100644 --- a/addons/bitwise/fnc_bitwiseOR.sqf +++ b/addons/bitwise/fnc_bitwiseOR.sqf @@ -26,21 +26,26 @@ Examples: Author: Daisy ---------------------------------------------------------------------------- */ -#define BITREP(num,pow) ((floor (num / pow)) mod 2) +#define BITGRAB(num,pow) ((floor (num / pow)) mod 2) #define BASE2LOG(num) ((ln num)*1.44269502162933349609) + _this = _this apply {floor abs _x}; _this sort true; params [["_min",0,[0]],["_max",1,[0]]]; -private _end = BASE2LOG(_max); //1/ln(2) = 1.44269502162933349609 -if (_end >= 24) exitWith {-1}; // precision drop after 2^24 (16777216) + +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 = BITREP(_max,_power); - _minBit = BITREP(_min,_power); + _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 index 7f48dff91..a0a31a5dd 100644 --- a/addons/bitwise/fnc_bitwiseRROT.sqf +++ b/addons/bitwise/fnc_bitwiseRROT.sqf @@ -5,14 +5,15 @@ 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 cause unexpected behavior. + * 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 + _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, false otherwise. + Rotated number on success, -1 otherwise. Examples: (begin example) @@ -23,35 +24,51 @@ Examples: // sum of rotated bits = 24 [17,2] call CBA_fnc_bitwiseRROT; // returns 12 - // rotates once to 11000 (24) - // rotates again to 01100 (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 BASE2LOG(number) ((ln number)*1.44269502162933349609) -params [["_num",0,[0]],["_numRot",0,[0]]]; -_num = floor abs _num; -_numRot = floor abs _numRot; -private _exp = floor BASE2LOG(_num); -private _power = 2^_exp; -if (_numRot > _exp) then {_numRot = _numRot % _exp}; -for "_i" from 1 to _numRot do { - _num = (floor (_num / 2)) + ((_num % 2) * _power); -}; -_num +#define BITGRAB(num,pow) ((floor (num / pow)) mod 2) +#define BASE2LOG(num) ((ln num)*1.44269502162933349609) +#define BITQUANT(num) floor BASE2LOG(num) + 1 -// or +params [["_num",0,[0]],["_numRot",0,[0]],"_numBits"]; -#define BASE2LOG(number) ((ln number)*1.44269502162933349609) -params [["_num",0,[0]],["_numRot",0,[0]]]; _num = floor abs _num; _numRot = floor abs _numRot; -private _exp = floor BASE2LOG(_num); -private _power = 2^(_exp+1); -if (_numRot > _exp) then {_numRot = _numRot % _exp}; -for "_i" from 1 to _numRot do { - _num = floor ((_power * (_num % 2) + _num) / 2); + +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 }; -_num \ No newline at end of file + +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_bitwiseXOR.sqf b/addons/bitwise/fnc_bitwiseXOR.sqf index 6524142cc..0e4cae41f 100644 --- a/addons/bitwise/fnc_bitwiseXOR.sqf +++ b/addons/bitwise/fnc_bitwiseXOR.sqf @@ -27,18 +27,18 @@ Examples: Author: Daisy ---------------------------------------------------------------------------- */ -#define BITREP(num,pow) ((floor (num / pow)) mod 2) +#define BITGRAB(num,pow) ((floor (num / pow)) mod 2) #define BASE2LOG(num) ((ln num)*1.44269502162933349609) _this sort true; params [["_min",0,[0]],["_max",1,[0]]]; _min = floor abs _min; _max = floor abs _max; private _end = floor ((ln _max)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 -if (_end >= 24) exitWith {-1}; // precision drop after 2^24 (16777216) +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 * ((BITREP(_max,_power) + BITREP(_min,_power)) % 2)); + _return = _return + (_power * ((BITGRAB(_max,_power) + BITGRAB(_min,_power)) % 2)); }; _return \ No newline at end of file From 267cbd551f12547efa5d46ea2949088dde040e8f Mon Sep 17 00:00:00 2001 From: Daisy <92063434+Anderanged@users.noreply.github.com> Date: Sat, 21 Dec 2024 20:18:22 -0700 Subject: [PATCH 08/11] renaming & final checks --- addons/bitwise/CfgFunctions.hpp | 2 +- addons/bitwise/fnc_bitflagsCheck.sqf | 29 +++++++++++------ addons/bitwise/fnc_bitflagsCheckAll.sqf | 40 ------------------------ addons/bitwise/fnc_bitflagsCheckBool.sqf | 30 ++++++++++++++++++ addons/bitwise/fnc_bitwiseLROT.sqf | 2 -- addons/bitwise/fnc_bitwiseRROT.sqf | 2 +- 6 files changed, 52 insertions(+), 53 deletions(-) delete mode 100644 addons/bitwise/fnc_bitflagsCheckAll.sqf create mode 100644 addons/bitwise/fnc_bitflagsCheckBool.sqf diff --git a/addons/bitwise/CfgFunctions.hpp b/addons/bitwise/CfgFunctions.hpp index 70ea19e2e..e1a39e56a 100644 --- a/addons/bitwise/CfgFunctions.hpp +++ b/addons/bitwise/CfgFunctions.hpp @@ -2,7 +2,7 @@ class CfgFunctions { class CBA { class Bitwise { PATHTO_FNC(bitflagsCheck); - PATHTO_FNC(bitflagsCheckAll); + PATHTO_FNC(bitflagsCheckBool); PATHTO_FNC(bitflagsFlip); PATHTO_FNC(bitflagsSet); PATHTO_FNC(bitflagsUnset); diff --git a/addons/bitwise/fnc_bitflagsCheck.sqf b/addons/bitwise/fnc_bitflagsCheck.sqf index 375b34184..8a04ecb61 100644 --- a/addons/bitwise/fnc_bitflagsCheck.sqf +++ b/addons/bitwise/fnc_bitflagsCheck.sqf @@ -2,7 +2,7 @@ Function: CBA_fnc_bitflagsCheck Description: - Checks if a given flagset has at least one of the specified flags set. + 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. @@ -11,19 +11,30 @@ Parameters: _flags - flags to check for Returns: - True on success, false otherwise. + The sum of not-set bits on success, -1 on error. Examples: (begin example) - [70,47] call CBA_fnc_bitflagsCheck // returns true: - // 70's set bits = 1000110 (64,4,2) - // 47's set bits = 0101111 (32,8,4,2,1) - // common set bits = 0000110 (4,2) - // sum of common set bits = 6 - // 6 > 0 so return true + [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 ---------------------------------------------------------------------------- */ -_this call CBA_fnc_bitwiseAND > 0 \ No newline at end of file +#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; diff --git a/addons/bitwise/fnc_bitflagsCheckAll.sqf b/addons/bitwise/fnc_bitflagsCheckAll.sqf deleted file mode 100644 index b163036e1..000000000 --- a/addons/bitwise/fnc_bitflagsCheckAll.sqf +++ /dev/null @@ -1,40 +0,0 @@ -/* ---------------------------------------------------------------------------- -Function: CBA_fnc_bitflagsCheckAll - -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 successful execution, -1 on error. - -Examples: - (begin example) - [47,22] call CBA_fnc_bitflagsCheckAll // 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_bitflagsCheckAll // 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; 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_bitwiseLROT.sqf b/addons/bitwise/fnc_bitwiseLROT.sqf index 16081d844..915b1beba 100644 --- a/addons/bitwise/fnc_bitwiseLROT.sqf +++ b/addons/bitwise/fnc_bitwiseLROT.sqf @@ -35,8 +35,6 @@ Examples: Author: Daisy - - [1645,634] ---------------------------------------------------------------------------- */ #define BITGRAB(num,pow) ((floor (num / pow)) mod 2) #define BASE2LOG(num) ((ln num)*1.44269502162933349609) diff --git a/addons/bitwise/fnc_bitwiseRROT.sqf b/addons/bitwise/fnc_bitwiseRROT.sqf index a0a31a5dd..0ea68bfe0 100644 --- a/addons/bitwise/fnc_bitwiseRROT.sqf +++ b/addons/bitwise/fnc_bitwiseRROT.sqf @@ -45,7 +45,7 @@ params [["_num",0,[0]],["_numRot",0,[0]],"_numBits"]; _num = floor abs _num; _numRot = floor abs _numRot; -private _bitCount = BITQUANT(num); +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 From f149d5b932843f30b5ac032003ac06316b6edefb Mon Sep 17 00:00:00 2001 From: Daisy <92063434+Anderanged@users.noreply.github.com> Date: Sat, 21 Dec 2024 20:44:07 -0700 Subject: [PATCH 09/11] documentation and readability --- addons/bitwise/fnc_bitflagsCheck.sqf | 3 ++- addons/bitwise/fnc_bitwiseAND.sqf | 5 +++-- addons/bitwise/fnc_bitwiseOR.sqf | 5 +++-- addons/bitwise/fnc_bitwiseXOR.sqf | 13 +++++++++---- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/addons/bitwise/fnc_bitflagsCheck.sqf b/addons/bitwise/fnc_bitflagsCheck.sqf index 8a04ecb61..a639085a1 100644 --- a/addons/bitwise/fnc_bitflagsCheck.sqf +++ b/addons/bitwise/fnc_bitflagsCheck.sqf @@ -35,6 +35,7 @@ Author: ---------------------------------------------------------------------------- */ #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; +[_flagset,_flags] call CBA_fnc_bitwiseAND; \ No newline at end of file diff --git a/addons/bitwise/fnc_bitwiseAND.sqf b/addons/bitwise/fnc_bitwiseAND.sqf index fbcc21db1..8b84bc116 100644 --- a/addons/bitwise/fnc_bitwiseAND.sqf +++ b/addons/bitwise/fnc_bitwiseAND.sqf @@ -5,11 +5,12 @@ 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: - _num1 - a number - _num2 - another number to compare to the first + _min - a number + _max - another number to compare to the first Returns: Resulting number on success, -1 otherwise. diff --git a/addons/bitwise/fnc_bitwiseOR.sqf b/addons/bitwise/fnc_bitwiseOR.sqf index 5ea38e3f6..c47f2c474 100644 --- a/addons/bitwise/fnc_bitwiseOR.sqf +++ b/addons/bitwise/fnc_bitwiseOR.sqf @@ -5,11 +5,12 @@ 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: - _num1 - a number - _num2 - another number to compare to the first + _min - a number + _max - another number to compare to the first Returns: Resulting number on success, -1 otherwise. diff --git a/addons/bitwise/fnc_bitwiseXOR.sqf b/addons/bitwise/fnc_bitwiseXOR.sqf index 0e4cae41f..13e3aa361 100644 --- a/addons/bitwise/fnc_bitwiseXOR.sqf +++ b/addons/bitwise/fnc_bitwiseXOR.sqf @@ -5,11 +5,12 @@ 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: - _num1 - a number - _num2 - another number to compare to the first + _min - a number + _max - another number to compare to the first Returns: Resulting number on success, -1 otherwise. @@ -29,16 +30,20 @@ Author: ---------------------------------------------------------------------------- */ #define BITGRAB(num,pow) ((floor (num / pow)) mod 2) #define BASE2LOG(num) ((ln num)*1.44269502162933349609) + +_this = _this apply {floor abs _x}; _this sort true; params [["_min",0,[0]],["_max",1,[0]]]; -_min = floor abs _min; -_max = floor abs _max; + 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 From 2ddad3bb2a27713cf8b0f0e19a19108bbd65863b Mon Sep 17 00:00:00 2001 From: Daisy <92063434+Anderanged@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:58:46 -0700 Subject: [PATCH 10/11] fixes to _this var reassignment --- addons/bitwise/fnc_bitwiseAND.sqf | 6 +++--- addons/bitwise/fnc_bitwiseOR.sqf | 6 +++--- addons/bitwise/fnc_bitwiseXOR.sqf | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/addons/bitwise/fnc_bitwiseAND.sqf b/addons/bitwise/fnc_bitwiseAND.sqf index 8b84bc116..0a37b34ae 100644 --- a/addons/bitwise/fnc_bitwiseAND.sqf +++ b/addons/bitwise/fnc_bitwiseAND.sqf @@ -30,9 +30,9 @@ Author: #define BITGRAB(num,pow) ((floor (num / pow)) mod 2) #define BASE2LOG(num) ((ln num)*1.44269502162933349609) -_this = _this apply {floor abs _x}; -_this sort true; -params [["_min",0,[0]],["_max",1,[0]]]; +private _input = _this 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) diff --git a/addons/bitwise/fnc_bitwiseOR.sqf b/addons/bitwise/fnc_bitwiseOR.sqf index c47f2c474..b01522d64 100644 --- a/addons/bitwise/fnc_bitwiseOR.sqf +++ b/addons/bitwise/fnc_bitwiseOR.sqf @@ -30,9 +30,9 @@ Author: #define BITGRAB(num,pow) ((floor (num / pow)) mod 2) #define BASE2LOG(num) ((ln num)*1.44269502162933349609) -_this = _this apply {floor abs _x}; -_this sort true; -params [["_min",0,[0]],["_max",1,[0]]]; +private _input = _this apply {floor abs _x}; +_input sort true; +_input params [["_min",0,[0]],["_max",1,[0]]]; private _end = floor BASE2LOG(_max); //1/ln(2) = 1.44269502162933349609 if (_end > 24) exitWith {-1}; // precision drop after 2^24 (16777216) diff --git a/addons/bitwise/fnc_bitwiseXOR.sqf b/addons/bitwise/fnc_bitwiseXOR.sqf index 13e3aa361..95f1ec071 100644 --- a/addons/bitwise/fnc_bitwiseXOR.sqf +++ b/addons/bitwise/fnc_bitwiseXOR.sqf @@ -31,9 +31,9 @@ Author: #define BITGRAB(num,pow) ((floor (num / pow)) mod 2) #define BASE2LOG(num) ((ln num)*1.44269502162933349609) -_this = _this apply {floor abs _x}; -_this sort true; -params [["_min",0,[0]],["_max",1,[0]]]; +private _input = _this apply {floor abs _x}; +_input sort true; +_input params [["_min",0,[0]],["_max",1,[0]]]; private _end = floor ((ln _max)*1.44269502162933349609); //1/ln(2) = 1.44269502162933349609 if (_end > 24) exitWith {-1}; // precision drop after 2^24 (16777216) From 409085230e1f8145920d0fa66a35b86d2f1ce56f Mon Sep 17 00:00:00 2001 From: Daisy <92063434+Anderanged@users.noreply.github.com> Date: Sat, 18 Jan 2025 19:34:25 -0700 Subject: [PATCH 11/11] limiting scope of arr sort --- addons/bitwise/fnc_bitwiseAND.sqf | 4 +++- addons/bitwise/fnc_bitwiseOR.sqf | 6 ++++-- addons/bitwise/fnc_bitwiseXOR.sqf | 6 ++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/addons/bitwise/fnc_bitwiseAND.sqf b/addons/bitwise/fnc_bitwiseAND.sqf index 0a37b34ae..3db7cb513 100644 --- a/addons/bitwise/fnc_bitwiseAND.sqf +++ b/addons/bitwise/fnc_bitwiseAND.sqf @@ -30,7 +30,9 @@ Author: #define BITGRAB(num,pow) ((floor (num / pow)) mod 2) #define BASE2LOG(num) ((ln num)*1.44269502162933349609) -private _input = _this apply {floor abs _x}; +private _input = _this; +_input resize 2; +_input = _input apply {floor abs _x}; _input sort true; _input params [["_min",0,[0]],["_max",1,[1]]]; diff --git a/addons/bitwise/fnc_bitwiseOR.sqf b/addons/bitwise/fnc_bitwiseOR.sqf index b01522d64..e4c72ec25 100644 --- a/addons/bitwise/fnc_bitwiseOR.sqf +++ b/addons/bitwise/fnc_bitwiseOR.sqf @@ -30,9 +30,11 @@ Author: #define BITGRAB(num,pow) ((floor (num / pow)) mod 2) #define BASE2LOG(num) ((ln num)*1.44269502162933349609) -private _input = _this apply {floor abs _x}; +private _input = _this; +_input resize 2; +_input = _input apply {floor abs _x}; _input sort true; -_input params [["_min",0,[0]],["_max",1,[0]]]; +_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) diff --git a/addons/bitwise/fnc_bitwiseXOR.sqf b/addons/bitwise/fnc_bitwiseXOR.sqf index 95f1ec071..bbb8236c9 100644 --- a/addons/bitwise/fnc_bitwiseXOR.sqf +++ b/addons/bitwise/fnc_bitwiseXOR.sqf @@ -31,9 +31,11 @@ Author: #define BITGRAB(num,pow) ((floor (num / pow)) mod 2) #define BASE2LOG(num) ((ln num)*1.44269502162933349609) -private _input = _this apply {floor abs _x}; +private _input = _this; +_input resize 2; +_input = _input apply {floor abs _x}; _input sort true; -_input params [["_min",0,[0]],["_max",1,[0]]]; +_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)