BIN Python Function
BIN Python Function

Hàm bin() giúp thể hiện một số nguyên dưới dạng nhị phân. Hàm có một tham số là số nguyên. Kết quả trả về của hàm là string với tiền tố là 0b trước kết quả nhị phân trả về.

#  Integer 15 thể hiện ở dạng decimal (thập phân)
decimal = 15
#  Integer 15 thể hiện ở dạng hexadecimal (thập lục phân)
hexadecimal = 0xf
#  Integer 15 thể hiện ở dạng octal (bát phân)
octal = 0o17

print("Binary representation", bin(decimal)) # Kết quả sẽ là :0b1111
print("Binary representation", bin(hexadecimal)) # Kết quả sẽ là :0b1111
print("Binary representation", bin(octal)) # Kết quả sẽ là :0b1111

# Hãy thử với x = bin(36), kết quả in ra là gì?
Last modified: September 19, 2023

Author

Comments

Write a Reply or Comment

Your email address will not be published.