w3resource

JavaScript - Parity of a given number

JavaScript Bit Manipulation: Exercise-13 with Solution

Write a JavaScript program to calculate and find the parity of a given number.

In mathematics, parity is the property of an integer of whether it is even or odd. In binary numbers, parity refers to the total number of 1s. The odd parity(1) represents an odd number of 1s, whereas the even parity(0) represents an even number of 1s.
In information theory, a parity bit appended to a binary number provides the simplest form of error detecting code. If a single bit in the resulting value is changed, then it will no longer have the correct parity: changing a bit in the original number gives it a different parity than the recorded one, and changing the parity bit while not changing the number it was derived from again produces an incorrect result. In this way, all single-bit transmission errors may be reliably detected. Some more sophisticated error detecting codes are also based on the use of multiple parity bits for subsets of the bits of the original encoded value.

Test Data:
(34) -> "Parity of 34 is even."
"34 in binary is 100010" // Even number of 1s
(104) -> "Parity of 104 is odd."
"104 in binary is 1101000" // Odd number of 1s

Sample Solution:

JavaScript Code:

const parity_even_odd = (n) => {
    parity = false
    temp = n 
    while (n != 0)
       {
        if ((n & 1) !== 0) {
           parity = !parity
        }
       n = n >> 1
      }
   if (parity)
     return "Parity of " +temp+ " is odd."
   else 
     return "Parity of " +temp+ " is even." 
  }
n = 34
console.log(n + " in binary is " + n.toString(2))
console.log(parity_even_odd(n))
n = 104
console.log(n + " in binary is " + n.toString(2))
console.log(parity_even_odd(n))

Sample Output:

34 in binary is 100010
Parity of 34 is even.
104 in binary is 1101000
Parity of 104 is odd.

Flowchart:

Flowchart: JavaScript - Parity of a given number.

Live Demo:

See the Pen javascript-bit-manipulation-exercise-12 by w3resource (@w3resource) on CodePen.


* To run the code mouse over on Result panel and click on 'RERUN' button.*

Improve this sample solution and post your code through Disqus

Previous: Position of the rightmost set bit of a number.
Next: Find the non-repeated element from an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

JavaScript: Tips of the Day

function and arguments

const person = {
  name: 'Lydia Hallie',
  hobbies: ['coding'],
};

function addHobby(hobby, hobbies = person.hobbies) {
  hobbies.push(hobby);
  return hobbies;
}

addHobby('running', []);
addHobby('dancing');
addHobby('baking', person.hobbies);

console.log(person.hobbies);

The addHobby function receives two arguments, hobby and hobbies with the default value of the hobbies array on the person object.
First, we invoke the addHobby function, and pass "running" as the value for hobby and an empty array as the value for hobbies. Since we pass an empty array as the value for y, "running" gets added to this empty array.
Then, we invoke the addHobby function, and pass "dancing" as the value for hobby. We didn't pass a value for hobbies, so it gets the default value, the hobbies property on the person object. We push the hobby dancing to the person.hobbies array.
Last, we invoke the addHobby function, and pass "bdaking" as the value for hobby, and the person.hobbies array as the value for hobbies. We push the hobby baking to the person.hobbies array.
After pushing dancing and baking, the value of person.hobbies is ["coding", "dancing", "baking"]

Ref: https://bit.ly/2Hcpkm6