Denary to Binary Conversion |
As far as the computer science students at GCSE and A Level are concerned, it is a vital concept to be mastered.
The process of converting from denary to binary involves repeatedly dividing the denary number by 2 and observing the remainders. The remainders represent the digits of the binary number, with a 0 indicating a 0 digit and a 1 indicating a 1 digit. The quotients are then used to perform further divisions, continuing the process until the quotient reaches 0.
Here's a step-by-step explanation of the conversion process:
Start with the denary number.
Divide the denary number by 2.
Note the remainder. This will be the least significant bit (LSB) of the binary number.
If the quotient is not 0, divide it by 2 again.
Repeat steps 3 and 4 until the quotient reaches 0.
The remaining remainders, in order from least significant to most significant, form the binary representation of the denary number.
For example, converting the denary number 15 to binary would result in the following steps:
15 ÷ 2 = 7 R 1
7 ÷ 2 = 3 R 1
3 ÷ 2 = 1 R 1
1 ÷ 2 = 0 R 1
Stop.
Therefore, the binary representation of 15 is 1111 (four bits in total, with the MSB being the leftmost digit).
A Python function for denary to binary conversion:
def denaryToBinary(): nmbr=int(input("Enter the number: ")) number=nmbr string="" while number>0: string=str(number%2)+string if number>0: number=number//2 return "The number, " + str(nmbr) + ", in binary form = " + string print(denaryToBinary())
You can use the above function as follows for interactive practice, by pressing the 'Play' button:
Nice
ReplyDelete