I need a way to have my program take a given decimal integer, and then in a loop print each binary equivalent up to that number.

for instance if the number is 7 I need my output to be

0000

0001

0010

0011

0100

0101

0110

0111

the zeros need to be included. Right now I'm having it print but later I'll be storing each character in an array or list. I'll figure that bit out later.

Any help is greatly appreciated.

# printing decimal to binary quick help needed

Page 1 of 1## 8 Replies - 9468 Views - Last Post: 29 March 2009 - 10:18 PM

##
**Replies To:** printing decimal to binary quick help needed

### #2

## Re: printing decimal to binary quick help needed

Posted 09 February 2009 - 10:09 AM

Hi, here is a small function that should help you get started.

This function will take a decimal and print out its binary equivalent. Because of the method it uses the binary is reversed, but you can figure that out!

edit: Because for some reason I originally wrote BinaryToDecimal...

def DecimalToBinary(decimal): if(decimal == 0): return 0; else: print(decimal % 2); DecimalToBinary((int)(decimal / 2));

This function will take a decimal and print out its binary equivalent. Because of the method it uses the binary is reversed, but you can figure that out!

edit: Because for some reason I originally wrote BinaryToDecimal...

This post has been edited by **headways_millennia**: 09 February 2009 - 10:30 AM

### #3

## Re: printing decimal to binary quick help needed

Posted 09 February 2009 - 10:38 AM

thanks for the response. After some digging around while I was in class today I discovered that in python 2.6.1 support for a %b was added. So you can use it like a %d in a print statement such as

print "the binary value is: %03b" % binaryVariable

It works almost identically to the printf of java

The funniest part of it all is that it doesn't help me solve my problem, and am now doing this in sage as it has built in functions for just such a problem as the one I'm trying to solve. For those who don't know sage is a powerful math program built on the python language. The two are almost interchangable as you can write a file with the .py extension and change the extension to .sage and sage 9 times out of 10 won't bat an eye at running the program. The benefit of sage is that it has functions such as Integer(x).Binary() which autconverts an int to bin. VERY USEFUL as python requires you find and/or write your own method.

Anyhow, thanks for the input, and all the help. I couldn't make it without the DiC community.

T.

print "the binary value is: %03b" % binaryVariable

It works almost identically to the printf of java

The funniest part of it all is that it doesn't help me solve my problem, and am now doing this in sage as it has built in functions for just such a problem as the one I'm trying to solve. For those who don't know sage is a powerful math program built on the python language. The two are almost interchangable as you can write a file with the .py extension and change the extension to .sage and sage 9 times out of 10 won't bat an eye at running the program. The benefit of sage is that it has functions such as Integer(x).Binary() which autconverts an int to bin. VERY USEFUL as python requires you find and/or write your own method.

Anyhow, thanks for the input, and all the help. I couldn't make it without the DiC community.

T.

### #4

## Re: printing decimal to binary quick help needed

Posted 09 February 2009 - 11:58 AM

Ok cool. Here is a simple Python solution based on what I posted earlier.

Output:

#DecimalToBinary def DecimalToBinary(decimal, emptyList): if(decimal == 0): return 0; else: emptyList.append(decimal % 2); DecimalToBinary((int)(decimal / 2), emptyList); #Empty List binary = [] #Loop for cnt in range(7): DecimalToBinary(cnt+1, binary) binary.reverse() for num in binary: print(num, end="") binary = [] print('\n')

Output:

1 10 11 100 101 110 111

### #5

## Re: printing decimal to binary quick help needed

Posted 09 February 2009 - 12:35 PM

great. thanks for the help. I'm pretty well there. now I just need to figure a way to create a list of these values and I'm set. Not sure if you saw my other posts but this is part of a much larger issue. I need to find a way to create a list such that the values are

000

001

010

011

100

101

110

111

