Skip to content

Commit

Permalink
Initial release of this repository
Browse files Browse the repository at this point in the history
  • Loading branch information
SteezCram committed Jul 30, 2020
1 parent cc54ecc commit 680f07d
Show file tree
Hide file tree
Showing 10 changed files with 736 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Byte/bitwise/fn_bitwiseAnd.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Name: fn_bitwiseAnd.sqf
Author: Steez
Description: Bitwise AND a binary representation
Method: bitwiseOr
Arguments:
* `_x` - binary representation to use (i.e [1, 0, 0, ..., 1])
* `_y` - binary representation to use (i.e [1, 0, 0, ..., 1])
* `_bits` - int bits width, _x and _y need to have the same bits width !!!
Return:
* `array number` - bitwise AND binary representation (i.e [1, 0, 0, ..., 1])
*/

params [["_x", [], [[]]], ["_y", [], [[]]], ["_bits", 0, [0]]];
// Set the bits width
if (_bits isEqualTo 0) then { _bits = 32 };

// Add all the missing bits to the _x array
reverse _x;
for "_i" from 0 to (_bits - count(_x) - 1) do {
_x pushBack 0;
};
reverse _x;

// Add all the missing bits to the _y array
reverse _y;
for "_i" from 0 to (_bits - count(_y) - 1) do {
_y pushBack 0;
};
reverse _y;


