Using while loop and if statement to tell if a binary has an odd or even number of 1's.
clp2 at rebertia.com
Thu Feb 5 02:39:28 CET 2009
A "byte" is *not* a Python type. My question was what *Python type*
(i.e. bytes (which is distinctly different from the abstract notion of
a byte), str/unicode, int, etc...) you were using for you "binary
representation", which you still haven't answered.
Also, please don't reply by top-posting
(http://en.wikipedia.org/wiki/Top_post#Top-posting), it's bad
netiquette. I'm only doing it in this message for consistency.
Follow the path of the Iguana...
On Wed, Feb 4, 2009 at 5:23 PM, todpose at hotmail.com <todpose at hotmail.com> wrote:
> By "binary representation", I mean a byte of 0s and 1s. Example: 00000101
> Also, I'm interested in only using while loop and if statement to accomplish
> this task.
>> Date: Wed, 4 Feb 2009 17:18:25 -0800
>> Subject: Re: Using while loop and if statement to tell if a binary has an
>> odd or even number of 1's.
>> From: clp2 at rebertia.com
>> To: todpose at hotmail.com
>> CC: python-list at python.org
>> On Wed, Feb 4, 2009 at 5:02 PM, todpose at hotmail.com <todpose at hotmail.com>
>> > Using while loop and if statement, I'm trying to get Python to tell me
>> > whether there are even or odd number of 1's in a binary representation.
>> > For example, if I give Python a 00000111, then I want it to say that the
>> > binary representation given has an odd number of 1's.
>> > If I give it 00010111, then it will tell me that there is an even number
>> > of
>> > 1's.
>> > I'd appreciate any suggestion.
>> Please define "binary representation". Do you mean a sequence of
>> bytes, an integer, a string of 0s and 1s, or something else entirely?
>> If it's a string of 0s and 1s, then:
>> is_even = zerosones.count('1') % 2 == 0
>> For an integer:
>> is_even = bin(the_int)[2:].count('1') % 2 == 0
>> For the others, I don't know offhand.
>> Follow the path of the Iguana...
> The new Windows Live Messenger. You don't want to miss this.
More information about the Python-list