+ 1
This code to change a value from 4 bit to 8 bit and code not working
m=0b101 #number on binary for define it do 0b print (m) s=0xa4 #number on hexadecimal for define it do 0x print(s) print(fun(0b110)) def fun(n): print("{:0b8}".format(n)) print(fun(0b110))
2 ответов
+ 4
def fun(n): #defining function
print("{0:08b}".format(n))
m=0b101 #number on binary for define it do 0b
print (m)
s=0xa4 #number on hexadecimal for define it do 0x
print(s)
fun(0b110) #calling
fun(m)
fun(s)
#first define function then use it..
+ 8
Berba Malek ,
instead of using ...format(...), we can use string interpolation with f-string like this:
print(f'{n:08b}') # for printing
var = f'{n:08b}' # creates a string and store it in a variable
> n -> is the name of the variable that should be processed
> 0 -> means that the output is padded with zeros on the left side
> 8 -> defines the total length of the output string
> b -> defines number system for output, in this case it is *b*inary
> {} -> are required around of python expressions