numpy.binary_repr(number,width = None)函數用於將輸入數字的二進製形式表示為字符串。
對於負數,如果未指定寬度,則會在前麵添加減號。如果給出寬度,則返回該寬度的數字的補碼。
在二進製補碼係統中,負數由絕對值的二進製補碼表示。這是在計算機上表示有符號整數的最常用方法。
用法: numpy.binary_repr(number, width=None)
參數:
number :輸入號碼。隻能使用整數十進製數作為輸入。
width :[int,可選]如果number為正數,則返回字符串的長度;如果number為負數,則為兩個補數的長度,但前提是寬度至少要足以以指定的形式表示number的位數。
如果寬度值不足,它將被忽略,並且數字將以二進製(數字> 0)或兩個補碼(數字
Return :輸入數字的二進製字符串表示形式。
代碼1:工作
# Python program explaining
# binary_repr() function
import numpy as geek
in_num = 10
print ("Input number:", in_num)
out_num = geek.binary_repr(in_num)
print ("binary representation of 10:", out_num)
輸出:
Input number: 10 binary representation of 10: 1010
代碼2:
# Python program explaining
# binary_repr() function
import numpy as geek
in_arr = [5, -8 ]
print ("Input array:", in_arr)
# binary representation of first array
# element without using width parameter
out_num = geek.binary_repr(in_arr[0])
print("Binary representation of 5")
print ("Without using width parameter:", out_num)
# binary representation of first array
# element using width parameter
out_num = geek.binary_repr(in_arr[0], width = 5)
print ("Using width parameter:", out_num)
print("\nBinary representation of -8")
# binary representation of 2nd array
# element without using width parameter
out_num = geek.binary_repr(in_arr[1])
print ("Without using width parameter:", out_num)
# binary representation of 2nd array
# element using width parameter
out_num = geek.binary_repr(in_arr[1], width = 5)
print ("Using width parameter:", out_num)
輸出:
Input array: [5, -8] Binary representation of 5 Without using width parameter: 101 Using width parameter: 00101 Binary representation of -8 Without using width parameter: -1000 Using width parameter: 11000
相關用法
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 numpy.binary_repr() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。