3

Assume that I have an ordinary number in Decmical base, now what I want to know is determining whether it has an even number of 1's in a binary base or not.and yet again I should emphasize the fact that I do not want the exact number of 1's in the binary base,just knowing if the number of 1's is even or odd.(by the way if you find a way to determine the exact number of 1's that should work as well)

Thanks in advance.

I'm new to English math so I apologize for any mistakes in the typing.(tried to be as clear as possible though :D)

Please feel free to edit the question and the tags if you wish.

Minuano
  • 617

3 Answers3

8

We can define a recursive function that will return $0$ for an even number of binary $1$s and $1$ for an odd number of binary $1$s using these facts:

$2k$ has the same number of binary $1$s as $k$ has ($2k$ is obtained from $k$ by appending a $0$ to the binary representation of $k$).

$2k+1$ has one more binary $1$ than $k$ has ($2k+1$ is obtained from $k$ by appending a $1$ to the binary representation of $k$).

Our function can then be $f(0)=0$; $f(2k)=f(k)$; and $f(2k+1)=1-f(k)$. Then $f$ will return $0$ for an even number of binary ones, and $1$ for an odd number of binary $1$s.

paw88789
  • 40,402
  • pretty nice! thank you – Minuano Sep 18 '14 at 16:00
  • 1
    You're welcome. By the way, if you wanted to count binary $1$s, you could just change $f(2k+1)=1-f(k)$ in the function to $f(2k+1)=1+f(k)$ – paw88789 Sep 18 '14 at 16:02
  • your answer was phenomenal,yet since I want to use this way in programming -as you may know- defining and using recursive function is quite time consuming.I'd like a quicker way which is proper to be used in my program.Thanks again! – Minuano Sep 18 '14 at 16:23
  • In that case, keep dividing by $2$. Each time you get a remainder of $1$ you are getting another binary $1$. Each time you get a remainder of $0$ you are getting a binary $0$. – paw88789 Sep 18 '14 at 16:39
  • Thanks for the reply.This is exactly what I've been trying to use in the programming,I was looking for a way faster than that.I couldn't get anywhere with this but my idea was that it is pretty easy to understand how many digits a decimal number has in binary base (using logarithm of course) then if there was a way to separate zeroes from ones I would have my problem solved.Do you happen to have a idea about that? – Minuano Sep 18 '14 at 17:07
  • I would think this would work fast: Pseudocode: 1) Input n. 2) Ones = 0. 3) While n > 0. 4) Ones = Ones + (n%2) [% is mod]. 5) n = n div 2 [div is integer quotient]. 6) End while. 7) Return Ones – paw88789 Sep 18 '14 at 17:37
  • @paw88789 "if you wanted to count binary 1s, you could just change" for this, i think the change has to be f(0)=0 to f(0)=1 – Moika Turns Sep 30 '22 at 21:46
  • an example in code (javascript) : f=k=>k?k&1?1-f(k>>1):f(k>>1):0 – Moika Turns Sep 30 '22 at 21:58
0

As far as I know there is only one binary base for numeric representation. In this base, it is very easy to count the number of $1$ digits: take the number logically AND'd (&) with $1$, then subtract this result from the number and divide by $2$, then add this result to the total, repeating until the number is $0$.

In pseudocode,

num = 71
Res=0
While (num > 0) {
  current = num & 1
  Res = Res + current
  num = (num - current) / 2
}
Return Res

Supposing that you wish to modify this to look for a specific digit in another base, you would change the above as current = num % mod and Res = Res + (current == chosenDigit ? 1 : 0) and num = (num - current) / mod

abiessu
  • 8,115
-1

Approach:

From a starting state of 0 (an even number of 1s) and a number, iterate through the sequence of bits in the number's binary form. The order is inconsequential (whether from the least significant bit or the reverse). For each bit compute the next state by XOR'g the current bit with the current state. The final value of state is the result (0 for even and 1 for odd).

|state |next bit in sequence|XOR gives new state|
|------|--------------------|-------------------|
|0     |0                   |0                  |
|0     |1                   |1                  |
|1     |0                   |1                  |
|1     |1                   |0                  |

Worked examples:

Example 1

The number 5 has binary form 101. Starting state is 0. Three XOR operations are performed (one for each bit in 101):

|step|current state|current bit|new state|
|----|-------------|-----------|---------|
|1   |0            |1          |1        |
|2   |1            |0          |1        |
|3   |1            |1          |0(result)|

Result = 0 (even)

Example 2

The number 7 has binary form 111. Starting state is 0. Three XOR operations are performed (one for each bit in 111):

|step|current state|current bit|new state|
|----|-------------|-----------|---------|
|1   |0            |1          |1        |
|2   |1            |1          |0        |
|3   |0            |1          |1(result)|

Result = 1 (odd)

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. – Community Sep 30 '22 at 23:36