So when we do xor (^) operation between the current value of result & 1, the result will be set to 1 if the result is currently 0, otherwise 1. With the condition ((no & 1) = 1), we check if the current LSB is 1 or 0, if 1, we do result ^= 1. In the above code snippet, we are going through all the bits in the while loop one by one. So the naive way is to keep doing a bit-wise right shift on the given number & check the current least significant bit (LSB) to keep track of the result. If the total number of set bits is odd, parity is 1 else 0. We can calculate the total number of set bits in the binary representation of the given number. The problem statement clearly states what parity is. Parity of a number is 1 if the total number of set bits in the binary representation of the number is odd else parity is 0. Design an algorithm considering such scale. Hypothetically you have to serve a huge scale like 1 million numbers per minute. You are getting a stream of numbers (say long type numbers), compute the parity of the numbers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |