Bit Length of Big Integer
Python
Medium
2 views
Problem Description
Input has one integer n is provided (can be very large). Output how many bits are needed to represent |n| in binary. For n=0 output 1.
Input Format
One integer n.
Output Format
One integer bitLength.
Official Solution
import sys
s=sys.stdin.read().strip()
if not s: sys.exit(0)
n=int(s)
x=abs(n)
if x==0:
sys.stdout.write('1')
else:
sys.stdout.write(str(x.bit_length()))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!