Whats a computationally fast way of calculating binary?

so i am writing a genetic algorithm ai and part of it needs to be able to run a function a lot, so what i am trying to emphasize is this function needs to be extremely efficient. And I don’t think this yandere dev code will help. I just don’t understand how else I could. I tried finding some formula online for converting binary to text but all I found was this video

local function computeFitness(chromosome)
	local chromosomeArray = string.split(chromosome, "")
	local bitValue = 0
	for i= #chromosomeArray, 1, -1 do --Iterate from the end of the byte
		if i == 1 then
			bitValue += 64 * chromosomeArray[i]
		elseif i == 2 then
			bitValue += 32 * chromosomeArray[i]
		elseif i == 3 then
			bitValue += 16 * chromosomeArray[i]
		elseif i == 4 then
			bitValue += 8 * chromosomeArray[i]
		elseif i == 5 then
			bitValue += 4 * chromosomeArray[i]
		elseif i == 6 then
			bitValue += 2 * chromosomeArray[i]
		elseif i == 7 then
			bitValue += 1 * chromosomeArray[i]
		end
	end
	local text = utf8.char(bitValue)
	return text
end
1 Like

EDIT: nvm im dumb the formula was in the video. Here is my abstraction of it

local function computeFitness(chromosome)
	local chromosomeArray = string.split(chromosome, "")
	local bitValue = 0
	for i= #chromosomeArray, 1, -1 do --Iterate from the end of the byte
		bitValue += 2^(i-1) * chromosomeArray[i]
	end
	local text = utf8.char(bitValue)
	return text
end

Have you tried tonumber(chromosome, 2)?

If Roblox hasn’t modified it, it will only work up to 32-bits (out of the 53 you can exactly represent assuming Roblox is using 64-bit Lua numbers), but it should be faster than anything you can reasonably do in Lua alone.

whoops, forgot about that lol. thanks

Do also make note of the fact that Roblox has the bit32 library built-in.
It might be faster than string manipulation if you’re really trying to crank the performance up to wumbo.

3 Likes