7
\$\begingroup\$

I'm trying to solve the following problem :

You are given two integers, X and Y. Calculate the number of bits that will be changed in order to convert integer X to integer Y.

Here is my solution

function findNumberOfBits(x, y){
 var bitCount = 0;
 var z = x ^ y; //XOR x and y
 while (z !== 0){
 bitCount += z & 1; 
 z = z >> 1; //shift Z by 1 bit to the right
 //console.log("bitCount ", bitCount);
 //console.log(z);
 }
 return bitCount;
}
var result = findNumberOfBits(12, 16);
  1. Is this correct? My tests seem to say yes, but just to make sure.
  2. Is this the most efficient way to write it in JavaScript?
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Oct 16, 2014 at 13:26
\$\endgroup\$
1

2 Answers 2

4
\$\begingroup\$

Comments

Your current comments aren't all that helpful, as they are just repeating excactly what the code already told me.

To improve them, think about why you are doing what you are doing, and what the result represents. For example:

var z = x ^ y; //XOR x and y

Right, so ^ is bitwise xor. But why are you doing this? What is z after this operation?

var z = x ^ y; // set bits in z that are different in x and y to 1.

This is a bit more helpful (but you can improve on the phrasing if you want).

Misc

  • Your indentation is off a bit.
  • remove debug statements

If you do this, your code might look like this:

function findNumberOfBits(x, y) {
 var bitCount = 0;
 var z = x ^ y; // set bits in z that are different in x and y to 1.
 while (z !== 0) { // not all bits are 0
 bitCount += z & 1; // add least significant bit
 z = z >> 1; // iterate over z; set next bit as lsb
 }
 return bitCount;
}

The comments might be a bit much (you can remove some if you want), but they are not just repeating the code.

Is this correct? My tests seem to say yes, but just to make sure.

Looks good to me.

Is this the most efficient way to write it in JavaScript?

If your code should work platform-independent, probably. Otherwise, no. You need the xor, but to count the number of set bits in the result, there might be better approaches.

What you are calculating is called the hamming distance, and here you can see some efficient implementations for the hamming weight (the count of set bits).

See also this question about counting the number of set bits in an integer.

answered Oct 16, 2014 at 15:23
\$\endgroup\$
1
  • 2
    \$\begingroup\$ @TopinFrassi I mentioned that in my answer (you could probably remove the second and fourth comment). This seems like a training exercise, so I think too many comments aren't bad. But even in real life code, I prefer too many comments over too few for bit operations. \$\endgroup\$ Commented Oct 16, 2014 at 17:13
2
\$\begingroup\$

Your solution seems good, but your comments aren't helpful (as tim stated). The explain the use of the ^ and >> operator. I think the intention is good, but these comments aren't helpful to you or to anyone that already knows what these operator do. You should remove them and assume the next developper who will go through your code will go check on internet to find the signification of these operators.

I think you should add an helpful comment to the algorithm that would explain why you shift bits to the right in your loop as it will explain your algorithm.

Remember, comments are there to explain why, not how.

answered Oct 16, 2014 at 17:11
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.