写一个python程序来计算一个数字的总位数?
首先,我们输入一个数字,然后使用bin()函数将此数字转换为二进制,然后删除输出字符串的前两个字符“0b”,然后计算二进制字符串的长度。
示例
Input:200 Output:8
说明
Binary representation of 200 is 10010000
算法
Step 1: input number. Step 2: convert number into its binary using bin() function. Step 3: remove first two characters ‘0b’ of output binary string because bin function appends ‘ob’ a prefix in output string. Step 4: then calculate the length of the binary string.
范例程式码
# Python program to count total bits in a number
def totalbits(n):
binumber = bin(n)[2:]
print("TOTAL BITS ::>",len(binumber))
# Driver program
if __name__ == "__main__":
n=int(input("Enter Number ::>"))
totalbits(n)输出结果
Enter Number ::>200 TOTAL BITS ::> 8
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短