Counting the ones in the binary representation of a number in C# [on hold]
up vote
-1
down vote
favorite
I want to count the 1s in the binary representation of a number (input
) in C#.
Which is more readable:
Enumerable
.Range(0, 32)
.Aggregate(0, (acc, i) => acc + ((input >> i) & 1));
or
Convert.ToString(input, 2).Count(x => x == '1');
c# comparative-review linq
put on hold as off-topic by t3chb0t, Toby Speight, IEatBagels, Mast, Ismael Miguel 20 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Toby Speight, IEatBagels, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-1
down vote
favorite
I want to count the 1s in the binary representation of a number (input
) in C#.
Which is more readable:
Enumerable
.Range(0, 32)
.Aggregate(0, (acc, i) => acc + ((input >> i) & 1));
or
Convert.ToString(input, 2).Count(x => x == '1');
c# comparative-review linq
put on hold as off-topic by t3chb0t, Toby Speight, IEatBagels, Mast, Ismael Miguel 20 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Toby Speight, IEatBagels, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.
Why the downvote?
– K. Gkinis
yesterday
1
I can't speak for the person that downvoted, but your question is getting some close-votes for 'lack of context' - it would help to know in what context this code is used. Either way, I'd say that the more readable solution is to encapsulate this logic in aBitCount(int input)
method. Not only does it make the intent clearer at call-sites, it also allows you to swap for a more efficient approach if the need arises.
– Pieter Witvoet
23 hours ago
Unfortunately, there is no better context. It's not actual code, it's more of an argument over readability over an exercise, so maybe it's too broad.
– K. Gkinis
21 hours ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I want to count the 1s in the binary representation of a number (input
) in C#.
Which is more readable:
Enumerable
.Range(0, 32)
.Aggregate(0, (acc, i) => acc + ((input >> i) & 1));
or
Convert.ToString(input, 2).Count(x => x == '1');
c# comparative-review linq
I want to count the 1s in the binary representation of a number (input
) in C#.
Which is more readable:
Enumerable
.Range(0, 32)
.Aggregate(0, (acc, i) => acc + ((input >> i) & 1));
or
Convert.ToString(input, 2).Count(x => x == '1');
c# comparative-review linq
c# comparative-review linq
edited yesterday
200_success
127k15149412
127k15149412
asked yesterday
K. Gkinis
1175
1175
put on hold as off-topic by t3chb0t, Toby Speight, IEatBagels, Mast, Ismael Miguel 20 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Toby Speight, IEatBagels, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by t3chb0t, Toby Speight, IEatBagels, Mast, Ismael Miguel 20 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Toby Speight, IEatBagels, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.
Why the downvote?
– K. Gkinis
yesterday
1
I can't speak for the person that downvoted, but your question is getting some close-votes for 'lack of context' - it would help to know in what context this code is used. Either way, I'd say that the more readable solution is to encapsulate this logic in aBitCount(int input)
method. Not only does it make the intent clearer at call-sites, it also allows you to swap for a more efficient approach if the need arises.
– Pieter Witvoet
23 hours ago
Unfortunately, there is no better context. It's not actual code, it's more of an argument over readability over an exercise, so maybe it's too broad.
– K. Gkinis
21 hours ago
add a comment |
Why the downvote?
– K. Gkinis
yesterday
1
I can't speak for the person that downvoted, but your question is getting some close-votes for 'lack of context' - it would help to know in what context this code is used. Either way, I'd say that the more readable solution is to encapsulate this logic in aBitCount(int input)
method. Not only does it make the intent clearer at call-sites, it also allows you to swap for a more efficient approach if the need arises.
– Pieter Witvoet
23 hours ago
Unfortunately, there is no better context. It's not actual code, it's more of an argument over readability over an exercise, so maybe it's too broad.
– K. Gkinis
21 hours ago
Why the downvote?
– K. Gkinis
yesterday
Why the downvote?
– K. Gkinis
yesterday
1
1
I can't speak for the person that downvoted, but your question is getting some close-votes for 'lack of context' - it would help to know in what context this code is used. Either way, I'd say that the more readable solution is to encapsulate this logic in a
BitCount(int input)
method. Not only does it make the intent clearer at call-sites, it also allows you to swap for a more efficient approach if the need arises.– Pieter Witvoet
23 hours ago
I can't speak for the person that downvoted, but your question is getting some close-votes for 'lack of context' - it would help to know in what context this code is used. Either way, I'd say that the more readable solution is to encapsulate this logic in a
BitCount(int input)
method. Not only does it make the intent clearer at call-sites, it also allows you to swap for a more efficient approach if the need arises.– Pieter Witvoet
23 hours ago
Unfortunately, there is no better context. It's not actual code, it's more of an argument over readability over an exercise, so maybe it's too broad.
– K. Gkinis
21 hours ago
Unfortunately, there is no better context. It's not actual code, it's more of an argument over readability over an exercise, so maybe it's too broad.
– K. Gkinis
21 hours ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Why the downvote?
– K. Gkinis
yesterday
1
I can't speak for the person that downvoted, but your question is getting some close-votes for 'lack of context' - it would help to know in what context this code is used. Either way, I'd say that the more readable solution is to encapsulate this logic in a
BitCount(int input)
method. Not only does it make the intent clearer at call-sites, it also allows you to swap for a more efficient approach if the need arises.– Pieter Witvoet
23 hours ago
Unfortunately, there is no better context. It's not actual code, it's more of an argument over readability over an exercise, so maybe it's too broad.
– K. Gkinis
21 hours ago