// Apply the operation
_result = [];
for "_i" from 0 to _bits -1 do {
if (((_x # _i) isEqualTo 1) && ((_y # _i) isEqualTo 1)) then {
_result pushBack 1;
} else {
_result pushBack 0;
};
};

_result;
44 changes: 44 additions & 0 deletions Byte/bitwise/fn_bitwiseOr.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Name: fn_bitwiseOr.sqf
Author: Steez
Description: Bitwise OR a binary representation
Method: bitwiseOr
Arguments:
* `_x` - binary representation to use (i.e [1, 0, 0, ..., 1])
* `_y` - binary representation to use (i.e [1, 0, 0, ..., 1])
* `_bits` - int bits width, _x and _y need to have the same bits width !!!
Return:
* `array number` - bitwise OR binary representation (i.e [1, 0, 0, ..., 1])
*/

params [["_x", [], [[]]], ["_y", [], [[]]], ["_bits", 0, [0]]];
// Set the bits width
if (_bits isEqualTo 0) then { _bits = 32 };

// Add all the missing bits to the _x array
reverse _x;
for "_i" from 0 to (_bits - count(_x) - 1) do {
_x pushBack 0;
};
reverse _x;

// Add all the missing bits to the _y array
reverse _y;
for "_i" from 0 to (_bits - count(_y) - 1) do {
_y pushBack 0;
};
reverse _y;


// Apply the operation
_result = [];
for "_i" from 0 to _bits -1 do {
if (((_x # _i) isEqualTo 1) || ((_y # _i) isEqualTo 1)) then {
_result pushBack 1;
} else {
_result pushBack 0;
};
};

_result;
44 changes: 44 additions & 0 deletions Byte/bitwise/fn_bitwiseXor.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Name: fn_bitwiseXor.sqf
Author: Steez
Description: Bitwise XOR a binary representation
Method: bitwiseOr
Arguments:
* `_x` - binary representation to use (i.e [1, 0, 0, ..., 1])
* `_y` - binary representation to use (i.e [1, 0, 0, ..., 1])
* `_bits` - int bits width, _x and _y need to have the same bits width !!!
Return:
* `array number` - bitwise XOR binary representation (i.e [1, 0, 0, ..., 1])
*/

params [["_x", [], [[]]], ["_y", [], [[]]], ["_bits", 0, [0]]];
// Set the bits width
if (_bits isEqualTo 0) then { _bits = 32 };

// Add all the missing bits to the _x array
reverse _x;
for "_i" from 0 to (_bits - count(_x) - 1) do {
_x pushBack 0;
};
reverse _x;

// Add all the missing bits to the _y array
reverse _y;
for "_i" from 0 to (_bits - count(_y) - 1) do {
_y pushBack 0;
};
reverse _y;


// Apply the operation
_result = [];
for "_i" from 0 to _bits -1 do {
if ((_x # _i) != ((_y # _i))) then {
_result pushBack 1;
} else {
_result pushBack 0;
};
};

_result;
27 changes: 27 additions & 0 deletions Byte/fn_binaryToDecimal.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Name: fn_binaryToDecimal.sqf
Author: Steez
Description: Convert a binary representation to it's decimal representation
Method: binaryToDecimal
Arguments:
* `_b` - binary representation to convert (i.e [1, 0, 0, ..., 1])
Return:
* `int` - the decimal representation of the binary representation
*/

params [["_b", [], [[]]]];

reverse _b;
_d = 0;
_p = 0;

// Compute the decimal number
{
if (_x isEqualTo 1) then {
_d = _d + 2 ^ _p;
};
_p = _p + 1;
} foreach _b;

_d;
173 changes: 173 additions & 0 deletions Byte/fn_codePointToByte.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
Name: fn_codePointToByte.sqf
Author: Steez
Description: Unicode code point converter to byte
This file is ready to use with no external dependency
Method: codePointToByte
Arguments:
* `_codePoint` - int code point to convert
Return:
* `byte or byte array` - converted byte or byte array (depend of the code point)
*/

params [["_codePoint", 0, [0]]];

// Error handle
if (_codePoint isEqualTo 0) exitWith {};


// Function helper
_decimalToBinary = {
params [["_n", 0, [0]]];
_k = [];

// Get the binary value
while {_n > 0} do {
_a = _n % 2;
_k pushBack _a;
_n = (_n - _a) / 2;
};

// Reverse the array
reverse _k;
_k;
};
_binaryToDecimal = {
params [["_b", [], [[]]]];
reverse _b;
_d = 0;
_p = 0;

// Compute the decimal number
{
if (_x isEqualTo 1) then {
_d = _d + 2 ^ _p;
};
_p = _p + 1;
} foreach _b;

_d;
};
_truncateBinary = {
params [["_b", [], [[]]]];

// Truncate the array
for "_i" from 0 to count(_b) - 1 do {
if ((_b # _i) isEqualTo 0) then {
_b deleteAt _i;
};
if ((_b # _i) isEqualTo 1) exitWith {};
};

_b;
};

// Bitwise functions
_bitwiseAnd = {
params [["_x", [], [[]]], ["_y", [], [[]]], ["_bits", 0, [0]]];
// Set the bits width
if (_bits isEqualTo 0) then { _bits = 32 };

// Add all the missing bits to the _x array
reverse _x;
for "_i" from 0 to (_bits - count(_x) - 1) do {
_x pushBack 0;
};
reverse _x;

// Add all the missing bits to the _y array
reverse _y;
for "_i" from 0 to (_bits - count(_y) - 1) do {
_y pushBack 0;
};
reverse _y;


// Apply the operation
_result = [];
for "_i" from 0 to _bits -1 do {
if (((_x # _i) isEqualTo 1) && ((_y # _i) isEqualTo 1)) then {
_result pushBack 1;
} else {
_result pushBack 0;
};
};

_result;
};
_bitwiseOr = {
params [["_x", [], [[]]], ["_y", [], [[]]], ["_bits", 0, [0]]];
// Set the bits width
if (_bits isEqualTo 0) then { _bits = 32 };

// Add all the missing bits to the _x array
reverse _x;
for "_i" from 0 to (_bits - count(_x) - 1) do {
_x pushBack 0;
};
reverse _x;

// Add all the missing bits to the _y array
reverse _y;
for "_i" from 0 to (_bits - count(_y) - 1) do {
_y pushBack 0;
};
reverse _y;


// Apply the operation
_result = [];
for "_i" from 0 to _bits -1 do {
if (((_x # _i) isEqualTo 1) || ((_y # _i) isEqualTo 1)) then {
_result pushBack 1;
} else {
_result pushBack 0;
};
};

_result;
};

// Shifting functions
_leftShift = {
params [["_x", 0, [0]], ["_y", 0, [0]]];
(_x * (2 ^ _y))
};
_rightShift = {
params [["_x", 0, [0]], ["_y", 0, [0]]];
floor((_x / (2 ^ _y)))
};


// Main logic
_return = [];

if (_codePoint < 0x80) then {
_return = _codePoint;
};
if (_codePoint >= 0x80 && _codePoint < 0x800) then
{
_bin0xC0 = 0xC0 call _decimalToBinary;
_binCodePoint = call _decimalToBinary;
_bin0x80 = 0x80 call _decimalToBinary;
_bin0x3F = 0x3F call _decimalToBinary;

_codePointShift = [_codePoint, 6] call _rightShift;
_codePointBitwiseAnd = [_binCodePoint, _bin0x3F] call _bitwiseAnd;
_firstByteBin = [_bin0xC0, [_codePointShift] call _decimalToBinary] call _bitwiseOr;
_secondByteBin = [_bin0x80, [_bin0x3F, _codePointBitwiseAnd] call _bitwiseAnd] call _bitwiseOr;

_firstByteNumber = [[_firstByteBin] call _truncateBinary] call _binaryToDecimal;
_secondByteNumber = [[_secondByteBin] call _truncateBinary] call _binaryToDecimal;

_return = [_firstByteNumber, _secondByteNumber];
};
/*if (_codePoint >= 0x800 && _codePoint < 0x10000) then {
_return = [(0xE0 | (_codePoint >> 12)), (0x80 | (_codePoint >> 6 & 0x3F)), (0x80 | (_codePoint & 0x3F))];
};
if (_codePoint >= 0x10000 && _codePoint < 0x110000) then {
_return = [(0xF0 | (_codePoint >> 12)), (0x80 | (_codePoint >> 12 & 0x3F)), (0x80 | (_codePoint >> 6 & 0x3F)), (0x80 | (_codePoint & 0x3F))];
};*/

_return;
26 changes: 26 additions & 0 deletions Byte/fn_decimalToBinary.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Name: fn_decimalToBinary.sqf
Author: Steez
Description: Convert a number into it's binary representation
Method: decimalToBinary
Arguments:
* `_n` - int to convert
Return:
* `array number` - binary representation array of the number (i.e [1, 0, 0, ..., 1])
*/

params [["_n", 0, [0]]];

_k = [];

// Get the binary value
while {_n > 0} do {
_a = _n % 2;
_k pushBack _a;
_n = (_n - _a) / 2;
};

// Reverse the array
reverse _k;
_k;
23 changes: 23 additions & 0 deletions Byte/fn_truncateBinary.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Name: fn_truncateBinary.sqf
Author: Steez
Description: Truncate a binary representation with a specific bits width
Method: truncateBinary
Arguments:
* `_b` - binary representation to convert (i.e [1, 0, 0, ..., 1])
Return:
* `array number` - the binary representation truncate
*/

params [["_b", [], [[]]]];

// Truncate the array
for "_i" from 0 to count(_b) - 1 do {
if ((_b # _i) isEqualTo 0) then {
_b deleteAt _i;
};
if ((_b # _i) isEqualTo 1) exitWith {};
};

_b;
Loading

0 comments on commit 680f07d

Please sign in to comment.