Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 73cd2da

Browse files
Number of Even and Odd Bits
1 parent 0a05a8f commit 73cd2da

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Link: https://leetcode.com/problems/number-of-even-and-odd-bits/
2+
Language: C#
3+
4+
5+
6+
7+
8+
9+
10+
public class Solution {
11+
public int[] EvenOddBit(int n)
12+
{
13+
int even = 0, odd = 0;
14+
short counter = 0;
15+
16+
while (n > 0)
17+
{
18+
int x = n & 1;
19+
n >>= 1;
20+
21+
if (x == 1)
22+
{
23+
if (counter % 2 == 0)
24+
even++;
25+
else
26+
odd++;
27+
}
28+
29+
counter++;
30+
}
31+
32+
return [even, odd];
33+
}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /