Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Unit 2 Practice Test Coding Problem 5 (External resource) Does anyone know how to write this code to print out the binary.. specifically using floor

Unit 2 Practice Test Coding Problem 5 (External resource)

Does anyone know how to write this code to print out the binary.. specifically using floor division and modulus ... im very lost

image text in transcribedimage text in transcribed

\#You may modify the lines of code above, but don't move them! \#When you submit your code, we'll change these lines to \#assign different values to the variables. \# \#The number above is given in base 10. Let's convert it to \#base 2 and print it in binary. For example, 215 can be written \#in binary as 11010111. \# \#Each digit of that string corresponds to a power of 2 . The far \#left digit represents 128 , then 64 , then 32 , then 16 , then 8 , \#then 4, then 2, and then finally 1 at the far right. \# \#To convert the number to binary, first check to see if it is \#greater than or equal to 128 . If it is, your first digit is 1. \#If not, your first digit is 0 . If the number was greater than \#128, subtract 128 (because the 128 is captured by the first \#digit of the string). \# \#Then, repeat that process for 64,32,16,8,4,2, and 1. \# \#For example: \# \#215 is >= 128:1 #87 is >=64:11 \#23 is not >=32:110 #23 is >=16:1101 #7 is not >= 8:11010 \#7 is >=4:110101 #3 is >=2:1101011 \#1 is >= 1: 11010111 \# \#Note that although we use 'if' a lot to describe this problem, \#this can be done entirely with floor division and modulus. \#Remember, if you convert a boolean to an integer, True becomes #1 and False becomes 0 . 9 \#Note that we always work with binary in 8-bit chunks : the 10 \#number 7 would be 00000111 , not just 111 . That s because inside 11 \#the computer, 8 ss and 0 m make a byte, which is the smallest 12 \#practical unit of storage (rarely are bits used outside 8 -bit 3 \#bytes). 4 \# \# 15 \#Print the string that results from this conversion. 17 18 \#Add your code here! 9 \#

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions