# Add bit\_len() function

**URL:** <https://internals.rust-lang.org/t/add-bit-len-function/23031>\
**Category:** internals\
**Created:** [June 2, 2025, 7:43pm UTC](https://internals.rust-lang.org/t/add-bit-len-function/23031 "2025-06-02T19:43:18Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jrose/32/9591_2.png) [@jrose](https://internals.rust-lang.org/u/jrose)\
**Post date:** [June 3, 2025, 12:38am UTC](https://internals.rust-lang.org/t/add-bit-len-function/23031/4 "2025-06-03T00:38:30Z")

</div>

Previously:

> [@Add methods that return the number of bits necessary to represent an integer in binary to the standard library](https://internals.rust-lang.org/t/add-methods-that-return-the-number-of-bits-necessary-to-represent-an-integer-in-binary-to-the-standard-library/21870):
>
> Hi internals, Currently, the number of bits necessary to represent an integer in binary to can be obtained by: For unsigned integers: let answer: u16 = 42; // 0b10\_1010 assert\_eq!(answer.checked\_ilog2().map\_or(0, |n| n + 1), 6); For signed integers (excluding the sign bit and leading zeros): let n: i16 = -37; // -0b10\_0101 assert\_eq!(n.unsigned\_abs().checked\_ilog2().map\_or(0, |n| n + 1), 6); Methods for this purpose appear to exist in the standard library of several languages: [int.bit\_le…](https://docs.python.org/3.13/library/stdtypes.html#int.bit_length)

---

_[View the full topic](https://internals.rust-lang.org/t/add-bit-len-function/23031)._
