diff options
Diffstat (limited to 'number_1bits.cpp')
-rw-r--r-- | number_1bits.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/number_1bits.cpp b/number_1bits.cpp new file mode 100644 index 0000000..c8e0ac3 --- /dev/null +++ b/number_1bits.cpp @@ -0,0 +1,20 @@ +#include "leetcode.h" + +class Solution { +public: + int hammingWeight(uint32_t n) { + int count = 0; + while (n > 0) { + if ((n & 1) > 0) + ++count; + n = n >> 1; + } + return count; + } +}; + +int main() { + uint32_t n = 00000000000000000000000000001011; + Solution obj; + cout << obj.hammingWeight(n); +} |