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:
if n==0 or n==1: