Question:
I am working on a python program using a for loop to change a decimal integer to binary. help please!!?
Patrick
2012-09-24 10:38:01 UTC
We are using python program to change a decimal integeer to binary. so far i have:

def main():
m=eval(input("Enter the number in which you would like to change into binary"))
for i in range(8):
m=m//2
print(m%2)
My teacher says that for this to work correctly i must change the conditions of the for loop slightly. Any ideas of what i should change?

edit: the question exactly is to:

convert a decimal integer m to binary.
divide the number by two, keep the quotient and remainded (0 or 1)
divide each new quotient by two and keep the remainder
starting with the bottom, read the sequence of remainders upwards to the top.
Three answers:
husoski
2012-09-24 14:31:26 UTC
Converting by division (that's what you are doing) produces "digit" in the new base in least-significant first order (from right to left, as normally written). That means you can's just print them as you go.



It looks like you're using Python 3.x, which I don't have installed, so use this as an idea, not a copy-paste:



To get the first digit:



output = str(m%2)

m = m/2



Getting the next digit, after one or more have been converted already, is almost the same:



output = str(m%2) + output

m = m/2



Notice that the digit is tacked onto the LEFT side of the previous value of output. That puts the digits in order so when you are done converting you can just:



print(output)



The type of loop depends on how you want to format the number. To get a fixed number of bits, padding leading 0 bits if needed, you can use a for loop. If you want to suppress leading zeroes, convert the first bit outside the loop, in case the original number was 0, and then:



while m != 0:

... convert the next digit



You can use a while to combine both features:



while m != 0 or len(s) < MIN_LENGTH:

... convert the next digit



That will convert at least (MIN_LENGTH) bits, padding 0s if needed, but keep on going if the binary representation of the original m value is more than (MIN_LENGTH) bits.



Warning: The while versions above will loop forever if given a negative value for m. You should test for and eliminate that possiblity. The fixed-width for-loop version will print out the twos-complement representation, truncated to the number of bits converted by the loop.
Jared
2012-09-24 13:16:18 UTC
How about something similar to this:



def int2bin(a):

rem = []

rem2 = []

print a,0

for i in xrange(8):

tmp = a // 2

rem.append(a % 2)

rem2.insert(0,a % 2)

a = tmp



print rem[::-1] # prints the remainders in reverse

print rem2 # remainders already reversed.



For this case 8 works, but if you want a true binary number, in computer science terms, all 4 or 8 bytes of the integer that you are working with. If you are just messing around, then you can use a while loop instead of a for loop such as



while (a != 0):



If you do a constrained problem like in C or something other that has a strict definition of int, you can use a for loop over the sequence of the byte and either break the loop or just let it run through since 0/2 will always be 0. Anyways, good luck.
casida
2016-10-24 07:55:06 UTC
public classification attempt { public static void major(String [] args) { String binary = "110110"; /* an social gathering */ char[] charArray = binary.toCharArray(); /* an array of characters as above */ int result = 0; for(char theCharacter: charArray) { result *= 2; /* you should apply a shift right here yet it is more beneficial obtrusive */ if(theCharacter == 'a million') { result++; } } equipment.out.println("Binary: " + binary + " is " + result); } } ------ Strictly speaking it extremely is 1/2 the question in fact, you should apply yet another loop to exhibit it back to a decimal string extremely than in basic terms using the print function. i'd use a "at the same time as" loop for this and that i'll leave that section to you.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...