(this is all dependent on the initial list I"m given. it could go up to 256). long story short I use the original list, with the newly created one to get the disjunctive normal form of the equation that would result in a certain truth table. Not sure if I need the list to be a multi dimension sort or if a simple list will work. As you've been an immense help, any thoughts on how to do either?

by the way, thanks for all the help. much kudos headed your way

T.

P.S. the zeros are a non issue now as I have ways of fixing it in either sage or python. So any help won't need to include code for that.

000

001

010

011

100

101

110

111

(this is all dependent on the initial list I"m given. it could go up to 256). long story short I use the original list, with the newly created one to get the disjunctive normal form of the equation that would result in a certain truth table. Not sure if I need the list to be a multi dimension sort or if a simple list will work. As you've been an immense help, any thoughts on how to do either?

by the way, thanks for all the help. much kudos headed your way

T.

P.S. the zeros are a non issue now as I have ways of fixing it in either sage or python. So any help won't need to include code for that.

This post has been edited by **troegs**: 09 February 2009 - 12:38 PM

### #6

## Re: printing decimal to binary quick help needed

Posted 09 February 2009 - 12:42 PM

The more I'm looking at your code I'm thinking you've put code in there to store the values in a list. am I mistaken? Sorry for my noobishness but I literally started looking at python last night, so there are some gaping holes in what little knowledge I have. The only thing I have going for me is a relatively decent working knowledge of java which helps me in determining the right terms for what I want to do, which makes looking them up a whole heck of a lot easier. I'm liking python a lot but it sure isn't java!

### #7

## Re: printing decimal to binary quick help needed

Posted 09 February 2009 - 01:07 PM

Quote

The more I'm looking at your code I'm thinking you've put code in there to store the values in a list. am I mistaken?

Yes, the values are stored in the list called binary before being printed out, after which the binary list is emptied.

If you want to store the values in a list as they are created you can add this little bit to the above for loop:

#CREATE A LIST valueList = [] #ADD VALUES TO LIST IN FOR LOOP for cnt in range(7): DecimalToBinary(cnt+1, binary) binary.reverse() #Add values here valueList.append(binary) for num in binary: print(num, end="") binary = [] print('\n')

Output:

valueList [[1], [1, 0], [1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]] valueList[0] [1] valueList[1] [1, 0] valueList[2] [1, 1] valueList[3] [1, 0, 0]

etc...

This post has been edited by **headways_millennia**: 09 February 2009 - 01:09 PM

### #8

## Re: printing decimal to binary quick help needed

Posted 09 February 2009 - 01:42 PM

Outstanding. that's exactly what I need. thanks again for all the help. When this is all said and done I'll post a final so you can see how this was all used.

T.

T.

### #9

## Re: printing decimal to binary quick help needed

Posted 29 March 2009 - 10:18 PM

troegs, on 9 Feb, 2009 - 09:38 AM, said:

thanks for the response. After some digging around while I was in class today I discovered that in python 2.6.1 support for a %b was added. So you can use it like a %d in a print statement such as

print "the binary value is: %03b" % binaryVariable

It works almost identically to the printf of java

The funniest part of it all is that it doesn't help me solve my problem, and am now doing this in sage as it has built in functions for just such a problem as the one I'm trying to solve. For those who don't know sage is a powerful math program built on the python language. The two are almost interchangable as you can write a file with the .py extension and change the extension to .sage and sage 9 times out of 10 won't bat an eye at running the program. The benefit of sage is that it has functions such as Integer(x).Binary() which autconverts an int to bin. VERY USEFUL as python requires you find and/or write your own method.

Anyhow, thanks for the input, and all the help. I couldn't make it without the DiC community.

T.

print "the binary value is: %03b" % binaryVariable

It works almost identically to the printf of java

The funniest part of it all is that it doesn't help me solve my problem, and am now doing this in sage as it has built in functions for just such a problem as the one I'm trying to solve. For those who don't know sage is a powerful math program built on the python language. The two are almost interchangable as you can write a file with the .py extension and change the extension to .sage and sage 9 times out of 10 won't bat an eye at running the program. The benefit of sage is that it has functions such as Integer(x).Binary() which autconverts an int to bin. VERY USEFUL as python requires you find and/or write your own method.

Anyhow, thanks for the input, and all the help. I couldn't make it without the DiC community.

T.

if %b works, you can use list comprehension:

array = ['%05b' %n for n in range(2**5)]

where 5 is the bitlength

in python 3.0 use:

array = ['{0:0{1}b}'.format(i,bitLength) for i in range(2**bitLength)]

where bitLength is the length of bits

This post has been edited by **confucius13**: 29 March 2009 - 10:22 PM

Page 1 of 1