> ## Content Index
> Fetch the complete content index at: https://www.javahandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Find the Bit Length of a Number
- URL: https://www.javahandbook.com/bitmask/find-the-bit-length-of-a-number/
- Published: 2025-08-05T22:00:00.000Z
- Updated: 2025-08-07T04:41:43.000Z
- Author: Gopi Gorantala
- Tags: #bitmask, #docs, Left Shift Problems

In this lesson, we use the Left Shift operator to find the bit length of a number.

## Introduction

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

## Problem Statement

Given an input, find its bit length.

```json
Input: 8

Output: 4 (1000)
```

```json
Input: 2

Output: 2 (10)
```

```json
Input: 7

Output: 3 (111)
```

## Algorithm

We already discussed the formula of the left shift operator.

> a << b = (a \* 2b)

**Steps:**

- Initialize a variable `bitsCounter` with value `0`.
- Now, left-shift `bitsCounter` until its value is less or equal to the given number.
  - if `true`, increment the `bitsCounter` on each iteration.
  - else, return `bitsCounter`.

## Solution

### Java

```java
class BitLength {
    static int bitsLength(int number) {
        int bitsCounter = 0;

        while ((1 << bitsCounter) <= number) {
            bitsCounter += 1;
        }
        return bitsCounter;
    }

    public static void main(String[] args) {
        System.out.println(bitsLength(8));
        System.out.println(bitsLength(2));
        System.out.println(bitsLength(7));
    }
}
```

### Python

```py
def bitsLength(n):
  bitsCounter = 0

  while((1 << bitsCounter) <= n):
    bitsCounter += 1
  
  return bitsCounter

print(bitsLength(8))
print(bitsLength(2))
print(bitsLength(7))

```

### JavaScript

```js
/**
 * Return the number of bits used in the binary representation of the number.
 *
 * @param {number} number
 * @return {number}
 */
const bitLength = (number) => {
    let bitsCounter = 0;

    while ((1 << bitsCounter) <= number) {
        bitsCounter += 1;
    }

    return bitsCounter;
}

console.log(bitLength(8));
console.log(bitLength(2));
console.log(bitLength(7));

```

### C++

```cpp
#include <iostream>
using namespace std;

int bitsLength(int n){
  int bitsCounter = 0;

  while((1 << bitsCounter) <= n){
    bitsCounter += 1;
  }

  return bitsCounter;
}

int main() {
  cout << bitsLength(8) << "\n";
  cout << bitsLength(2) << "\n";
  cout << bitsLength(7);
  return 0;
}
```

### TypeScript

```ts
/**
 * Return the number of bits used in the binary representation of the number.
 *
 * @param {number} number
 * @return {number}
 */
export const bitLength = (number: number): number => {
    let bitsCounter: number = 0;

    while ((1 << bitsCounter) <= number) {
        bitsCounter += 1;
    }

    return bitsCounter;
}

console.log(bitLength(8));
console.log(bitLength(2));
console.log(bitLength(7));

```

## Complexity analysis

### Time Complexity

We are running a loop, which continues until and unless the loop breaks. The inputs never change. overall, its `*O*(*n*)`.

### Space Complexity

We didn’t create an extra memory for this. So, the space complexity is `*O*(1)`.