# Binary

Library "Binary"
This library includes functions to convert between decimal and binary numeral formats, and logical and arithmetic operations on binary numbers.

method toBin(value)
Converts the provided boolean value into binary integers (0 or 1).
Namespace types: series bool, simple bool, input bool, const bool
Parameters:
value (bool): The boolean value to be converted.
Returns: The converted value in binary integers.

method dec2bin(value, iBits, fBits)
Converts a decimal number into its binary representation.
Namespace types: series float, simple float, input float, const float
Parameters:
value (float): The decimal number to be converted.
iBits (int): The number of binary digits allocated for the integer part.
fBits (int): The number of binary digits allocated for the fractional part.
Returns: An array containing the binary digits for the integer part at the rightmost positions and the digits for the fractional part at the leftmost positions. The array indexes correspond to the bit positions.

method bin2dec(value, iBits, fBits)
Converts a binary number into its decimal representation.
Namespace types: array<int>
Parameters:
value (array<int>): The binary number to be converted.
iBits (int): The number of binary digits allocated for the integer part.
fBits (int): The number of binary digits allocated for the fractional part.
Returns: The converted value in decimal format.

method lgcAnd(a, b)
Bitwise logical AND of two binary numbers. The result of ANDing two binary digits is 1 only if both digits are 1, otherwise, 0.
Namespace types: array<int>
Parameters:
a (array<int>): First binary number.
b (array<int>): Second binary number.
Returns: An array containing the logical AND of the inputs.

method lgcOr(a, b)
Bitwise logical OR of two binary numbers. The result of ORing two binary digits is 0 only if both digits are 0, otherwise, 1.
Namespace types: array<int>
Parameters:
a (array<int>): First binary number.
b (array<int>): Second binary number.
Returns: An array containing the logical OR of the inputs.

method lgcXor(a, b)
Bitwise logical XOR of two binary numbers. The result of XORing two binary digits is 1 only if ONE of the digits is 1, otherwise, 0.
Namespace types: array<int>
Parameters:
a (array<int>): First binary number.
b (array<int>): Second binary number.
Returns: An array containing the logical XOR of the inputs.

method lgcNand(a, b)
Bitwise logical NAND of two binary numbers. The result of NANDing two binary digits is 0 only if both digits are 1, otherwise, 1.
Namespace types: array<int>
Parameters:
a (array<int>): First binary number.
b (array<int>): Second binary number.
Returns: An array containing the logical NAND of the inputs.

method lgcNor(a, b)
Bitwise logical NOR of two binary numbers. The result of NORing two binary digits is 1 only if both digits are 0, otherwise, 0.
Namespace types: array<int>
Parameters:
a (array<int>): First binary number.
b (array<int>): Second binary number.
Returns: An array containing the logical NOR of the inputs.

method lgcNot(a)
Bitwise logical NOT of a binary number. The result of NOTing a binary digit is 0 if the digit is 1, or vice versa.
Namespace types: array<int>
Parameters:
a (array<int>): A binary number.
Returns: An array containing the logical NOT of the input.

method lgc2sC(a)
2's complement of a binary number. The 2's complement of a binary number N with n digits is defined as 2^(n) - N.
Namespace types: array<int>
Parameters:
a (array<int>): A binary number.
Returns: An array containing the 2's complement of the input.

method shift(value, direction, newBit)
Shifts a binary number in the specified direction by one position.
Namespace types: array<int>
Parameters:
value (array<int>)
direction (int): The direction of the shift operation.
newBit (int): The bit to be inserted into the unoccupied slot.
Returns: A tuple of the shifted binary number and the serial output of the shift operation.

method multiShift(value, direction, newBits)
Shifts a binary number in the specified direction by multiple positions.
Namespace types: array<int>
Parameters:
value (array<int>)
direction (int): The direction of the shift operation.
newBits (array<int>)
Returns: A tuple of the shifted binary number and the serial output of the shift operation.

method crclrShift(value, direction, count)
Circularly shifts a binary number in the specified direction by multiple positions. Each ejected bit is inserted from the opposite side.
Namespace types: array<int>
Parameters:
value (array<int>)
direction (int): The direction of the shift operation.
count (int): The number of positions to be shifted by.
Returns: The shifted binary number.

method arithmeticShift(value, direction, count)
Performs arithmetic shift on a binary number in the specified direction by multiple positions. Every new bit is 0 if the shift is leftward, otherwise, it equals the sign bit.
Namespace types: array<int>
Parameters:
value (array<int>)
direction (int): The direction of the shift operation.
count (int): The number of positions to be shifted by.
Returns: The shifted binary number.

Performs arithmetic addition on two binary numbers.
Namespace types: array<int>
Parameters:
a (array<int>): First binary number.
b (array<int>): Second binary number.
carry (int): The input carry of the operation.
Returns: The result of the arithmetic addition of the inputs.

method sub(a, b, carry)
Performs arithmetic subtraction on two binary numbers.
Namespace types: array<int>
Parameters:
a (array<int>): First binary number.
b (array<int>): Second binary number. The number to be subtracted.
carry (int): The input carry of the operation.
Returns: The result of the arithmetic subtraction of the input b from the input a.
Pine library

In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in a publication is governed by House Rules.

Disclaimer