LibraryBitwiseOperandsLibrary "LibraryBitwiseOperands"
Description: When you need more space for your data you can use bitwise operations. For example if you are creating an Order Block indicator and you have multiple types then you can define variables for each type with only one bit set like these:
const int TYPE_OB = 1
const int TYPE_HH = 2
const int TYPE_LH = 4
const int TYPE_HL = 8
const int TYPE_LL = 16
const int TYPE_BOS = 32
const int TYPE_CHOCH = 64
bitwise_shift_left(x, y)
bitwise_shift_left(): Bitwise left shift: x << y
Parameters:
x (int)
y (int)
Returns: : The left operand’s value is moved toward left by the number of bits specified by the right operand.
bitwise_shift_right(x, y)
bitwise_shift_right(): Bitwise right shift: x >> y
Parameters:
x (int)
y (int)
Returns: : The left operand’s value is moved toward right by the number of bits specified by the right operand.
bitwise_not(x)
bitwise_not(): Bitwise NOT: ~x
Parameters:
x (int)
Returns: : Inverts individual bits.
bitwise_and(x, y)
bitwise_and(): Bitwise AND: x & y
Parameters:
x (int)
y (int)
Returns: : Result bit 1, if both operand bits are 1; otherwise results bit 0.
bitwise_or(x, y)
bitwise_or(): Bitwise OR: x | y
Parameters:
x (int)
y (int)
Returns: : Result bit 1, if any of the operand bit is 1; otherwise results bit 0.
bitwise_xor(x, y)
bitwise_xor(): Bitwise (exclusive OR) XOR: x ^ y
Parameters:
x (int)
y (int)
Returns: : Result bit 1, if any of the operand bit is 1 but not both, otherwise results bit 0.
logical_xor(x, y)
logical_xor(): Logical (exclusive OR) XOR: x xor y
Parameters:
x (bool)
y (bool)
Returns: : Result true, if any of the operand bit are different, otherwise results bit 0.
bit_check(x, y)
bit_check(): Bitwise Checks the specified bit
Parameters:
x (int)
y (int)
Returns: : Returns True if the bit is set.
bit_set(x, y)
bit_set(): Bitwise Sets the specified bit
Parameters:
x (int)
y (int)
Returns: : Returns a value with the bit set.
bit_clear(x, y)
bit_clear(): Bitwise Clears the specified bit
Parameters:
x (int)
y (int)
Returns: : Returns a value with the bit cleared.
bit_flip(x, y)
bit_flip(): Bitwise Inverts the specified bit
Parameters:
x (int)
y (int)
Returns: : Returns a value with the bit inverted.
bitmask_check(x, mask)
bitmask_check(): Bitwise Checks the specified Mask
Parameters:
x (int)
mask (int)
Returns: : Returns True if the mask is set.
bitmask_set(x, mask)
bitmask_set(): Bitwise Sets the specified Mask
Parameters:
x (int)
mask (int)
Returns: : Returns a value with the mask set.
bitmask_clear(x, mask)
bitmask_clear(): Bitwise Clears the specified Mask
Parameters:
x (int)
mask (int)
Returns: : Returns a value with the mask cleared.
bitmask_flip(x, mask)
bitmask_flip(): Inverts the specified Mask
Parameters:
x (int)
mask (int)
Returns: : Returns a value with the mask inverted.
math_bit_check(x, y)
math_bit_check(): Fast arithmetic bitwise Checks the specified Mask
Parameters:
x (int)
y (int)
Returns: : Returns True if the mask is set.
math_bit_set(x, y)
math_bit_set(): Fast arithmetic bitwise Sets the specified bit
Parameters:
x (int)
y (int)
Returns: : Returns a value with the bit set.
math_bit_clear(x, y)
math_bit_clear(): Fast arithmetic bitwise Clears the specified bit
Parameters:
x (int)
y (int)
Returns: : Returns a value with the bit cleared.
math_bitmask_check(x, y)
math_bitmask_check(): Fast arithmetic bitwise Checks the specified Mask
Parameters:
x (int)
y (int)
Returns: : Returns True if the mask is set.
math_bitmask_set(x, y)
math_bitmask_set(): Fast arithmetic bitwise Sets the specified Mask
Parameters:
x (int)
y (int)
Returns: : Returns a value with the mask set.
math_bitmask_clear(x, y)
math_bitmask_clear(): Fast arithmetic bitwise Clears the specified Mask
Parameters:
x (int)
y (int)
Returns: : Returns a value with the mask cleared.
Bitwise
Bitwise, Encode, DecodeLibrary "Bitwise, Encode, Decode"
Bitwise, Encode, Decode, and more Library
docs()
Hover-Over Documentation for inside Text Editor
bAnd(a, b)
Returns the bitwise AND of two integers
Parameters:
a : `int` - The first integer
b : `int` - The second integer
Returns: `int` - The bitwise AND of the two integers
bOr(a, b)
Performs a bitwise OR operation on two integers.
Parameters:
a : `int` - The first integer.
b : `int` - The second integer.
Returns: `int` - The result of the bitwise OR operation.
bXor(a, b)
Performs a bitwise Xor operation on two integers.
Parameters:
a : `int` - The first integer.
b : `int` - The second integer.
Returns: `int` - The result of the bitwise Xor operation.
bNot(n)
Performs a bitwise NOT operation on an integer.
Parameters:
n : `int` - The integer to perform the bitwise NOT operation on.
Returns: `int` - The result of the bitwise NOT operation.
bShiftLeft(n, step)
Performs a bitwise left shift operation on an integer.
Parameters:
n : `int` - The integer to perform the bitwise left shift operation on.
step : `int` - The number of positions to shift the bits to the left.
Returns: `int` - The result of the bitwise left shift operation.
bShiftRight(n, step)
Performs a bitwise right shift operation on an integer.
Parameters:
n : `int` - The integer to perform the bitwise right shift operation on.
step : `int` - The number of bits to shift by.
Returns: `int` - The result of the bitwise right shift operation.
bRotateLeft(n, step)
Performs a bitwise right shift operation on an integer.
Parameters:
n : `int` - The int to perform the bitwise Left rotation on the bits.
step : `int` - The number of bits to shift by.
Returns: `int`- The result of the bitwise right shift operation.
bRotateRight(n, step)
Performs a bitwise right shift operation on an integer.
Parameters:
n : `int` - The int to perform the bitwise Right rotation on the bits.
step : `int` - The number of bits to shift by.
Returns: `int` - The result of the bitwise right shift operation.
bSetCheck(n, pos)
Checks if the bit at the given position is set to 1.
Parameters:
n : `int` - The integer to check.
pos : `int` - The position of the bit to check.
Returns: `bool` - True if the bit is set to 1, False otherwise.
bClear(n, pos)
Clears a particular bit of an integer (changes from 1 to 0) passes if bit at pos is 0.
Parameters:
n : `int` - The integer to clear a bit from.
pos : `int` - The zero-based index of the bit to clear.
Returns: `int` - The result of clearing the specified bit.
bFlip0s(n)
Flips all 0 bits in the number to 1.
Parameters:
n : `int` - The integer to flip the bits of.
Returns: `int` - The result of flipping all 0 bits in the number.
bFlip1s(n)
Flips all 1 bits in the number to 0.
Parameters:
n : `int` - The integer to flip the bits of.
Returns: `int` - The result of flipping all 1 bits in the number.
bFlipAll(n)
Flips all bits in the number.
Parameters:
n : `int` - The integer to flip the bits of.
Returns: `int` - The result of flipping all bits in the number.
bSet(n, pos, newBit)
Changes the value of the bit at the given position.
Parameters:
n : `int` - The integer to modify.
pos : `int` - The position of the bit to change.
newBit : `int` - na = flips bit at pos reguardless 1 or 0 | The new value of the bit (0 or 1).
Returns: `int` - The modified integer.
changeDigit(n, pos, newDigit)
Changes the value of the digit at the given position.
Parameters:
n : `int` - The integer to modify.
pos : `int` - The position of the digit to change.
newDigit : `int` - The new value of the digit (0-9).
Returns: `int` - The modified integer.
bSwap(n, i, j)
Switch the position of 2 bits of an int
Parameters:
n : `int` - int to manipulate
i : `int` - bit pos to switch with j
j : `int` - bit pos to switch with i
Returns: `int` - new int with bits switched
bPalindrome(n)
Checks to see if the binary form is a Palindrome (reads the same left to right and vice versa)
Parameters:
n : `int` - int to check
Returns: `bool` - result of check
bEven(n)
Checks if n is Even
Parameters:
n : `int` - The integer to check.
Returns: `bool` - result.
bOdd(n)
checks if n is Even if not even Odd
Parameters:
n : `int` - The integer to check.
Returns: `bool` - result.
bPowerOfTwo(n)
Checks if n is a Power of 2.
Parameters:
n : `int` - number to check.
Returns: `bool` - result.
bCount(n, to_count)
Counts the number of bits that are equal to 1 in an integer.
Parameters:
n : `int` - The integer to count the bits in.
to_count `string` - the bits to count
Returns: `int` - The number of bits that are equal to 1 in n.
GCD(a, b)
Finds the greatest common divisor (GCD) of two numbers.
Parameters:
a : `int` - The first number.
b : `int` - The second number.
Returns: `int` - The GCD of a and b.
LCM(a, b)
Finds the least common multiple (LCM) of two integers.
Parameters:
a : `int` - The first integer.
b : `int` - The second integer.
Returns: `int` - The LCM of a and b.
aLCM(nums)
Finds the LCM of an array of integers.
Parameters:
nums : `int ` - The list of integers.
Returns: `int` - The LCM of the integers in nums.
adjustedLCM(nums, LCM)
adjust an array of integers to Least Common Multiple (LCM)
Parameters:
nums : `int ` - The first integer
LCM : `int` - The second integer
Returns: `int ` - array of ints with LCM
charAt(str, pos)
gets a Char at a given position.
Parameters:
str : `string` - string to pull char from.
pos : `int` - pos to get char from string (left to right index).
Returns: `string` - char from pos of string or "" if pos is not within index range
decimalToBinary(num)
Converts a decimal number to binary
Parameters:
num : `int` - The decimal number to convert to binary
Returns: `string` - The binary representation of the decimal number
decimalToBinary(num, to_binary_int)
Converts a decimal number to binary
Parameters:
num : `int` - The decimal number to convert to binary
to_binary_int : `bool` - bool to convert to int or to string (true for int, false for string)
Returns: `string` - The binary representation of the decimal number
binaryToDecimal(binary)
Converts a binary number to decimal
Parameters:
binary : `string` - The binary number to convert to decimal
Returns: `int` - The decimal representation of the binary number
decimal_len(n)
way of finding decimal length using arithmetic
Parameters:
n `float` - floating decimal point to get length of.
Returns: `int` - number of decimal places
int_len(n)
way of finding number length using arithmetic
Parameters:
n : `int`- value to find length of number
Returns: `int` - lenth of nunber i.e. 23 == 2
float_decimal_to_whole(n)
Converts a float decimal number to an integer `0.365 to 365`.
Parameters:
n : `string` - The decimal number represented as a string.
Returns: `int` - The integer obtained by removing the decimal point and leading zeroes from s.
fractional_part(x)
Returns the fractional part of a float.
Parameters:
x : `float` - The float to get the fractional part of.
Returns: `float` - The fractional part of the float.
form_decimal(a, b, zero_fix)
helper to form 2 ints into 1 float seperated by the decimal
Parameters:
a : `int` - a int
b : `int` - b int
zero_fix : `bool` - fix for trailing zeros being truncated when converting to float
Returns: ` ` - float = float decimal of ints | string = string version of b for future use to ref length
bEncode(n1, n2)
Encodes two numbers into one using bit OR. (fastest)
Parameters:
n1 : `int` - The first number to Encodes.
n2 : `int` - The second number to Encodes.
Returns: `int` - The result of combining the two numbers using bit OR.
bDecode(n)
Decodes an integer created by the bCombine function.(fastest)
Parameters:
n : `int` - The integer to decode.
Returns: ` ` - A tuple containing the two decoded components of the integer.
Encode(a, b)
Encodes by seperating ints into left and right of decimal float
Parameters:
a : `int` - a int
b : `int` - b int
Returns: `float` - new float of encoded ints one on left of decimal point one on right
Decode(encoded)
Decodes float of 2 ints seperated by decimal point
Parameters:
encoded : `float` - the encoded float value
Returns: ` ` - tuple of the 2 ints from encoded float
encode_heavy(a, b)
Encodes by combining numbers and tracking size in the
decimal of a floating number (slowest)
Parameters:
a : `int` - a int
b : `int` - b int
Returns: `float` - new decimal of encoded ints
decode_heavy(encoded)
Decodes encoded float that tracks size of ints in float decimal
Parameters:
encoded : `float` - encoded float
Returns: ` ` - tuple of decoded ints
decimal of float (slowest)
Parameters:
encoded : `float` - the encoded float value
Returns: ` ` - tuple of the 2 ints from encoded float
Bitwise, Encode, Decode Docs
In the documentation you may notice the word decimal
not used as normal this is because when referring to
binary a decimal number is a number that
can be represented with base 10 numbers 0-9
(the wiki below explains better)
A rule of thumb for the two integers being
encoded it to keep both numbers
less than 65535 this is because anything lower uses 16 bits or less
this will maintain 100% accuracy when decoding
although it is possible to do numbers up to 2147483645 with
this library doesnt seem useful enough
to explain or demonstrate.
The functions provided work within this 32-bit range,
where the highest number is all 1s and
the lowest number is all 0s. These functions were created
to overcome the lack of built-in bitwise functions in Pinescript.
By combining two integers into a single number,
the code can access both values i.e when
indexing only one array index
for a matrices row/column, thus improving execution time.
This technique can be applied to various coding
scenarios to enhance performance.
Bitwise functions are a way to use integers in binary form
that can be used to speed up several different processes
most languages have operators to perform these function such as
`<<, >>, &, ^, |, ~`
en.wikipedia.org