PINE LIBRARY

Bitwise, Encode, Decode

已更新
Library "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` - ["all","0","1"] 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, string]` - 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: `[int, int]` - 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: `[int,int]` - 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: `[int, int]` - tuple of decoded ints

decimal of float (slowest)
  Parameters:
    encoded: `float` - the encoded float value
  Returns: `[int,int]` - 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
    `<<, >>, &, ^, |, ~`


[url=en.wikipedia.org/wiki/Bitwise_operation ]en.wikipedia.org/wiki/Bitwise_operation
發布通知
=D


發布通知
v3
發布通知
v4
發布通知
v5

Minor fix to the docs there was some typos
發布通知
v6

updated bShiftLeft()
added error handling
arraysbinarybitwiseencodingffrizzfrizlabzperformancestatisticstechindicator

Pine腳本庫

在真正的TradingView精神中,作者將這段Pine程式碼發佈為開源程式庫,以便我們社群的其他Pine程式設計師可以重複使用它。請向作者致敬!您可以私下使用這個函式庫,或在其他開源出版品中使用,但在出版物中再次使用這段程式碼將受到網站規則的約束。


更多:

免責聲明