<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
</style>
</head>
<body class='hmmessage'>
By "binary representation", I mean a byte of 0s and 1s. Example: 00000101<BR>
Also, I'm interested in only using while loop and if statement to accomplish this task.<BR>
Thanks.<BR><BR>> Date: Wed, 4 Feb 2009 17:18:25 -0800<BR>> Subject: Re: Using while loop and if statement to tell if a binary has an odd or even number of 1's.<BR>> From: clp2@rebertia.com<BR>> To: todpose@hotmail.com<BR>> CC: python-list@python.org<BR>> <BR>> On Wed, Feb 4, 2009 at 5:02 PM, todpose@hotmail.com <todpose@hotmail.com> wrote:<BR>> > Using while loop and if statement, I'm trying to get Python to tell me<BR>> > whether there are even or odd number of 1's in a binary representation.<BR>> > For example, if I give Python a 00000111, then I want it to say that the<BR>> > binary representation given has an odd number of 1's.<BR>> > If I give it 00010111, then it will tell me that there is an even number of<BR>> > 1's.<BR>> > I'd appreciate any suggestion.<BR>> <BR>> Please define "binary representation". Do you mean a sequence of<BR>> bytes, an integer, a string of 0s and 1s, or something else entirely?<BR>> <BR>> If it's a string of 0s and 1s, then:<BR>> is_even = zerosones.count('1') % 2 == 0<BR>> <BR>> For an integer:<BR>> is_even = bin(the_int)[2:].count('1') % 2 == 0<BR>> <BR>> For the others, I don't know offhand.<BR>> <BR>> Cheers,<BR>> Chris<BR>> <BR>> -- <BR>> Follow the path of the Iguana...<BR>> http://rebertia.com<BR><BR><br /><hr />The new Windows Live Messenger.  <a href='http://www.microsoft.com/windows/windowslive/messenger.aspx' target='_new'>You don’t want to miss this.</a></body>
</html>