def bitslice(arg, length = (missing_length = true))
      if missing_length
        if arg.is_a?(Range)
          from, to = arg.begin, arg.end
          from += bitsize if from < 0
          return nil if from < 0
          to   += bitsize if to < 0
          to   += 1       if !arg.exclude_end?
          length = to - from
          length = 0 if length < 0
          length = bitsize - from if (from + length) > bitsize
          return nil if length < 0
          l8 = length.div(8)
          l1 = length % 8
          pad = 8 - l1
          enum = each_bit
          skip = from
          enum = enum.drop_while {
            if skip > 0
              skip -= 1
              next true
            else
              next false
            end
          }
          head = enum.take(length)
          if l1 == 0
            return Erlang::Binary[[head.join].pack(BIT_PACK)]
          else
            tail = head[-l1..-1]
            head = head[0...-l1]
            tail = ([0] * pad).concat(tail)
            return Erlang::Bitstring[[[head.join, tail.join].join].pack(BIT_PACK), bits: l1]
          end
        else
          arg += bitsize if arg < 0
          return nil if arg < 0
          return nil if arg >= bitsize
          a8 = arg.div(8)
          a1 = arg % 8
          byte = @data.getbyte(a8)
          return nil if byte.nil?
          return (byte >> ((@bits - a1 - 1) & 7)) & 1
        end
      else
        return nil if length < 0
        arg += bitsize if arg < 0
        return nil if arg < 0
        length = bitsize - arg if (arg + length) > bitsize
        return nil if length < 0
        l8 = length.div(8)
        l1 = length % 8
        pad = 8 - l1
        enum = each_bit
        skip = arg
        enum = enum.drop_while {
          if skip > 0
            skip -= 1
            next true
          else
            next false
          end
        }
        head = enum.take(length)
        if l1 == 0
          return Erlang::Binary[[head.join].pack(BIT_PACK)]
        else
          tail = head[-l1..-1]
          head = head[0...-l1]
          tail = ([0] * pad).concat(tail)
          return Erlang::Bitstring[[[head.join, tail.join].join].pack(BIT_PACK), bits: l1]
        end
      end
    end
def bitslice(arg, length = (missing_length = true))
      if missing_length
        if arg.is_a?(Range)
          from, to = arg.begin, arg.end
          from += bitsize if from < 0
          return nil if from < 0
          to   += bitsize if to < 0
          to   += 1       if !arg.exclude_end?
          length = to - from
          length = 0 if length < 0
          length = bitsize - from if (from + length) > bitsize
          return nil if length < 0
          l8 = length.div(8)
          l1 = length % 8
          pad = 8 - l1
          enum = each_bit
          skip = from
          enum = enum.drop_while {
            if skip > 0
              skip -= 1
              next true
            else
              next false
            end
          }
          head = enum.take(length)
          if l1 == 0
            return Erlang::Binary[[head.join].pack(BIT_PACK)]
          else
            tail = head[-l1..-1]
            head = head[0...-l1]
            tail = ([0] * pad).concat(tail)
            return Erlang::Bitstring[[[head.join, tail.join].join].pack(BIT_PACK), bits: l1]
          end
        else
          arg += bitsize if arg < 0
          return nil if arg < 0
          return nil if arg >= bitsize
          a8 = arg.div(8)
          a1 = arg % 8
          byte = @data.getbyte(a8)
          return nil if byte.nil?
          return (byte >> ((@bits - a1 - 1) & 7)) & 1
        end
      else
        return nil if length < 0
        arg += bitsize if arg < 0
        return nil if arg < 0
        length = bitsize - arg if (arg + length) > bitsize
        return nil if length < 0
        l8 = length.div(8)
        l1 = length % 8
        pad = 8 - l1
        enum = each_bit
        skip = arg
        enum = enum.drop_while {
          if skip > 0
            skip -= 1
            next true
          else
            next false
          end
        }
        head = enum.take(length)
        if l1 == 0
          return Erlang::Binary[[head.join].pack(BIT_PACK)]
        else
          tail = head[-l1..-1]
          head = head[0...-l1]
          tail = ([0] * pad).concat(tail)
          return Erlang::Bitstring[[[head.join, tail.join].join].pack(BIT_PACK), bits: l1]
        end
      end
    end
Return specific objects from the `Bitstring`. All overloads return `nil` if
 the starting index is out of range.

 @overload bitslice(index)
   Returns a single bit at the given `index`. If `index` is negative,
   count backwards from the end.

   @param index [Integer] The index to retrieve. May be negative.
   @return [Integer, nil]
   @example
     b = Erlang::Bitstring[2, bits: 2]
     b.bitslice(0)  # => 1
     b.bitslice(1)  # => 0
     b.bitslice(-1) # => 0
     b.bitslice(2)  # => nil

 @overload bitslice(index, length)
   Return a bitstring starting at `index` and continuing for `length`
   bits or until the end of the `Bitstring`, whichever occurs first.

   @param start [Integer] The index to start retrieving bits from. May be
                          negative.
   @param length [Integer] The number of bits to retrieve.
   @return [Bitstring, Binary]
   @example
     b = Erlang::Bitstring[1, 117, bits: 7]
     b.bitslice(0, 11) # => Erlang::Bitstring[1, 7, bits: 3]
     b.bitslice(11, 4) # => Erlang::Bitstring[5, bits: 4]
     b.bitslice(16, 1) # => nil

 @overload bitslice(index..end)
   Return a bitstring starting at `index` and continuing to index
   `end` or the end of the `Bitstring`, whichever occurs first.

   @param range [Range] The range of bits to retrieve.
   @return [Bitstring, Binary]
   @example
     b = Erlang::Bitstring[1, 117, bits: 7]
     b.bitslice(0...11)  # => Erlang::Bitstring[1, 7, bits: 3]
     b.bitslice(11...15) # => Erlang::Bitstring[5, bits: 4]
     b.bitslice(16..-1)  # => nil

 @see Erlang::Binary#bitslice