Skip to content

Instantly share code, notes, and snippets.

@andrew-wilkes
Created August 9, 2021 11:22
Show Gist options
  • Save andrew-wilkes/f414a25e7c827ca1b04b7928596aba3c to your computer and use it in GitHub Desktop.
Save andrew-wilkes/f414a25e7c827ca1b04b7928596aba3c to your computer and use it in GitHub Desktop.
Count number of bits in a number
extends Node2D
"""
See: https://www.geeksforgeeks.org/count-total-bits-number/
Given a positive number n, count total bit in it.
Input : 183
Output : 8
Input : 4096
Output : 13
"""
func _ready():
var n = 4096
var i = 1
var num_bits = 0
while i <= n:
i *= 2
num_bits += 1
print(num_bits)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment