BinaryDecimalConversion
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5

// @description Converts decimal to and from binary.
library(title='BinaryDecimalConversion')

// to_binary () {

// @function convert integer to binary string
// @param number int, value to convert.
// @returns string
export to_binary(int number) =>
    string _str = ''
    int _num = number
    while _num >= 1
        int _rem = _num % 2
        _str := (_rem == 0 ? '0' : '1') + _str
        _num := (_num - _rem) / 2
    _str

// usage:
// st = to_binary(3232)
// if barstate.islastconfirmedhistory
//     label.new(bar_index, 0.0, st) // 110010100000

// }
// to_decimal () {

// @function Converts a binary in a string to decimal.
// @param binary string, binary number in a string.
// @returns int
export to_decimal (string binary) => //{
    int _val = 0
    int _b = int(str.tonumber(binary))
    int i = 0
    while _b > 0
        int _rem = _b % 10
        _val += int(_rem * math.pow(2, i))
        _b := (_b - _rem) / 10
        i += 1
    _val

// usage:
// plot(to_decimal('110010100000')) // 3232

// }
// leftshift_logical () {
// reference:
// https://stackoverflow.com/questions/5832982/how-to-get-the-logical-right-binary-shift-in-python

// @function Shift the bits of an integer to the left.
// @param x int . Value to shift.
// @param n int . Number of zeros to append to x.
// @returns int.
export leftshift_logical(int x, int n=1) =>
    int _n = math.max(1, n)
    array<string> _bin = str.split(to_binary(x), '')
    for _i = 1 to _n
        array.shift(_bin)
        array.push(_bin, '0')
    to_decimal(_bin.join(''))

// lls = leftshift_logical(10,2) 
// plot(lls) // 8

// }
// rightshift_logical () {
// reference:
// https://stackoverflow.com/questions/5832982/how-to-get-the-logical-right-binary-shift-in-python

// @function Shift the bits of an integer to the right.
// @param x int . Value to shift.
// @param n int . Number of bits to remove at the right of x.
// @returns int.
export rightshift_logical (int x, int n=1) =>
    int _n = math.max(1, n)
    array<string> _bin = str.split(to_binary(x), '')
    for _i = 1 to _n
        _bin.pop()
        _bin.unshift('0')
    to_decimal(_bin.join(''))

// rls = rightshift_logical(10,2) 
// plot(rls) // 2

// }
// leftshift_arithmetic () {
// reference:
// https://stackoverflow.com/questions/5832982/how-to-get-the-logical-right-binary-shift-in-python

// @function Shift the bits of an integer to the left.
// @param x int . Value to shift.
// @param n int . Number of zeros to append to x.
// @returns int.
export leftshift_arithmetic (int x, int n=1) =>
    leftshift_logical(x, n)

// las = leftshift_arithmetic(10,2)
// plot(las) // 8

// }
// rightshift_arithmetic () {
// reference:
// https://stackoverflow.com/questions/5832982/how-to-get-the-logical-right-binary-shift-in-python

// @function Shift the bits of an integer to the right.
// @param x int . Value to shift.
// @param n int . Number of bits to remove at the right of x.
// @returns int.
export rightshift_arithmetic (int x, int n=1) =>
    int _n = math.max(1, n)
    array<string> _bin = str.split(to_binary(x), '')
    for _i = 1 to _n
        string _tmp = _bin.get(0)
        _bin.pop()
        _bin.unshift(_tmp)
    to_decimal(_bin.join(''))

// ras = rightshift_arithmetic(10,2)
// plot(ras) // 14

// }