KEMBAR78
5 Exercise Convert Decimal Integer To Binary | PDF | Theoretical Computer Science | Computer Engineering
0% found this document useful (0 votes)
74 views7 pages

5 Exercise Convert Decimal Integer To Binary

This document provides instructions for converting a decimal integer to its binary equivalent using the division by 2 method. It explains that you repeatedly divide the number by 2 and record the remainders from least to most significant bit to build the binary number. It includes an example of converting 242 to its binary representation. Finally, it prompts the reader to code a solution that implements this method using a stack data structure to pass provided tests.

Uploaded by

Kuldeep Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views7 pages

5 Exercise Convert Decimal Integer To Binary

This document provides instructions for converting a decimal integer to its binary equivalent using the division by 2 method. It explains that you repeatedly divide the number by 2 and record the remainders from least to most significant bit to build the binary number. It includes an example of converting 242 to its binary representation. Finally, it prompts the reader to code a solution that implements this method using a stack data structure to pass provided tests.

Uploaded by

Kuldeep Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Exercise: Convert Decimal Integer to Binary

Challenge yourself to solve the problem in this lesson!

WE'LL COVER THE FOLLOWING

• Division by 2 Method
• Coding Time!

In this coding exercise, you are required to use the stack data structure to
convert integer values to their binary equivalent.

Division by 2 Method #
The slides below show how to use the division by 2 method to compute the
binary equivalent for an integer.

242 / 2 = 121 0

Integer Value from the division Remainder

Divide the number by two.


Extract the non-fractional part from the answer
and record the remainder from the division.
1 of 10

242 / 2 = 121 0
121 / 2 = 60 1

Keep dividing the answer from the previous


calculation by two until you reach zero
and keep recording the remainders.

2 of 10

242 / 2 = 121 0
121 / 2 = 60 1
60 / 2 = 30 0
3 of 10

242 / 2 = 121 0
121 / 2 = 60 1
60 / 2 = 30 0
30 / 2 = 15 0

4 of 10

242 / 2 = 121 0
121 / 2 = 60 1
60 / 2 = 30 0
30 / 2 = 15 0
15 / 2 = 7 1
5 of 10

242 / 2 = 121 0
121 / 2 = 60 1
60 / 2 = 30 0
30 / 2 = 15 0
15 / 2 = 7 1
7 / 2 = 3 1

6 of 10

242 / 2 = 121 0
121 / 2 = 60 1
60 / 2 = 30 0
30 / 2 = 15 0
15 / 2 = 7 1
7 / 2 = 3 1
3 / 2 = 1 1
7 of 10

242 / 2 = 121 0
121 / 2 = 60 1
60 / 2 = 30 0
30 / 2 = 15 0
15 / 2 = 7 1
7 / 2 = 3 1
3 / 2 = 1 1
1 / 2 = 0 1

8 of 10

LSB (Least Significant Bit)

242 / 2 = 121 0
121 / 2 = 60 1
60 / 2 = 30 0
30 / 2 = 15 0
15 / 2 = 7 1
7 / 2 = 3 1
3 / 2 = 1 1
1 / 2 = 0 1
MSB (Most Significant Bit)

You have to read from the bottom of the remainders(MSB) to


the top(LSB) to get the binary equivalent of the integer.

9 of 10

LSB (Least Significant Bit)

242 / 2 = 121 0
121 / 2 = 60 1
60 / 2 = 30 0
30 / 2 = 15 0
15 / 2 = 7 1
7 / 2 = 3 1
3 / 2 = 1 1
1 / 2 = 0 1
MSB (Most Significant Bit)
Therefore the binary equivalent for 242 is:
11110010
10 of 10

Coding Time! #
You can build your solution based on division by 2 method. Your solution
should return the correct binary equivalent of dec_num as a string from the
convert_int_to_bin(dec_num) in order to pass the tests.

Make sure that you use stack while solving this challenge. The stack.py has
been imported to the code. You can make use of the implementation while
coding your solution. Remove the pass statement if you start implementing
your solution.

Good luck!

Show Hint
main.py from stack import Stack

def convert_int_to_bin(dec_num):
stack.py pass

You might also like