-5

Is there a python 3 funtion that will return the largest number of N bits?

Example:

>>> print(largest_bitsize(8))
255
>>> print(largest_bitsize(16))
65535
Legorooj
  • 2,646
  • 2
  • 15
  • 35

6 Answers6

8

I don't think there's a builtin one, but it's easy enough to write your own. 2^N is always the smallest number that requires N+1 bits, so (2^N)-1 must be the largest number that requires N bits.

def largest_bitsize(n):
    return 2**n - 1

print(largest_bitsize(8))
#result: 255

print(largest_bitsize(16))
#result: 65535

print(largest_bitsize(64))
#result: 18446744073709551615
Kevin
  • 74,910
  • 12
  • 133
  • 166
6

I also don't think there's a built-in func but you could write it out. Use a bit shift (as opposed to exponents) for even faster performance:

def largest_bitsize(b):
     return (1 << b) - 1
stevestar888
  • 113
  • 7
5

How about this?

def largest_bitsize(n):
    return int('1' * n, 2)

Examples:

>>> int('1'*16, 2)
65535
>>> int('1'*64, 2)
18446744073709551615
rdas
  • 20,604
  • 6
  • 33
  • 46
5

@Kevin's answer costs O(log n) in time complexity due to the use of the power operator.

A more efficient way to calculate the largest number of n bits would be to use bitwise shift and negation instead, which costs O(1):

def largest_bitsize(n):
    return ~(-1 << n)
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • 1
    This answer is faster than @Kevins answer. Thank you! – Legorooj Jun 14 '19 at 11:30
  • Does Python allow you to shift a negative number like that? Does the language allow it, or does it just happen to work in this instance? (It seems to depend on representations, and that seems non-portable). – jww Jun 14 '19 at 12:42
  • 1
    @jww It's how the binary system for negative numbers works. It's portable everywhere. See demo in [C](https://onlinegdb.com/BkVIprb1B) and [Java](https://repl.it/repls/TrustworthyMuddyLinuxkernel). – blhsing Jun 14 '19 at 16:38
  • Thanks @blhsing - I believe that's [undefined behavior in C](https://stackoverflow.com/q/8415895/608639). In C you *can* shift an `int`, but it must be non-negative value. And of course you can shift an `unsigned`. I don't know about Java. – jww Jun 14 '19 at 16:43
  • 2
    @jww The Python language deliberately makes this possible. The behaviour doesn't come out of the internal representation - in fact, the internal representation in CPython is sign-magnitude, so CPython has to go to extra effort to do the "right thing" and behave as though some form of infinite two's complement were being used. – Mark Dickinson Jun 14 '19 at 18:07
  • 1
    @jww: Relevant documentation: https://docs.python.org/3/library/stdtypes.html#bitwise-operations-on-integer-types. "The result of bitwise operations is calculated as though carried out in two’s complement with an infinite number of sign bits." (BTW, I usually spell this as `~(~0 << n)`, just for the symmetry...) – Mark Dickinson Jun 14 '19 at 18:09
5

Here's a less efficient way to do this:

def largest_bitsize( n ):
   return sum( [ 2 ** i for i in range( n ) ] )
2

I think there is not any BuiltIn is available. But you can try this..

def largest_bitsize(b):
    return (2**b) - 1

Output:-

>>> largest_bitsize(64)

18446744073709551615
Urvi Soni
  • 314
  • 1
  • 2
  • 12