Skip to content

Commit

Permalink
Merge pull request #44 from justanindieguy/CountBits
Browse files Browse the repository at this point in the history
CountBits
  • Loading branch information
justanindieguy authored Jan 30, 2023
2 parents 6a3e70f + 01b09db commit 0ad1222
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Bit-manipulationTechniques/CountBits/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>

using namespace std;

int countBits(int n)
{
int count = 0;

while (n > 0)
{
int lastBit = n & 1;
count += lastBit;
n = n >> 1;
}

return count;
}

int main()
{
int n;
cin >> n;

cout << countBits(n) << endl;

return 0;
}
18 changes: 18 additions & 0 deletions Bit-manipulationTechniques/CountBits/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def count_bits(n: int):
count = 0

while n > 0:
last_bit = n & 1
count += last_bit
n = n >> 1

return count


def main():
n = int(input())
print(count_bits(n))


if __name__ == "__main__":
main()

0 comments on commit 0ad1222

Please sign in to comment.