# printing decimal to binary quick help needed

Page 1 of 1

## 8 Replies - 9640 Views - Last Post: 29 March 2009 - 10:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=85554&amp;s=afaccdf909b75647f0ff7b1ca552fc4a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 troegs

Reputation: 1
• Posts: 40
• Joined: 05-December 08

# printing decimal to binary quick help needed

Posted 09 February 2009 - 02:12 AM

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.

Is This A Good Question/Topic? 0

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

Reputation: 4
• Posts: 18
• Joined: 03-February 09

## 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.

```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 troegs

Reputation: 1
• Posts: 40
• Joined: 05-December 08

## 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.

Reputation: 4
• Posts: 18
• Joined: 03-February 09

## 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.

```#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 troegs

Reputation: 1
• Posts: 40
• Joined: 05-December 08

## 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.

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

### #6 troegs

Reputation: 1
• Posts: 40
• Joined: 05-December 08

## 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!

Reputation: 4
• Posts: 18
• Joined: 03-February 09

## 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()

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 troegs

Reputation: 1
• Posts: 40
• Joined: 05-December 08

## 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.

### #9 confucius13

Reputation: 0
• Posts: 1
• Joined: 29-March 09

## 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.

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