Skip to content

Instantly share code, notes, and snippets.

@andrew-wilkes
Last active May 26, 2021 12:01
Show Gist options
  • Select an option

  • Save andrew-wilkes/63804dc15c46c954f0f993aad432749f to your computer and use it in GitHub Desktop.

Select an option

Save andrew-wilkes/63804dc15c46c954f0f993aad432749f to your computer and use it in GitHub Desktop.
Convert an Integer to a Binary String
var num_bits_index = 0
var bit_lengths = [4, 8, 16]
func int2binary(x: int) -> String:
var b = ""
for n in bit_lengths[num_bits_index]:
b = String(x % 2) + b
x /= 2
return b
@andrew-wilkes

Copy link
Copy Markdown
Author

To ensure that the index does not go out of bounds you may enter: for n in bit_lengths[num_bits_index % bit_lengths.size()]:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment