Skip to content

[Bit Manipulation] Number of 1 Bits (Hamming Weight) #152

@sudosf

Description

@sudosf

Problem

Write a function that takes a positive integer and returns the number of set bits (1s) in its binary representation.

References

Difficulty

🟢 Easy

Companies

Amazon, Sumo Logic, Paytm

Notes

Language: Java
Trick: n & (n-1) clears the lowest set bit. Count how many times you can do this before n becomes 0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    easyEasy difficulty

    Projects

    Status
    Backlog

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions