# File lib/dnsruby/resource/NSEC.rb, line 154
      def self.encode_types(nsec)
        output = ''
        # types represents all 65536 possible RR types.
        # Split up types into sets of 256 different types.
        type_codes = []
        nsec.types.each { |type| type_codes << type.code }
        type_codes.sort!
        window = -1
        0.step(65536,256) { |step|
          #  Gather up the RR types for this set of 256
          types_to_go = []
          while (!type_codes.empty? && type_codes[0] < step)
            types_to_go << type_codes[0]
            #  And delete them from type_codes
            type_codes = type_codes.last(type_codes.length - 1)
            break if type_codes.empty?
          end

          unless types_to_go.empty?
            #  Then create the bitmap for them
            bitmap = ''
            #  keep on adding them until there's none left
            pos = 0
            bitmap_pos = 0
            while (!types_to_go.empty?)

              #  Check the next eight
              byte = 0
              pos += 8
              while types_to_go[0] < (pos + step - 256)
                byte = byte | (1 << (pos - 1 - (types_to_go[0] - (step - 256))))
                #  Add it to the list
                #  And remove it from the to_go queue
                types_to_go = types_to_go.last(types_to_go.length - 1)
                break if types_to_go.empty?
              end
              bitmap << ' '
              if bitmap[bitmap_pos].class == String
                bitmap.setbyte(bitmap_pos, byte) # Ruby 1.9
              else
                bitmap[bitmap_pos] = byte
              end
              bitmap_pos += 1
            end

            #  Now add data to output bytes
            start = output.length
            output << (' ' * (2 + bitmap.length))

            if output[start].class == String
              output.setbyte(start, window)
              output.setbyte(start + 1, bitmap.length)
              bitmap.length.times do |i|
                output.setbyte(start + 2 + i, bitmap[i].getbyte(0))
              end
            else
              output[start] = window
              output[start + 1] = bitmap.length
              bitmap.length.times do |i|
                output[start + 2 + i] = bitmap[i]
              end
            end
          end
          window += 1

          #  Are there any more types after this?
          if type_codes.empty?
            #  If not, then break (so we don't add more zeros)
            break
          end
        }
        if output[0].class == String
          output = output.force_encoding("ascii-8bit")
        end
        output
      end