![]() This comes at the cost of bit logic being rather difficult, but in such a simple case as looking for number parity (even/odd), the performance gain is worth the minor complexity. However, bit logic tends to be slightly more performant. Var oddNumbers = numbers.Where(x => x % 2 = 1).ToList() One other example would be var evenNumbers = numbers.Where(x => x % 2 = 0).ToList() There are other possible solutions here, but they all boil down to the same principle. What is important here is that you've now correctly separated the evens and the odds. The labels of the groups are of no importance. In the group labeled 00000001, you will find all numbers which ended on a 1, which is all odd numbers. In the group labeled 00000000, you will find all numbers which ended on a 0, which is all even numbers. Using our demo values, the result is either 00000000 or 00000001 (because the first 7 digits are always 0, and the last digit could be different). These groups are being made based on the calculated value x & 1. It groups all elements which have the same calculated value in the same group. ToLookup is essentially a group by statement. If the result ends on a 1, then the first number is odd. If the result ends on a 0, then the first number is even. If the first number is odd, the result will also end on a 1, because 1 AND 1 = 1 If the first number is even, the result will also end on a 0, because 0 AND 1 = 0. So we could rewrite our earlier conclusion to say the following: ![]() Remember what we said before? In binary, all odd numbers end in 1, all even numbers end in 0. If the first number ends on a 1, the result will also end on a 1, because 1 AND 1 = 1 If the first number ends on a 0, the result will also end on a 0, because 0 AND 1 = 0. When will it be 1, when will it be 0? Think about this before reading the answer below. 146 of the 151 are even-proton, even-neutron (EE) nuclides, which necessarily have spin 0 because of pairing. Even though we don't know the first number, the second number already reveals that the first 7 digit positions will definitely result in a 0, because the second number already forces this by having 0s in those positions. Even-mass-number nuclides, which comprise 151/252 60 of all stable nuclides, are bosons, i.e., they have integer spin. In any position, if there is at least one 0, the result is definitely 0. Seems impossible, right? Well, let's think about it. Try to figure out how much of the result you can already guess. I'm going to hide the first number, and only show you the second number. In all other positions, there is always a 0 (false) in one of the two input numbers, and therefore the outcome is 0 (false) Notice how only in the leftmost position, both bits are 1 (true), and therefore the result is also 1 (true). Now let's look at two random binary numbers, and for each digit position, figure out what the AND result would be.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |