0

Swap 2 Nibbles in a byte & find the output of the swapped Nibbles after converting a Decimal Number to a Binary Number.

Given a byte, swap the two nibbles in it. For example 100 is to be represented as 01100100 in a byte (or 8 bits). The two nibbles are (0110) and (0100). If we swap the two nibbles, we get 01000110 which is 70 in decimal. But, for smaller numbers that are less than 8 bits, need to be added extra zeros in the front. I am strucked with how to push extra zeros after splitting the obtained binary number & swap the two numbers. I am getting the binary output, but the splitting is not working. Anyone please help? Here's my code: def dec(n): if n==0 or n==1: print(n,end="") return dec(int(n/2)), dec(n%2) n1= str(dec(10)) st=n1.split() if len(st)<8: st.format(0,8) s=st[4:8]+s[0:4] print(s)

23rd Jan 2020, 3:22 PM
Prithvi
2 Réponses
0
Thank you Alexandr. Could you please answer without any built-in functions
23rd Jan 2020, 3:39 PM
Prithvi
0
Wow. This was so quick.
23rd Jan 2020, 4:23 PM
Prithvi