Search⌘ K

Find Bit Length of a Number

Explore how to determine the bit length of a given number by applying bitwise left-shift techniques. Understand the algorithmic steps involved and analyze the time and space complexity. This lesson helps improve coding interview preparation by mastering essential bit manipulation concepts.

Introduction

In this question, we take input and find its bit length.

Problem Statement

Given an input find its bit length.

Input: 8

Output: 4 (1000)
Input: 2

Output: 2 (10)
Input: 7

Output: 3 (111)

Algorithm

We already discussed the formula of the left shift operator.

a << b = ( a * 2b ...